4

Simulate clicking a button in another window in C #

 2 years ago
source link: https://www.codesd.com/item/simulate-clicking-a-button-in-another-window-in-c.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Simulate clicking a button in another window in C #

advertisements

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? :)


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.)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK