实际应用中,一段代码在Win7下使用正常,在XP下获取字体异常。
ZeroMemory ((PVOID) &m_lf,sizeof (LOGFONT));
NONCLIENTMETRICS nm;
nm.cbSize = sizeof (NONCLIENTMETRICS);
VERIFY (SystemParametersInfo(SPI_GETNONCLIENTMETRICS,nm.cbSize,&nm,0));
m_lf = nm.lfMenuFont;
m_lf.lfWeight = FW_NORMAL;
m_lf.lfHeight = -12;
m_fontMenu.CreateFontIndirect (&m_lf);
pFont = pDC->SelectObject (&m_fontMenu);
问题出在NONCLIENTMETRICS这个结构体上,头文件中的定义如下
typedef struct tagNONCLIENTMETRICSW
{
UINT cbSize;
int iBorderWidth;
int iScrollWidth;
int iScrollHeight;
int iCaptionWidth;
int iCaptionHeight;
LOGFONTW lfCaptionFont;
int iSmCaptionWidth;
int iSmCaptionHeight;
LOGFONTW lfSmCaptionFont;
int iMenuWidth;
int iMenuHeight;
LOGFONTW lfMenuFont;
LOGFONTW lfStatusFont;
LOGFONTW lfMessageFont;
#if(WINVER >= 0x0600)
int iPaddedBorderWidth;
#endif /* WINVER >= 0x0600 */
} NONCLIENTMETRICSW, *PNONCLIENTMETRICSW, FAR* LPNONCLIENTMETRICSW;
在Vista及以上的系统中结构体的成员发生了增加,为了确保在XP,Vista,Win7下均能正常,我们需要在targetver.h中或头文件的其他地方定义
#ifndef WINVER
#define WINVER 0x0500
#endif