开发者

python/win32: post a click event to a window?

开发者 https://www.devze.com 2023-01-09 19:09 出处:网络
I want to simulate a mouse click on a window, but I want to post the click event directly to the window (not by simulating a general mouse click using win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOW

I want to simulate a mouse click on a window, but I want to post the click event directly to the window (not by simulating a general mouse click using win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)). What's the proper way to do it? I've tried the following, but it doesn't seem to have an effect:

def MAKELONG(low, high):
    return low | (high << 16)
win32gui.PostMessage(window,
                     win32con.WM_LBUTTONDOWN,
                     win32con.MK_LBUTT开发者_如何学编程ON,
                     MAKELONG(21,42))
time.sleep(0.05)
win32gui.PostMessage(window,
                     win32con.WM_LBUTTONUP,
                     0,
                     MAKELONG(21,42))

window is the correct handle for the window. In this case I was trying to get the file menu to activate.


If window is the window that owns the menu, this won't work because WM_LBUTTONDOWN is for the window's client area, and the menu area is non-client. I haven't tested this, but you might try posting WM_NCLBUTTONDOWN instead, with a wParam of HTMENU, and mouse position in screen coordinates.

Another alternative would be to just use GetSubMenu and TrackPopupMenu. The only problem with this is if you want the user to then be able to navigate to the other sub-menus.

0

精彩评论

暂无评论...
验证码 换一张
取 消