简体   繁体   中英

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

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_LBUTTON,
                     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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM