浅谈如何利用C#枚举全部的窗体

 C#枚举全部的窗体的两种方法ide

一、直接查找游戏窗口,找到后做处理。ui

二、C#枚举全部窗口,列表显示,而后再处理。this

我这里按第二种方式作。首先是一些准备工做,如,了解如何调用系统API,见之前的博文。枚举窗口要用的一些spa

API:EnumWindows,GetWindowText,GetParent,IsWindowVisible.orm

EnumWindows:枚举窗口游戏

GetWindowText:取得窗口标题ci

GetParent:取得当前窗体的父窗体(很是重要,用于判断是否为顶级窗体)string

IsWindowVisible:判断窗体是否可见,用于过滤到不可见窗体。it

C#枚举代码以下:io

  
  
  
  
  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#枚举全部的窗体就讲到这里。

相关文章
相关标签/搜索