我需要在我的程序中临时加载一些字体.最好从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,FW_NORMAL,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,FontName); if FontHandle = 0 then RaiseLastOSError;