Click on 'OK' button of message box using WinAPI in C#
c#, messagebox, winapi
Solution
Finallu, this works for me. First click probably activates the window and second click clicks the button.
SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);
SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);
Problem
I am trying to click on 'OK' button on a message box of C# Windows Forms using WinAPI . Below is the code that I am working on. ``` private const int WM_CLOSE = 16; private const int BN_CLICKED = 245; [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); // this works hwnd = FindWindow(null, "Message"); if(hwnd!=0) SendMessage(hwnd, WM_CLOSE, 0, IntPtr.Zero); // this doesn't work. hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "ok"); SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero); ``` Though I get a value in `hwndChild`, it is not recognising `BN_CLICKED`. I am not sure what am I missing. any help? I am trying to close the message box button of another application and this is what I am doing. But, I m still missing something. ``` IntPtr hwndChild = IntPtr.Zero; hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero,' '"Button", "OK"); SendMessage((int)hwndChild, WM_COMMAND, (BN_CLICKED '<<16) | IDOK, hwndChild); ```