Settings
|
Sign in
|
Sign up
Bar
Post
Home
>
Bar Windows API
>
View post
Reply the post
|
View author only
There are currently
8
posts.
【教程】用三行C++代碼讓Win32程序擁有XP風格控件,無需manifest文件或24資源
Floor 1
巨大八爪鱼
12/5/15 14:34
首先,在Visual Studio 2012中,建立一個Win32空工程,再新建一個源文件,名叫Main.cpp:
【代碼】
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, L"Hello World!", L"Hello", MB_ICONWARNING);
return 0;
}
然後編譯運行。
運行結果:
Floor 2
巨大八爪鱼
12/5/15 14:38
可見目前該程序的控件沒有XP風格。
返回代碼編輯窗口。在文件包含部分加入#include <CommCtrl.h>。注意這個一定要放到#include <Windows.h>的下面,如果放反了的話,編譯時就會出現下面的錯誤:
Floor 3
巨大八爪鱼
12/5/15 14:39
在WinMain函數前,加入如下代碼:
#pragma comment(lib, "Comctl32.lib")
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
Floor 4
巨大八爪鱼
12/5/15 14:39
Floor 5
巨大八爪鱼
12/5/15 14:40
最後,編譯並運行程序。
可見程序已經擁有了XP風格控件:
Floor 6
巨大八爪鱼
12/5/15 14:41
本文參考資料:
http://www.bianceng.cn/Programming/cplus/201306/36676.htm
Floor 7
巨大八爪鱼
12/5/15 16:47
這是微軟官方對這個問題給出的解答:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb773175%28v=vs.85%29.aspx
翻譯:Comctl32.dll第六版無需下載,它是一個Windows自帶的庫文件。在manifest裏面聲明一下就能在程序中啟用。
Floor 8
巨大八爪鱼
5/24/16 16:10
【補充】
如果編譯的程序需要在XP系統下運行,最好在主函數裏調用一下InitCommonControls();,否則控件可能在XP下完全無法顯示,而Win7上卻正常。
例如:
詳見:
https://zh.arslanbar.net/post.php?t=24058
Content converter:
No conversion
Mainland
Taiwan
Hong Kong
Singapore
Reply the post
Content:
User:
You are currently anonymous.
Captcha:
Unclear? Try another one.