将编译环境从VC6迁移至VS2008,运行系统为Windows XP时调用以下代码:
- // Retrieves the message font info
- NONCLIENTMETRICS ncm;
- ncm.cbSize = sizeof(NONCLIENTMETRICS);
- SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
得到的结果为空。
原因分析:
用VS2008新建项目时,默认会在StdAfx.h文件中定义:
- #ifndef WINVER // 指定要求的最低平台是 Windows Vista。
- #define WINVER 0x0600 // 将此值更改为相应的值,以适用于 Windows 的其他版本。
- #endif
- #ifndef _WIN32_WINNT // 指定要求的最低平台是 Windows Vista。
- #define _WIN32_WINNT 0x0600 // 将此值更改为相应的值,以适用于 Windows 的其他版本。
- #endif
然后我们再看一下NONCLIENTMETRICS 这个结构体的定义:
- typedef struct tagNONCLIENTMETRICSA
- {
- UINT cbSize;
- int iBorderWidth;
- int iScrollWidth;
- int iScrollHeight;
- int iCaptionWidth;
- int iCaptionHeight;
- LOGFONTA lfCaptionFont;
- int iSmCaptionWidth;
- int iSmCaptionHeight;
- LOGFONTA lfSmCaptionFont;
- int iMenuWidth;
- int iMenuHeight;
- LOGFONTA lfMenuFont;
- LOGFONTA lfStatusFont;
- LOGFONTA lfMessageFont;
- #if(WINVER >= 0x0600)
- int iPaddedBorderWidth;
- #endif /* WINVER >= 0x0600 */
- }
- #ifdef UNICODE
- #define NONCLIENTMETRICSW NONCLIENTMETRICS
- #else
- #define NONCLIENTMETRICSA NONCLIENTMETRICS
- #endif
注意最后的:
- #if(WINVER >= 0x0600)
- int iPaddedBorderWidth;
- #endif /* WINVER >= 0x0600 */
这是新的系统新加的,在Windows XP 下这个参数是没有用的,但是我们的在StdAfx.h定义了我们的最低平台是Vista所以会出错。
解决方法:
一、修改我们的代码,将最后分配的字节大小减掉。
- // Retrieves the message font info
- NONCLIENTMETRICS ncm;
- ncm.cbSize = sizeof(NONCLIENTMETRICS) - sizeof(ncm.iPaddedBorderWidth);
- SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
二、修改中的宏为:
- #define WINVER 0x0500
- #define _WIN32_WINNT 0x0500