如何使用python win32获取字体写字板

2024-04-25 18:03:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如何找到当前使用Python和Win32 GUI字体的Word Pad应用程序? 我能够找到Windows处理程序和子窗口 下面是一个示例应用程序

import win32gui,win32api,win32con,win32ui

hwnd = win32gui.GetDesktopWindow()
dc = win32gui.GetWindowDC(hwnd)
hfont = win32gui.SendMessage(dc, win32con.WM_GETFONT, 0,0)
fnt_spc = {}
fnt_n = win32ui.CreateFont(fnt_spc)
lf = win32gui.SelectObject(hfont,fnt_n.GetSafeHandle())
print(lf.lfFaceName)

Tags: 应用程序字体guidcwordwin32padlf
1条回答
网友
1楼 · 发布于 2024-04-25 18:03:32

正如您在Spy++中看到的,写字板中的控件是富编辑: enter image description here

根据Unsupported Edit Control Functionality,应该使用^{}而不是WM_GETFONT

首先,您需要获得丰富的编辑句柄(直接使用Spy++,或者WindowFromPointFindWindowExEnumChildWindows等等。但是GetDesktopWindow您使用的只是将句柄返回到桌面窗口,并且SendMessage接收窗口句柄,而不是设备上下文句柄)

此外,您还需要注意,当在另一个进程中发送EM_GETCHARFORMAT消息时,您需要请求一段内存,以便在窗口进程中读写^{}结构,以便与这两个进程交互

C++样本(删除错误检查):

#include <iostream>
#include <windows.h>
#include <Richedit.h>
int main(int argc, char** argv)
{
    HWND hwnd = (HWND)0x00090BF0;
    DWORD pid = 0;
    GetWindowThreadProcessId(hwnd,&pid);
    HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION| PROCESS_VM_READ | PROCESS_VM_WRITE,false, pid);//7784
    CHARFORMAT2 cp;
    cp.cbSize = sizeof(CHARFORMAT2);
    cp.dwMask = CFM_FACE;
    CHARFORMAT2* lf = (CHARFORMAT2*)VirtualAllocEx(hProcess,NULL,sizeof(CHARFORMAT2), MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
    BOOL ret = WriteProcessMemory(hProcess,lf,&cp, sizeof(CHARFORMAT2),NULL);
    //ZeroMemory(&lf,sizeof(lf));

    LRESULT lr = SendMessage(hwnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)lf);

    ret = ReadProcessMemory(hProcess, lf, &cp, sizeof(CHARFORMAT2), NULL);
    std::cout << cp.szFaceName << std::endl;
    VirtualFreeEx(hProcess,lf, 0, MEM_RELEASE);
    return 0;
}

结果:

Courier New

相关问题 更多 >