views:

986

answers:

3

I'd like to close a dialog that pops up automatically, but I'm having some trouble getting it to work. My Win32 programming is a bit rusty after several years of limited usage.

I'm using FindWindowEx to get handles to the dialog and the button I want to click. I was under the impression that sending a WM_COMMAND to the dialog, with the button handle in the wParam parameter would do the trick.

Window window = Window.FindWindow("TSomeDialog", null);
Window cancelButton = Window.FindWindow("TButton", "Cancel", window);

Message message = Message.Create(window.HWnd, 0x0111, cancelButton.HWnd, IntPtr.Zero);
PostMessage(message);

public void PostMessage(Message message)
{
    // Win32 API import
    PostMessage(message.HWnd, message.Msg, message.WParam, message.LParam);
}

Window is a class that implements IWin32Window and wraps some Win32 API calls. I have inlined the constant for WM_COMMAND (0x111).

What am I doing wrong? :)

A: 

Why not just send a WM_SYSCOMMAND message with a SC_CLOSE parameter? That should close the window.

Vincent McNabb
A: 

Why not send a WM_CLOSE message instead ?

Thomas Levesque
I need to press a specific button in this case. It's a... special... dialog.
Thorarin
+2  A: 

Well, according to the documentation for WM_COMMAND, lParam should be the handle to the control's window (it looks like you're passing it in wParam).

wParam should have its high order word equal to BN_CLICKED and its low order word equal to the control's identifier.

(You can use GetWindowLong with GWL_ID to retrieve this, but presumably its IDCANCEL.)

Peter Ruderman
Thanks, I just found out myself that I should be using lParam instead. Never assume what other people write to be correct :(
Thorarin