win32 API建立tooltip的版本不匹配問題解決方案

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.