使用Window API,如何确保控件保留本机外观?
时间:2020-03-06 14:30:43 来源:igfitidea点击:
我创建的某些控件似乎默认使用旧的Windows 95主题,如何防止这种情况发生?这是一个不保留操作系统本机外观的按钮示例(我使用Vista作为开发环境):
HWND button = CreateWindowEx(NULL, L"BUTTON", L"OK", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 170, 340, 80, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
我使用Windows API的本机C ++,没有托管代码。
解决方案
我相信它与代码无关,但是我们需要设置一个适当的清单文件来获取主题控件。
这里一些信息:@ msdn.com和这里:@ blogs.msdn.com
我们可以在此处查看有无清单的应用程序之间的区别:heaventools.com
要将清单添加到应用程序,我们需要创建一个MyApp.manifest文件并将其添加到应用程序资源文件中:
//-- This define is normally part of the SDK but define it if this //-- is an older version of the SDK. #ifndef RT_MANIFEST #define RT_MANIFEST 24 #endif //-- Add the MyApp XP Manifest file CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "MyApp.manifest"
对于Visual Studio的较新版本,在项目设置中可以找到"清单工具"选项卡,并且在该选项卡上可以找到"其他清单文件"字段,也可以用来定义清单文件。
这是Win32应用程序的简单MyApp.manifest文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.1" processorArchitecture="X86" name="Microsoft.Windows.MyApp" type="win32" /> <description>MyApp</description> </assembly>
如果应用程序依赖于其他dll,则这些详细信息也可以添加到清单中,并且Windows将使用此信息来确保应用程序始终使用这些从属dll的正确版本。
例如,以下是公共控件和8.0 C版本运行时库的清单依赖项详细信息:
<dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="X86" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.VC80.CRT" version="8.0.50608.0" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b" /> </dependentAssembly>