我有一个API窗口函数FindWindowEx的问题,实际上我获得了进程的MainWindow的句柄,但是当我试图用FindWindowEx检索其中一个按钮的句柄时,它并没有运行。我已经用spy++验证了窗口和它的按钮,一切都运行得很好,甚至我的程序返回的主窗口的句柄也与spy++的匹配。我已经测试了"Marshal.GetLastWin32Error()“返回的错误代码,我总是得到错误1008。我已经搜索了许多处理我的问题的旧帖子,但我没有找到任何解决方案。这是我的代码:
DllImport("user32.dll" , CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
// ....
IntPtr hwnd = proc.MainWindowHandle;
string str = proc.MainWindowTitle;
Console.WriteLine("Main window Title : " + str);
Console.WriteLine("Main window Handle : " + hwnd.ToString());
//Get a handle for the "suivant" button
IntPtr hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "suivant" );
int error = Marshal.GetLastWin32Error() ; 发布于 2012-03-17 00:28:22
正如Hans Passant先生所说,类名是不可预测的,因此解决方案是不在FindWindowEx函数中指定类名,以便在主窗口中获得我们可以使用的所有控件句柄:
do {
IntPtr hwndchild = FindWindowEx(hwndparent, hwndchild , null, null) ;
}while( hwndchild != IntPtr.Zero );我们可以在主窗口中找到"suivant“按钮的句柄:
IntPtr hwndchild = FindWindowEx(hwnd, hwndchild , null, "suivant") ;谢谢你的帮助。
https://stackoverflow.com/questions/9721779
复制相似问题