a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁(yè) > 編程 > C# > 正文

使用C#開發(fā)檢測(cè)并關(guān)閉彈出窗口的方法

2023-05-10 18:49:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文給出了開發(fā)關(guān)閉彈出窗口的方法。

基本原理:本系統(tǒng)可以自動(dòng)關(guān)閉IE彈出窗口的程序,它工作在系統(tǒng)的托盤中,按照一定的間隔來(lái)檢測(cè)IE窗口,然后關(guān)閉彈出窗體。最后,還提供了用熱鍵來(lái)殺掉彈出窗口的功能。

具體內(nèi)容如下: 

系統(tǒng)托盤

程序切換

計(jì)時(shí)控件

查找窗口

系統(tǒng)熱鍵

生成一個(gè)系統(tǒng)托盤程序

首先,產(chǎn)生一個(gè)新的C# Windows Form程序,將NotifyIcon控件從工具箱中拖到窗體中:

在C# windows Form程序中添加托盤

為了保證系統(tǒng)托盤的圖標(biāo)和應(yīng)用程序的圖標(biāo)一致,我們用一個(gè)共同的圖標(biāo)文件a.ico來(lái)設(shè)置系統(tǒng)托盤的圖標(biāo)和應(yīng)用程序的圖標(biāo)。

為了使程序不顯示在工具欄上,我們可以設(shè)置窗體的visible屬性為false. 這個(gè)可以在窗體屬性窗口中直接實(shí)現(xiàn)。

this.ShowInTaskbar = false;

到目前為止,系統(tǒng)托盤已基本好了,但是我們還沒有設(shè)置右鍵菜單,也沒有使程序顯示和隱藏的功能。

程序切換

首先,程序的主窗體可以根據(jù)不同的狀態(tài)來(lái)選擇顯示或者是隱藏,除此之外,我們可以用WindowState設(shè)置窗體的狀態(tài): 

public void HideApp()
{
this.WindowState = FormWindowState.Minimized;
Hide();
}
public void ShowApp()
{
Show();
this.WindowState = FormWindowState.Normal;
}

一個(gè)非常有趣的功能是讓用戶關(guān)閉窗體的時(shí)候程序并不是退出,為了實(shí)現(xiàn)這個(gè)功能,我們必須要重寫窗體的OnClosing事件。

protected override void OnClosing(CancelEventArgs e)
{
// 用最小化來(lái)代替關(guān)閉操作d
e.Cancel = true;
// 最小化,并且隱藏窗體
this.WindowState = FormWindowState.Minimized;
Hide();
}
當(dāng)然,我們必須要提供一個(gè)必須的退出方法.這個(gè)可以在托盤的右鍵菜單的exit中實(shí)現(xiàn),
private void menu_App_Exit(object sender, System.EventArgs e)
{
NativeWIN32.UnregisterHotKey(Handle, 100);
//隱藏托盤
notifyIcon1.Visible = false;
Application.Exit();
}

添加右鍵菜單

添加一個(gè)右鍵菜單和添加托盤基本一樣,從工具箱中添加context menu就可以.右鍵菜單在你鼠標(biāo)右鍵按下的時(shí)候是會(huì)自動(dòng)彈出的。

當(dāng)設(shè)置好右鍵菜單以后,我們必要要根據(jù)不同的情況來(lái)啟用或停用右鍵菜單,這個(gè)可以通過(guò)在菜單的BeforePopup設(shè)置。Enabled屬性來(lái)實(shí)現(xiàn)。 

private void menu_App_BeforePopup(object sender, System.EventArgs e)
{
if ( this.WindowState == FormWindowState.Minimized )
{
App_Show.Enabled = true;
App_Hide.Enabled = false;
}
else
{
App_Show.Enabled = false;
App_Hide.Enabled = true;
}
}

計(jì)時(shí)工具

.Net Framework的 Timer能和系統(tǒng)的Win32 timer實(shí)現(xiàn)一樣的功能。我們要做的就是設(shè)置一個(gè)timer,然后合理的設(shè)置屬性。

m_Timer = new System.Timers.Timer(); // explicit namespace (Timer also in System.Threading)
m_Timer.Elapsed += new ElapsedEventHandler(OnTimerKillPopup);
m_Timer.Interval = m_nInterval; // for instance 3000 milliseconds
m_Timer.Enabled = true; // start timer

protected void OnTimerKillPopup(Object source, ElapsedEventArgs e)
{
m_Timer.Enabled = false; // pause the timer

FindPopupToKill();

m_Timer.Enabled = true;
}

本地win32窗體查找

本程序的實(shí)現(xiàn)原理是這樣,先檢查所有的IE窗口標(biāo)題,然后于已經(jīng)有的列表來(lái)比較,如果有相同的,我們就關(guān)閉這個(gè)窗口。

