如何使用代码单击“是”按钮

时间:2013-12-10 16:18:45

标签: c# windows winforms user32

我有一个代码

 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

1 个答案:

答案 0 :(得分:1)

您没有发送正确的讯息。

在调用SendMessage()时尝试使用BM_CLICK(0x00F5)。如果hwndChild是按钮的窗口句柄而不是容器对话框,那应该有用。

BN_CLICKED无法正常工作,因为这是通知代码,而不是消息。