我需要在程序中临时加载一些字体.优选地,来自dll资源文件.
这里有一个Delphi版本:
procedure LoadFontFromDll(const DllName, FontName: PWideChar); var DllHandle: HMODULE; ResHandle: HRSRC; ResSize, NbFontAdded: Cardinal; ResAddr: HGLOBAL; begin DllHandle := LoadLibrary(DllName); if DllHandle = 0 then RaiseLastOSError; ResHandle := FindResource(DllHandle, FontName, RT_FONT); if ResHandle = 0 then RaiseLastOSError; ResAddr := LoadResource(DllHandle, ResHandle); if ResAddr = 0 then RaiseLastOSError; ResSize := SizeOfResource(DllHandle, ResHandle); if ResSize = 0 then RaiseLastOSError; if 0 = AddFontMemResourceEx(Pointer(ResAddr), ResSize, nil, @NbFontAdded) then RaiseLastOSError; end;
使用方式如下:
var FontName: PChar; FontHandle: THandle; ... FontName := 'DEJAVUSANS'; LoadFontFromDll('Project1.dll' , FontName); FontHandle := CreateFont(0, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, FontName); if FontHandle = 0 then RaiseLastOSError;