淺談如何利用C#枚舉所有的窗體
C#枚舉所有的窗體的兩種方法
1、直接查找游戲窗口,找到后作處理。
2、C#枚舉所有窗口,列表顯示,然后再處理。
我這里按第二種方式做。首先是一些準備工作,如,了解如何調用系統(tǒng)API,見以前的博文。枚舉窗口要用的一些
API:EnumWindows,GetWindowText,GetParent,IsWindowVisible.
EnumWindows:枚舉窗口
GetWindowText:取得窗口標題
GetParent:取得當前窗體的父窗體(非常重要,用于判斷是否為頂級窗體)
IsWindowVisible:判斷窗體是否可見,用于過濾到不可見窗體。
C#枚舉代碼如下:
- namespaceHideProcess
- {
- publicdelegateboolCallBack(inthwnd,inty);
- publicpartialclassForm1:Form
- {
- [DllImport("user32.dll")]
- publicstaticexternintEnumWindows(CallBackx,inty);
- [DllImport("user32")]
- publicstaticexternintGetWindowText(inthwnd,StringBuilderlptrString,intnMaxCount);
- [DllImport("user32")]
- publicstaticexternintGetParent(inthwnd);
- [DllImport("user32")]
- publicstaticexternintIsWindowVisible(inthwnd);
- publicboolReport(inthwnd,intlParam)
- {
- intpHwnd;
- pHwnd=GetParent(hwnd);
- if(pHwnd==0&&IsWindowVisible(hwnd)==1)
- {
- StringBuildersb=newStringBuilder(512);
- GetWindowText(hwnd,sb,sb.Capacity);
- if(sb.Length>0)
- {
- this.comboBox1.Items.Add(sb.ToString());
- }
- }
- returntrue;
- }
- publicForm1()
- {
- InitializeComponent();
- }
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- Process[]ProcArray=Process.GetProcesses();
- comboBox1.Items.Clear();
- EnumWindows(this.Report,0);
- }
- }
- }
有一個combobox和button,點擊按鈕,將所有窗口列舉顯示在下拉框。接下來的工作就是設置窗體為隱藏。但是有一個缺點
隱藏后無法顯示。留待以后解決。利用C#枚舉所有的窗體就講到這里。
【編輯推薦】