C#枚举所有的窗体的两种方法
2010-03-19 17:33
363 查看
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. }
47. }
有一个combobox和button,点击按钮,将所有窗口列举显示在下拉框。接下来的工作就是设置窗体为隐藏。但是有一个缺点
隐藏后无法显示。留待以后解决。利用C#枚举所有的窗体就讲到这里。
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. }
47. }
有一个combobox和button,点击按钮,将所有窗口列举显示在下拉框。接下来的工作就是设置窗体为隐藏。但是有一个缺点
隐藏后无法显示。留待以后解决。利用C#枚举所有的窗体就讲到这里。
相关文章推荐
- C#窗体间传值的两种方法
- c#中两种方法拖动没有边框的窗体
- C# 判断窗体是否打开 如果打开激活窗体 两种方法
- C#枚举所有的窗体
- c# winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值两种方法(2)
- c#枚举所有的窗体
- 浅谈如何利用C#枚举所有的窗体
- C# 系统应用之无标题窗体移动的两种方法
- C#递归遍历窗体所有textbox控件并设置textbox事件的方法
- C#窗体间传值的两种方法
- C#通过反射获取当前工程中所有窗体并打开的方法
- C#使控件随窗体变化而变化的两种方法
- C# 无边框窗体 移动 两种方法
- C#两种子父窗体传值方法
- C#中获得文件夹下所有文件的两种方法
- C#中窗体间数据传递的两种方法
- C#读写txt文件的两种方法介绍【申明:来源于网络】
- C#将DataTable数据导出到EXCEL的两种方法
- VS2010 (MFC)调用 C#生成DLL的两种方法
- C#窗体间通讯的几种处理方法