在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);