FindWindow 用来根据类名和窗口名来得到窗口句柄的。但是这个函数不能查找子窗口,也不区分大小写。
如果要从一个窗口的子窗口中查找需要使用FindWindowEX。
函数功能:该函数获得一个窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数查找子窗口,从排在给定的子窗口后面的下一个子窗口开始。在查找时不区分大小写。
函数原型:HWND FindWindowEx(HWND hwndParent,HWND hwndChildAfter,LPCTSTR lpszClass,LPCTSTR lpszWindow);
参数:
C#中使用该函数首先导入命名空间: using System.Runtime.InteropServices;
然后写API引用部分的代码,放入 class 内部
//查找窗体 [DllImport(“User32.dll”, EntryPoint = “FindWindow”)] public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport(“User32.dll”, EntryPoint = “FindWindowEx”)] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
例如:查找qq2012窗口 private void button2_Click(object sender, EventArgs e) { IntPtr maindHwnd = FindWindow(null, “qq2012”); //获得QQ登陆框的句柄 if (maindHwnd != IntPtr.Zero) { MessageBox.Show(“找到了!”); } else { MessageBox.Show(“没有找到窗口”); } }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/170215.html原文链接:https://javaforall.cn