按照上面的方法,我們每n妙使用KillPopup()來(lái)檢查。比較遺憾的是我們無(wú)法使用安全代碼來(lái)完成所有的工作。我們可以使用 System.Diagnostics.Proces來(lái)檢查所有的IE進(jìn)程,然后得到主窗體。但是每一個(gè)IE進(jìn)程可以打開好幾個(gè)窗口,雖然每一個(gè)窗體都于一個(gè)進(jìn)程相關(guān),但是還沒有辦法來(lái)使每一個(gè)窗體于進(jìn)程對(duì)應(yīng)起來(lái)。

一個(gè)可行的辦法使用System.Diagnostics.Process列舉出所有的運(yùn)行的進(jìn)程,然后System.Diagnostics.ProcessThreadCollection 來(lái)得到他們的.Threads屬性,為了得到thread Id,我們使用Win32 API EnumThreadWindows(DWORD threadId,WNDENUMPROC lpfn,LPARAM lParam) 來(lái)實(shí)現(xiàn),這是一個(gè)回調(diào)(call back)函數(shù),他可以列舉出于進(jìn)程相關(guān)的窗體。當(dāng)我們得到了窗體的句柄以后,我們可以使用另一個(gè)API函數(shù) GetWindowText(HWND hwnd,/*out*/LPTSTR lpString,int nMaxCount)來(lái)得到窗體的標(biāo)題,然后根據(jù)已經(jīng)有的窗體,調(diào)用API函數(shù)SendMessage(HWND hWnd,int msg,int wParam,int lParam)來(lái)關(guān)閉窗口。下面使演示代碼 

Process[] myProcesses = Process.GetProcessesByName("IEXPLORE");

foreach(Process myProcess in myProcesses)
{
FindPopupToKill(myProcess);
}

protected void FindPopupToKill(Process p)
{
// traverse all threads and enum all windows attached to the thread
foreach (ProcessThread t in p.Threads)
{
int threadId = t.Id;

NativeWIN32.EnumThreadProc callbackProc =
new NativeWIN32.EnumThreadProc(MyEnumThreadWindowsProc);
NativeWIN32.EnumThreadWindows(threadId, callbackProc, IntPtr.Zero /*lParam*/);
}
}

// callback used to enumerate Windows attached to one of the threads
bool MyEnumThreadWindowsProc(IntPtr hwnd, IntPtr lParam)
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;


// get window caption
NativeWIN32.STRINGBUFFER sLimitedLengthWindowTitle;
NativeWIN32.GetWindowText(hwnd, out sLimitedLengthWindowTitle, 256);

String sWindowTitle = sLimitedLengthWindowTitle.szText;
if (sWindowTitle.Length==0) return true;

// find this caption in the list of banned captions
foreach (ListViewItem item in listView1.Items)
{
if ( sWindowTitle.StartsWith(item.Text) )
NativeWIN32.SendMessage(hwnd, NativeWIN32.WM_SYSCOMMAND,
NativeWIN32.SC_CLOSE,
IntPtr.Zero); // try soft kill
}

return true;
}

public class NativeWIN32
{
public delegate bool EnumThreadProc(IntPtr hwnd, IntPtr lParam);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern bool EnumThreadWindows(int threadId, EnumThreadProc pfnEnum, IntPtr lParam);

// used for an output LPCTSTR parameter on a method call
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
public string szText;
}

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
}

上面的方法在性能上是不錯(cuò)的,因?yàn)樗^(guò)濾了其他非IE的窗口.但是我們可以用一個(gè)更簡(jiǎn)單的方法來(lái)實(shí)現(xiàn),就是調(diào)用API FindWindowEx(HWND hWndParent, HWND hWndNext, /*in*/LPCTSTR szClassName, /*in*/LPCTSTR szWindowTitle)方法.比較有用的是這句,我們可以使用registered window class name來(lái)找到IE窗口(IEFrame是所有打開的IE的標(biāo)識(shí)). 

protected void FindPopupToKill()
{
IntPtr hParent = IntPtr.Zero;
IntPtr hNext = IntPtr.Zero;
String sClassNameFilter = "IEFrame"; // 所有IE窗口的類
do
{
hNext = NativeWIN32.FindWindowEx(hParent,hNext,sClassNameFilter,IntPtr.Zero);

// we've got a hwnd to play with
if ( !hNext.Equals(IntPtr.Zero) )
{
// get window caption
NativeWIN32.STRINGBUFFER sLimitedLengthWindowTitle;
NativeWIN32.GetWindowText(hNext, out sLimitedLengthWindowTitle, 256);

String sWindowTitle = sLimitedLengthWindowTitle.szText;
if (sWindowTitle.Length>0)
{
// find this caption in the list of banned captions
foreach (ListViewItem item in listView1.Items)
{
if ( sWindowTitle.StartsWith(item.Text) )
NativeWIN32.SendMessage(hNext, NativeWIN32.WM_SYSCOMMAND,
NativeWIN32.SC_CLOSE,
IntPtr.Zero); // try soft kill
}
}
}
}
while (!hNext.Equals(IntPtr.Zero));

}

