淺談如何利用C#枚舉所有的窗體

 C#枚舉所有的窗體的兩種方法

1、直接查找遊戲窗口,找到後作處理。

2、C#枚舉所有窗口,列表顯示,然後再處理。

我這裏按第二種方式做。首先是一些準備工作,如,瞭解如何調用系統API,見以前的博文。枚舉窗口要用的一些

API:EnumWindows,GetWindowText,GetParent,IsWindowVisible.

EnumWindows:枚舉窗口

GetWindowText:取得窗口標題

GetParent:取得當前窗體的父窗體(非常重要,用於判斷是否爲頂級窗體)

IsWindowVisible:判斷窗體是否可見,用於過濾到不可見窗體。

C#枚舉代碼如下:

  1. namespaceHideProcess  
  2. {  
  3. publicdelegateboolCallBack(inthwnd,inty);  
  4. publicpartialclassForm1:Form  
  5. {  
  6.  
  7.  
  8. [DllImport("user32.dll")]  
  9.  
  10. publicstaticexternintEnumWindows(CallBackx,inty);  
  11. [DllImport("user32")]  
  12. publicstaticexternintGetWindowText(inthwnd,StringBuilderlptrString,intnMaxCount);  
  13. [DllImport("user32")]  
  14. publicstaticexternintGetParent(inthwnd);  
  15. [DllImport("user32")]  
  16. publicstaticexternintIsWindowVisible(inthwnd);  
  17.  
  18. publicboolReport(inthwnd,intlParam)  
  19. {  
  20. intpHwnd;  
  21. pHwnd=GetParent(hwnd);  
  22.  
  23. if(pHwnd==0&&IsWindowVisible(hwnd)==1)  
  24. {  
  25. StringBuildersb=newStringBuilder(512);  
  26.  
  27. GetWindowText(hwnd,sb,sb.Capacity);  
  28. if(sb.Length>0)  
  29. {  
  30. this.comboBox1.Items.Add(sb.ToString());  
  31. }  
  32. }  
  33. returntrue;  
  34. }  
  35. publicForm1()  
  36. {  
  37. InitializeComponent();  
  38. }  
  39.  
  40. privatevoidbutton1_Click(objectsender,EventArgse)  
  41. {  
  42. Process[]ProcArray=Process.GetProcesses();  
  43. comboBox1.Items.Clear();  
  44. EnumWindows(this.Report,0);  
  45. }  
  46. }  

有一個combobox和button,點擊按鈕,將所有窗口列舉顯示在下拉框。接下來的工作就是設置窗體爲隱藏。但是有一個缺點

隱藏後無法顯示。留待以後解決。利用C#枚舉所有的窗體就講到這裏。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章