How to Click "Yes" button in order application with code

1.9k views Asked by At

I have a code

 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);
        }
    }

I can't Click button "Yes" on MessageBox of application :(

Anyone got a tip? Tks

1

There are 1 answers

1
0xFF On BEST ANSWER

You aren't sending the correct message.

Try using BM_CLICK (0x00F5) in your call to SendMessage(). That should work provided that hwndChild is the window handle of the button, rather than the container dialog box.

BN_CLICKED doesn't work because that is a notification code, not a message.