在visual studio 2005以上版本中使用API创建tootip,创建后发送TTM_ADDTOOL等消息会失败,原因是加载的commctrl dll版本不匹配,解决方法如下:
1 在stdafx.h文件中把 #define _WIN32_WINNT 0x0501 改为 #define _WIN32_WINNT 0x0500
2 在#include "commctrl.h" #pragma comment(lib, "comctl32.lib") 之前加上如下代码:
#if _WIN32_WINNT>0x0500
#if defined _M_IX86
#pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'/"")
#elif defined _M_IA64
#pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'/"")
#elif defined _M_X64
#pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'/"")
#else
#pragma comment(linker, "/manifestdependency:/"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'/"")
#endif
#endif
3 在填充TOOLINFO结构时改变cbSize的大小,如下:
TOOLINFO ti;
memset(&ti, 0, sizeof(TOOLINFO));
#if _WIN32_WINNT>0x0500
ti.cbSize = sizeof(TOOLINFO)-sizeof(void*);
#else
ti.cbSize = sizeof(TOOLINFO);
#endif
原因是因为_WIN32_WINNT 大于0x0500的时候,TOOLINFO结构体多了一个LPARAM lParam的定义,导致sizeof(TOOLINFO)和旧版本不匹配。
推荐使用第3种方法,因为前两种方法修改了整个commctrl dll版本号,导致整个系统用到的都是会较低版本的commctrl dll
示例代码如下:
HWND hWindow = ::CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL, WS_POPUP|TTS_NOPREFIX|TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, (HMENU)0, NULL, NULL);
TOOLINFO ti;
memset(&ti, 0, sizeof(TOOLINFO));
#if _WIN32_WINNT>0x0500
ti.cbSize = sizeof(TOOLINFO)-sizeof(void*);
#else
ti.cbSize = sizeof(TOOLINFO);
#endif
。。。//其他数据填充
::SendMessage(hWindow, TTM_ADDTOOL, 0, &ti);
在Visual Studio 2005以上版本使用API创建Tooltip时,遇到因commctrl.dll版本不匹配导致TTM_ADDTOOL消息失败的问题。解决方法包括:1) 修改stdafx.h中_WIN32_WINNT定义;2) 使用特定链接器指令;3) 动态调整TOOLINFO结构体的cbSize。推荐采用第三种方法,因为它不影响整个系统的commctrl.dll版本。
1万+





