在codeproject上面看见一个颜色选择控件CColourPopup, 地址是
http://www.codeproject.com/Articles/713/A-color-picker-button
上面这段代码在VC 6.0 中工作正常,但是在VC2008 + XP系统里面 SystemParametersInfo 返回 0, 因为VS2008默认你是面向Vista开发的软件, GetLastError 返回的是 0
这里控件看到很多人再用, 但是在这个函数里面有点小问题
void CColourPopup::Initialise()
{//other code// Create the fontNONCLIENTMETRICS ncm;ncm.cbSize = sizeof(NONCLIENTMETRICS);VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));m_Font.CreateFontIndirect(&(ncm.lfMessageFont));//other code}
上面这段代码在VC 6.0 中工作正常,但是在VC2008 + XP系统里面 SystemParametersInfo 返回 0, 因为VS2008默认你是面向Vista开发的软件, GetLastError 返回的是 0
简单订正如下:
void CColourPopup::Initialise()
{//other code// Create the fontUINT size;NONCLIENTMETRICS ncm;ncm.cbSize = sizeof(NONCLIENTMETRICS);OSVERSIONINFO osv;osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);::GetVersionEx(&osv);if(osv.dwMajorVersion < 6) //Vista以下的Windowsncm.cbSize = sizeof(NONCLIENTMETRICS) - sizeof(ncm.iPaddedBorderWidth);size = ncm.cbSize;VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, size, &ncm, 0));m_Font.CreateFontIndirect(&(ncm.lfMessageFont));//other code}
实际是由于从 Vista 和 Windows Server 2008 开始 NONCLIENTMETRICS 在最后增加了iPaddedBorderWidth字段,如果你的程序打算同时支持 vista 或 XP ,Windows 2000, Windows Server 2003,那么应该先调用 GetVersionEx 检测Windows版本,然后决定是否需要减去 sizeof (ncms.iPaddedBorderWidth) ;
=====================================================================
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;