C#枚举全部的窗体的两种方法ide
一、直接查找游戏窗口,找到后做处理。ui
二、C#枚举全部窗口,列表显示,而后再处理。this
我这里按第二种方式作。首先是一些准备工做,如,了解如何调用系统API,见之前的博文。枚举窗口要用的一些spa
API:EnumWindows,GetWindowText,GetParent,IsWindowVisible.orm
EnumWindows:枚举窗口游戏
GetWindowText:取得窗口标题ci
GetParent:取得当前窗体的父窗体(很是重要,用于判断是否为顶级窗体)string
IsWindowVisible:判断窗体是否可见,用于过滤到不可见窗体。it
C#枚举代码以下:io
- 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#枚举全部的窗体就讲到这里。