首页 > 解决方案 > 如何知道窗口类名长度?

问题描述

我可以使用此功能获取窗口标题:

import ctypes
from ctypes import wintypes
from collections import namedtuple
user32 = ctypes.WinDLL('user32', use_last_error=True)

def get_title(hWnd, lParam):
    pid = wintypes.DWORD()
    length = user32.GetWindowTextLengthW(hWnd) + 1
    title = ctypes.create_unicode_buffer(length)
    user32.GetWindowTextW(hWnd, title, length)
    return title.value

我可以使用 user32.GetClassNameW 获取 ClassName,但哪个函数可以替代 GetWindowTextLengthW,它给出 ClassName 的长度?

标签: pythonctypes

解决方案


根据WNDCLASSEX的文档,类名最多为 256 个字符,因此您可以给它一个 257 个字符的缓冲区(根据GetClassName为终止符额外增加一个字符)。在处理这么小的缓冲区时,没有必要采取额外的步骤来节省几个字节来获得确切的缓冲区大小。


推荐阅读