我有一个代码
private const int WM_CLOSE = 16; private const int BN_CLICKED = 245; [DllImport("User32.dll")] public static extern Int32 FindWindow(String lpClassName, String lpWindowName); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); public void Click(string _btnTitle) { int hwnd = 0; IntPtr hwndChild = IntPtr.Zero; //Get a handle for the Calculator Application main window // foreach (Process p in Process.GetProcesses()) //{ hwnd = FindWindow(null, FrmTitle); if (hwnd != 0) { hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", _btnTitle); SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero); } }
我不能点击应用程序的MessageBox上的“是”
任何人有小费? TKS
你没有发送正确的信息。
尝试在SendMessage()的调用中使用BM_CLICK
(0x00F5)。 这应该工作,只要hwndChild
是按钮的窗口句柄,而不是容器对话框。
BN_CLICKED
不起作用,因为这是通知代码,而不是消息。