public class NativeWIN32
{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr parent /*HWND*/,
IntPtr next /*HWND*/,
string sClassName,
IntPtr sWindowTitle);

}

注冊(cè)系統(tǒng)熱鍵

系統(tǒng)熱鍵用在像彈出窗口殺手這種應(yīng)用程序非常有用, Ctrl+Shift+J是缺省熱鍵。

說(shuō)道實(shí)現(xiàn),我們繼續(xù)用RegisterHotkey(HWND hWnd, int id, UINT fsModifiers, UINT vkey)。完成,代碼如下: 

public void SetHotKey(Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
{
m_hotkey = c;
m_ctrlhotkey = bCtrl;
m_shifthotkey = bShift;
m_althotkey = bAlt;
m_winhotkey = bWindows;

// update hotkey
NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers.None;
if (m_ctrlhotkey)
modifiers |= NativeWIN32.KeyModifiers.Control;
if (m_shifthotkey)
modifiers |= NativeWIN32.KeyModifiers.Shift;
if (m_althotkey)
modifiers |= NativeWIN32.KeyModifiers.Alt;
if (m_winhotkey)
modifiers |= NativeWIN32.KeyModifiers.Windows;

NativeWIN32.RegisterHotKey(Handle, 100, modifiers, m_hotkey); //Keys.J);
}
一般的,注冊(cè)熱鍵要一下幾步

/* ------- using HOTKEYs in a C# application -------

-- code snippet by James J Thompson --

在Form的load 中 : Ctrl+Shift+J

bool success = RegisterHotKey(Handle,
100,
KeyModifiers.Control | KeyModifiers.Shift,
Keys.J);

在 form的closing中:

UnregisterHotKey(Handle, 100);

如何處理熱鍵 : 

protected override void WndProc( ref Message m )
{
const int WM_HOTKEY = 0x0312;

switch(m.Msg)
{
case WM_HOTKEY:

MessageBox.Show("Hotkey pressed");

ProcessHotkey();

break;
}
base.WndProc(ref m );
}


public class NativeWIN32
{
[DllImport("user32.dll", SetLastError=true)]
public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window
int id, // hot key identifier
KeyModifiers fsModifiers, // key-modifier options
Keys vk // virtual-key code
);

[DllImport("user32.dll", SetLastError=true)]
public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window
int id // hot key identifier
);

[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}

}
------- using HOTKEYs in a C# application ------- */

當(dāng)我們按下熱鍵以后,流程是這樣:首先用HWND GetForegroundWindow()來(lái)得到窗體,然后要抓出窗體的標(biāo)題, GetWindowText(HWND hwnd, /*out*/LPTSTR lpString, int nMaxCount). 具體如下: 

protected void ProcessHotkey()
{
IntPtr hwnd = NativeWIN32.GetForegroundWindow();
if (!hwnd.Equals(IntPtr.Zero))
{
NativeWIN32.STRINGBUFFER sWindowTitle;
NativeWIN32.GetWindowText(hwnd, out sWindowTitle, 256);

if (sWindowTitle.szText.Length>0)
AddWindowTitle( sWindowTitle.szText ); // add to the ListView (Form)
}
}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲午夜一区 | 精品一区二区三区蜜桃 | 亚洲成人毛片 | 精品国产一区二区三区久久影院 | 精品国产一区二区三区成人影院 | 国产亚洲欧美一区 | av在线免费观看网址 | 老牛影视av一区二区在线观看 | 日本精品999 | 久久人人爽人人爽人人片av高清 | 欧美一区二区免费 | 国产三区在线观看 | 蜜桃免费一区二区三区 | 日韩欧美高清视频 | 最新色站| 黄色毛片在线观看 | 国产 日韩 欧美 中文 在线播放 | 国产激情美女久久久久久吹潮 | 夜添久久精品亚洲国产精品 | 日本在线www| 亚洲日本高清 | 亚洲第一视频 | 国产1页| 99国产视频 | 久久久久国产一级毛片高清版小说 | 国产精品一区亚洲二区日本三区 | 日韩国产免费观看 | 久久中文视频 | 男女视频免费看 | 欧美久久久久久 | 97国产在线视频 | 亚洲二区视频 | 日韩一区欧美一区 | www.4虎| 欧美综合一区二区 | 欧美一区二区在线观看 | 亚洲巨乳自拍在线视频 | 手机在线观看毛片 | 欧美jizz18性欧美 | 91av导航| 久久se精品一区精品二区 |