Let's say I've got a window for which I want to simulate a mouse click at a specific x, y coordinate. I already have the hwnd but I'm not sure how to construct the lParam. I've used SendMessage in the past to click on buttons, etc., but I knew their hwnds. Any help would be greatly appreciated. I also can't help but wonder if I'm going about this the right way. My end goal is clicking on a certain user on skype main window (for example). I used EnumChildWindows to find all the main window's children, but couldn't find the right one. So figured I would try to 'click' on it using coordinates.
+1
A:
You can use the low level windows api thanks to ctypes. See an example below (adapted from something i didn't test it but it should be ok)
import ctypes
MOUSEEVENTF_MOVE = 0x0001 # mouse move
MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
MOUSEEVENTF_MOVEABS = MOUSEEVENTF_MOVE + MOUSEEVENTF_ABSOLUTE
MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
MOUSEEVENTF_LEFTUP = 0x0004 # left button up
MOUSEEVENTF_CLICK = MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP
def click(x, y):
#move first
x = 65536L * x / ctypes.windll.user32.GetSystemMetrics(0) + 1
y = 65536L * y / ctypes.windll.user32.GetSystemMetrics(1) + 1
ctypes.windll.user32.mouse_event(MOUSEEVENTF_MOVEABS, x, y, 0, 0)
#then click
ctypes.windll.user32.mouse_event(MOUSEEVENTF_CLICK, 0, 0, 0, 0)
Update: I didn't test the code below but I think that it should help you to write something to get the child position. Then you can try to click at the correct position.
CHILD= None
def the_callback(child_hwnd, regex):
'''Pass to win32gui.EnumWindows() to check all the opened windows'''
if re.match(regex, win32gui.GetWindowText(child_hwnd)):
CHILD= child_hwnd
win32gui.EnumChildWindows(hwnd, the_callback, regex)
if CHILD:
(x_tl, y_tl, x_br, y_br) = win32gui.GetWindowRect(CHILD)
luc
2010-06-03 07:54:13
hi thanks for quick reply. btw lets say you have hwnd of main window and bunch of his children windows also with their hwnds ... do you know how could i look for some specific text content acrosss all those children? and identify the right one so i could click on this text (link)? ...
nabizan
2010-06-03 08:11:15
@nabizan: see my update. I hope it helps
luc
2010-06-03 12:03:20
@luc: hmmm correct me if im wrong but *.GetWindowText only returns title of the window.this is something new for me and i dont know how exactly is it working but i think that anytime you open a window of some complex app (eg photoshop) you get main hwnd and bunch of children windows (they dont have a title) so at first you have to find the one specific child window whitch operates for example with some random generated links and after that you could click it ... i dont know if its the right way im going blind here
nabizan
2010-06-03 14:40:39
GetWindowText returns the title. you can also use GetClassName to find the window. You may need a tool like spy++ to find the right method to locate your window.
luc
2010-06-03 16:07:19