简体   繁体   中英

Rename a window

Iam trying to get the HWND of multiple windows called the same, and i thought the easy way would be to rename the one i found and search again, but it seems iam not allowed to rename it the way i want. This is what i have tryed

import win32gui
import win32api
test = win32gui.FindWindow(0, "notepad")
win32gui.SetWindowText(test, "testname")

I have done this in my old project. may be this could help you. use this example as your reference

def UpdateControl_FromValue(self):
name_val = self.GetOptionValue(self.option_folder_name)
id_val = self.GetOptionValue()
self.in_setting_name = True
if id_val:
    self.SetOptionValue("", self.option_folder_name)
    opt_processors.FolderIDProcessor.UpdateControl_FromValue(self)
else:
    if name_val:
        win32gui.SetWindowText(self.GetControl(), name_val)
self.in_setting_name = False

FindWindow can return an invalid handle (eg. when no windows matching the text is found). That could be your issue.

The win32gui module exposes win32gui.EnumWindows ( doc ) that iterates top level windows. You can provide a custom callback that transform the window title.

In the following sample, I filter windows by title prefix, but you could implement a filter with regex, if that's more suitable for you.

import win32gui 

def f(hwnd, more):
    title = win32gui.GetWindowText(hwnd)
    # print(f"{hwnd} - {title}")
    prefix = 'notepad'
    if title.startswith(prefix):
        win32gui.SetWindowText(hwnd, title[len(prefix):])

win32gui.EnumWindows(f, None)

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