我正在使用Visual Studio 2017更新15.3。我已经编译了以下代码,但获得了无效的字体句柄。返回HFONT句柄为0xffffffffffffffffff045875ca;
#include "stdafx.h"
#include "afxwin.h"
#include "Windows.h"
int main()
{
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // zero out structure
lf.lfHeight = 12; // request a 12-pixel-height font
_tcsncpy_s(lf.lfFaceName, LF_FACESIZE,
_T("Arial"), 7); // request a face name "Arial"
HFONT hFont = CreateFontIndirect(&lf);
return 0;
}
看来您正在使用MFC的CFont
对象,但是直接使用Windows API创建字体。调用全局函数CreateFontIndirect
不更改无关的CFont
实例font
,而是应该使用CFont::CreateFontIndirect
:
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // zero out structure
lf.lfHeight = 12; // request a 12-pixel-height font
_tcsncpy_s(lf.lfFaceName, LF_FACESIZE,
_T("Arial"), 7); // request a face name "Arial"
font.CreateFontIndirect(&lf);