views:

23

answers:

1

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.

A: 

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.

brian