Python Win32API SendMessage win32con WM_SETTEXT only works once

813 views Asked by At

Simplified and working code below, but only works once then not again until the window is restarted. Is there some sort of finish set text missing or some other limitation? Can't find any results on google, Thanks

import win32api
import win32gui
import win32con

handle = windowName #Script is working with actual window name
mainWindowHWND = win32gui.FindWindow(None, handle)

win32api.SendMessage(mainWindowHWND, win32con.WM_SETTEXT, 0, "test")

1

There are 1 answers

1
Vinod KC On

You need to find the exact control handle in order to send the text. Now, you are changing the window title of that program. So Assume that you are setting a notepad window's title to 'test'. Then it become a window with title 'test'. So you can't get the window handle again with the old text. You need to enumerate the all the child windows of that specific window and check the type of control you are interested in. Then set the text of that control You can use EnumChildWindows api function for that. https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumchildwindows