[Windows]模拟按键

Find the handle to the button that you want to click (by using FindWindowEx), and just send click message:

SendMessage(hButton, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(0, 0));
SendMessage(hButton, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(0, 0));
  •  It works but it's not an ellegant solution. Doesn't WinAPI support functions that would directly "click" one of the windows's buttons? And I had to use Spy++ to get the name of the Button which wasn't straight-forward. – AB. Apr 30 '13 at 9:13
  • @AronBoguta You can enumerate all windows using EnumChildWindows, until you find out target button handle. If WinAPI contained a function that would directly "click" on of the window's buttons, it would do exactly the same that we did. Also, WinAPI is treating buttons as (child) windows. – Nemanja Boric Apr 30 '13 at 9:22
  • thanks, I know already about the EnumChildWindows still I would expect more from WinAPI :) – AB. Apr 30 '13 at 12:38
  • @AronBoguta: Use a single BM_CLICK message instead of two WM_LBUTTON... messages: SendMessage(hButton, BM_CLICK, 0, 0); – Remy Lebeau Apr 30 '13 at 23:31

 

https://stackoverflow.com/questions/16295677/how-can-i-simulate-a-button-click-given-the-handle-to-the-buttons-window

 

=======================================================================================================================

 

using SendMessage to insert text into the edit buffer (which it sounds like you want):

HWND notepad = FindWindow(_T("Notepad"), NULL);
HWND edit = FindWindowEx(notepad, NULL, _T("Edit"), NULL);
SendMessage(edit, WM_SETTEXT, NULL, (LPARAM)_T("hello"));

if you need keycodes and arbitrary keystrokes, you can use SendInput() (available in 2k/xp and preferred), or keybd_event()` (which will end up calling SendInput in newer OSs) some examples here:

http://www.codeguru.com/forum/showthread.php?t=377393

there's also WM_SYSCOMMAND/WM_KEYDOWN/WM_KEYUP/WM_CHAR events for SendMessage which you might be interested in.

answered Jan 22 '10 at 0:03  jspcal

https://stackoverflow.com/questions/2113950/how-to-send-keystrokes-to-a-window

原文地址:https://www.cnblogs.com/liujx2019/p/13678636.html