如何获得字体写字板 whit python win32

How can get font wordpad whit python win32

如何找到当前使用 Python 和 win32gui 字体的写字板应用程序? 我能够找到 Windows 处理程序和 child 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)

在Spy++中可以看到,写字板中的控件是Rich Edit:

根据Unsupported Edit Control Functionality: EM_GETCHARFORMAT应该使用而不是WM_GETFONT

首先,你需要得到Rich Edit handle(直接用Spy++,或者WindowFromPoint, FindWindowEx, EnumChildWindows等等。但是你用的GetDesktopWindow会仅 return 桌面句柄 window,并且 SendMessage 收到 window 句柄而不是设备上下文句柄)

另外还需要注意的是,在另一个进程中发送EM_GETCHARFORMAT消息时,需要申请一块内存用于读写[=37]中的一个CHARFORMAT2结构=]进程与两个进程进行交互。

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