Python win32api SendMesage

trying to clarify win32api for me. And just made a simple example. Get a notepad window, move the mouse over the position and write a line. But that does not work. What is the problem? And will someone clarify the lParam parameter to me like this? What does it do? Which type? What does it look like? :) Thanks in advance.

import win32api, win32con, win32gui, win32ui, win32service, os, time def f_click(pycwnd): x=300 y=300 lParam = y <<15 | x pycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam); pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam); def get_whndl(): whndl = win32gui.FindWindowEx(0, 0, None, 'NB.txt - Notepad') return whndl def make_pycwnd(hwnd): PyCWnd = win32ui.CreateWindowFromHandle(hwnd) return PyCWnd def send_input_hax(pycwnd, msg): f_click(pycwnd) for c in msg: if c == "\n": pycwnd.SendMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) pycwnd.SendMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0) else: pycwnd.SendMessage(win32con.WM_CHAR, ord(c), 0) pycwnd.UpdateWindow() whndl = get_whndl() pycwnd = make_pycwnd(whndl) msg = "It works !\n" send_input_hax(pycwnd,msg) 
+4
source share
1 answer

There is another window in the main Notepad block, you need to send it your messages. You can see this β€œhidden” window using the Microsoft Spy ++ tool or you can get all child windows as follows:

 def callback(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd): hwnds[win32gui.GetClassName(hwnd)] = hwnd return True hwnds = {} win32gui.EnumChildWindows(whndl, callback, hwnds) 

The window we are looking for is named Edit, and it is the only available and visible child window for Notepad. Thus, your code will work as follows:

 import win32api, win32con, win32gui, win32ui, win32service, os, time def f_click(pycwnd): x=300 y=300 lParam = y <<15 | x pycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam); pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam); def get_whndl(): whndl = win32gui.FindWindowEx(0, 0, None, 'NB.txt - Notepad') return whndl def make_pycwnd(hwnd): PyCWnd = win32ui.CreateWindowFromHandle(hwnd) return PyCWnd def send_input_hax(pycwnd, msg): f_click(pycwnd) for c in msg: if c == "\n": pycwnd.SendMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) pycwnd.SendMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0) else: pycwnd.SendMessage(win32con.WM_CHAR, ord(c), 0) pycwnd.UpdateWindow() whndl = get_whndl() def callback(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd): hwnds[win32gui.GetClassName(hwnd)] = hwnd return True hwnds = {} win32gui.EnumChildWindows(whndl, callback, hwnds) whndl = hwnds['Edit'] pycwnd = make_pycwnd(whndl) msg = "It works !\n" send_input_hax(pycwnd,msg) 

lParam is an int, and you see a trick here that allows you to pass more than one value through a single argument. Let's say that we need to pass two digits to a function that takes only one argument. We can send them as a two-digit number and divide it inside the function. Similar operations of bitwise shift (<<) and bitwise or (|) can also be reversed in your case:

 >>> x = 300 >>> y = 300 >>> lParam = y << 15 | x >>> lParam & 0x7FFF # x 0: 300 >>> lParam >> 15 # y 1: 300 

Read more about bitwise operations in the Wikipedia and Python Wiki .

+7
source

All Articles