这里有一个install_font函数,可以将字体复制到系统字体文件夹,在当前会话中加载它,通知正在运行的程序,并更新注册表。它只依赖于Python的标准库,应该可以在python2和python3中使用。在
c类型定义import os
import shutil
import ctypes
from ctypes import wintypes
try:
import winreg
except ImportError:
import _winreg as winreg
user32 = ctypes.WinDLL('user32', use_last_error=True)
gdi32 = ctypes.WinDLL('gdi32', use_last_error=True)
FONTS_REG_PATH = r'Software\Microsoft\Windows NT\CurrentVersion\Fonts'
HWND_BROADCAST = 0xFFFF
SMTO_ABORTIFHUNG = 0x0002
WM_FONTCHANGE = 0x001D
GFRI_DESCRIPTION = 1
GFRI_ISTRUETYPE = 3
if not hasattr(wintypes, 'LPDWORD'):
wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD)
user32.SendMessageTimeoutW.restype = wintypes.LPVOID
user32.SendMessageTimeoutW.argtypes = (
wintypes.HWND, # hWnd
wintypes.UINT, # Msg
wintypes.LPVOID, # wParam
wintypes.LPVOID, # lParam
wintypes.UINT, # fuFlags
wintypes.UINT, # uTimeout
wintypes.LPVOID) # lpdwResult
gdi32.AddFontResourceW.argtypes = (
wintypes.LPCWSTR,) # lpszFilename
# http://www.undocprint.org/winspool/getfontresourceinfo
gdi32.GetFontResourceInfoW.argtypes = (
wintypes.LPCWSTR, # lpszFilename
wintypes.LPDWORD, # cbBuffer
wintypes.LPVOID, # lpBuffer
wintypes.DWORD) # dwQueryType
函数定义
^{pr2}$