简体   繁体   English

重命名窗口

[英]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.我试图让多个窗口的 HWND 调用相同,我认为最简单的方法是重命名我找到的那个并再次搜索,但似乎不允许我按照我想要的方式重命名它。 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). FindWindow可以返回一个无效的句柄(例如,当没有找到与文本匹配的窗口时)。 That could be your issue.那可能是你的问题。

The win32gui module exposes win32gui.EnumWindows ( doc ) that iterates top level windows. win32gui 模块公开了迭代顶级窗口的win32gui.EnumWindows ( doc )。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM