浅谈如何利用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#枚举所有的窗体就讲到这里。

你可能感兴趣的:(C#)