clicking message box button using api

As a little bit of background: I am using code to change the values of a setup project - using envDTE.DTE. This works fine, but as a result of one of the values I am changing, a message box is raised - on which I need to click the "OK" button - also programatically.

I have set a timer to run every second which checks to see if the messagebox is open and if it is it clicks the OK button.

This is the code I'm using:

[System.Runtime.InteropServices.DllImport("user32.dll")]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[System.Runtime.InteropServices.DllImport("user32.dll")]

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[System.Runtime.InteropServices.DllImport("user32.dll")]

public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

[System.Runtime.InteropServices.DllImport("user32.dll")]

public static extern IntPtr SetActiveWindow(IntPtr hWnd);

private const uint BM_CLICK = 245;

public static void Timer_Tick(object sender, EventArgs Args)

{

IntPtr messageHandle = FindWindow("#32770", "Microsoft Visual Studio");

if (messageHandle.ToInt32() != 0)

{

IntPtr buttonHandle = FindWindowEx(messageHandle, IntPtr.Zero, "Button", "&Yes");

if (buttonHandle.ToInt32() != 0)

{

IntPtr sawResult;

sawResult=SetActiveWindow(buttonHandle);

if (sawResult.ToInt32() != 0)

{

SendMessage(buttonHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);

} // if (sawResult.ToInt32() != 0)

} // if (buttonHandle.ToInt32() != 0)

} //if (messageHandle.ToInt32() != 0)

} //public static void Timer_Tick(object sender, EventArgs Args)

Sometimes this works fine, but sometimes it doesn't seem to be clicking the OK button.... Apparently BM_CLICK only works with the active window, so I tried using setActiveWindow to make sure that the messagebox was the active window, but this only works when the application itself is the active application. I think it's likely that because the program is running in code and not with a gui that it is not always the active application.

Any thoughts on how to get this to work?

thanks!

[2901 byte] By [CharlieWasAnAngel] at [2008-1-10]
# 1
Send WM_LBUTTONDOWN and WM_LBUTTONUP instead.
nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
what's the difference between wm_lbuttondown and wm_lbuttonup?
CharlieWasAnAngel at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
One is button down, other is button up. Seems obvious...
nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

sorry, meant to say between up/down and bm_click - isn't click just the up/down functions put together?

CharlieWasAnAngel at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
Not quite sure, never used it. The SDK documentation is pretty clear though:

"If the button is in a dialog box and the dialog box is not active, the BM_CLICK message might fail. To ensure success in this situation, call the SetActiveWindow function to activate the dialog box before sending the BM_CLICK message to the button."

How is WM_LBUTTONDOWN/UP working out?

nobugz at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms General...