L
O
A
D
I
N
G

解决 Win32 系统控件都是老式的问题


在 Win32 编程时我们时常会遇到使用系统窗口类的情况。按理说我们使用的系统窗口类样式应该与我们正在使用的操作系统版本保持一致才是,但为什么往往都是老式的控件呢?

其实解决办法很简单!

将下列代码加入头文件顶部即可:

#ifdef _UNICODE
#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_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

简单来说,你看到的大部分 Win32 窗口方框都是由公共控件库(Common Control Library) Comctl32.dll 实现的窗口。要使应用程序能够显示出与操作系统版本匹配的视觉样式,必须使用ComCtl32.dll 版本 6.0 或更高版本;但在没有任何定义的默认情况下,应用程序使用 User32.dll 中定义的用户控件(User Controls)和 ComCtl32.dll 版本 5.0 中定义的公共控件(Common Controls)。如果要求指定应用程序使用 ComCtl32.dll 版本 6.0 或更高,则必须添加应用程序清单(manifest,如其他答主所述)或编译器指令(#pragma)使得使应用程序能够指定它需要的程序集(Assembly,一组由不同版本 dll 组成的对象列表)版本。

具体可参考官方文档 Enabling Visual Styles

ComCtl32.dll 版本 6.0 不可再发行(redistributable),也就是说只有当应用程序在对应的 Windows 版本上运行时,这些对应的样式效果可用,也就是说你不能在 Windows 11 上编译程序之后拿到 Windows XP 上运行,要求其用这种方法显示 Windows 11 的效果。关于不同操作系统内置的对应版本:[2]

VersionDistribution Platform
5.81Microsoft Internet Explorer 5.01
Microsoft Internet Explorer 5.5
Microsoft Internet Explorer 6
5.82Windows Server 2003
Windows Vista
Windows Server 2008
Windows 7
6.0Windows Server 2003
6.10Windows Vista
Windows Server 2008
Windows 7

声明

原文链接:https://www.zhihu.com/question/489914730


文章作者: SeaYJ
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明来源 SeaYJ !
评论
  目录