【发布时间】:2011-06-02 16:40:02
【问题描述】:
我在资源文件中定义了一个对话框。但是,它使用的是 Windows 95 风格的按钮等。如何为这些控件使用视觉主题(即 XP 及更高版本中添加的主题)?
【问题讨论】:
标签: c++ windows resources dialog
我在资源文件中定义了一个对话框。但是,它使用的是 Windows 95 风格的按钮等。如何为这些控件使用视觉主题(即 XP 及更高版本中添加的主题)?
【问题讨论】:
标签: c++ windows resources dialog
您需要将清单文件嵌入到可执行文件中,告诉 Windows 您想要启用主题的控件版本 (there's MSDN documentation specifically for this topic)。这确实是出于兼容性原因,因为some people really like to write programs that mess around with the internal data structures of other programs。
在 Visual C++ 中,最简单的方法可能是通过#pragma:
#pragma comment(linker,"/manifestdependency:\"" \
"type='win32' " \
"name='Microsoft.Windows.Common-Controls' " \
"version='6.0.0.0' " \
"processorArchitecture='*' " \
"publicKeyToken='6595b64144ccf1df' " \
"language='*'\"")
这会导致链接器将类似这样的内容添加到生成的清单文件中:
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*" />
</dependentAssembly>
</dependency>
您还需要调用InitCommonControlsEx()注册相应的控件类,否则对话框不会出现。
正如 Mark Ransom 在下面的 cmets 中提到的,Windows 2000 ignores theming manifests,所以这在 Windows 2000、Windows XP 和更高版本中应该仍然有效。此外,一些框架(如 MFC)定义了 #pragma 并为您执行初始化。
【讨论】:
#pragma 在 stdafx.h 中,InitCommonControlsEx 在 InitInstance 中。