给Visual C++ 2008的Win32项目XP界面化

来源:互联网 发布:是指对明文文件或数据 编辑:程序博客网 时间:2024/05/16 11:39

本文转自:http://fenying.blog.163.com/blog/static/1020559932009620112413235/

 

如何在 Visual C++ 2008 中实现程序XP界面化

对于Win32程序,VC2008并没有直接提供XP界面化,这也大大小小影响了程序的界面。

下面来看看如何把程序实现XP界面化。

网上资料都使用了InitCommonControls函数来实现XP界面。(后来经我发现在VC2008中没有这个必要。)

所以在程序中加入声明:

#include <commctrl.h> //Windows Common Controls 头文件
#pragma comment(lib, "comctl32.lib")


 

在WinMain里添加:InitCommonControls();
准备就绪,开始吧。


 

测试一:


 

用eXeScope打开程序的EXE程序,点“XP化”,提示“已经是XP化了”……

晕,我怎么不觉得已经是XP界面了?看来这办法行不通啊。

用Resource Hacker打开生成的EXE程序。

给Visual C++ 2008的Win32项目XP界面化。 - Fenying - Fenying

根据MSDN的说法,在24:1号资源里声明Microsoft.Windows.Common-Controls就可以实现XP界面了。

那么,把别的程序里的24:1资源复制过来,粘贴进去,点“编译脚本”。

给Visual C++ 2008的Win32项目XP界面化。 - Fenying - Fenying

保存,运行程序。错误。

Oh, No! What's this?

给Visual C++ 2008的Win32项目XP界面化。 - Fenying - Fenying

呵呵,这是很正常的。

对比修改前后的24:1资源,少了什么?

VC2008生成的21:1资源有一项调用:Microsoft.VC90.CRT。

而我们复制进去的没有这一项,只有:Microsoft.Windows.Common-Controls。

故而就出错了。


 

测试二:


 

再用Resource Hacker打开它,把24:1资源改成如下:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1"manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="asInvoker"uiAccess="false"></requestedExecutionLevel>
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32"name="Microsoft.Windows.Common-Controls"version="6.0.0.0"processorArchitecture="*"publicKeyToken="6595b64144ccf1df"language="*"></assemblyIdentity>
      <assemblyIdentity type="win32"name="Microsoft.VC90.CRT"version="9.0.21022.8"processorArchitecture="x86"publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
    </dependentAssembly>
  </dependency>
</assembly>


 

编辑,保存。

What happened?

给Visual C++ 2008的Win32项目XP界面化。 - Fenying - Fenying

Ah, Oh...

再次错误。

测试三:

再找找项目是怎么设置的……(这里假设用项目X)

找到这个文件:X\X\Release\X.exe.intermediate.manifest

它就是24:1资源的内容。

给Visual C++ 2008的Win32项目XP界面化。 - Fenying - Fenying

怎么修改它?鬼知道……

继续找,又一发现:“项目”菜单-X 属性-配置属性-链接器-清单文件。

里面是有关X.exe.intermediate.manifest文件的设置。

给Visual C++ 2008的Win32项目XP界面化。 - Fenying - Fenying

看看“附加清单依赖项”,空的,那么……开工。

试了好两次,终于知道改怎么改了,修改为:

type="win32"name="Microsoft.Windows.Common-Controls"version="6.0.0.0"processorArchitecture="*"publicKeyToken="6595b64144ccf1df"language="*"


 

注意这里不能使用双引号,只能使用半角单引号。

运行,清爽的XP风格界面。

给Visual C++ 2008的Win32项目XP界面化。 - Fenying - Fenying

好了,现在再看看它的24:1资源是如何实现的,这里直接查看X.exe.intermediate.manifest文件了。

给Visual C++ 2008的Win32项目XP界面化。 - Fenying - Fenying

彻底无语……

就栽在这份上……

总结:

最后总结下实现方法:

1、在程序中加入声明:

#include <commctrl.h> //Windows Common Controls 头文件
#pragma comment(lib, "comctl32.lib")


 

2、在WinMain里添加:InitCommonControls(); //声明(或者说是注册) Windows Common Controls 组件

3、把“项目菜单-XXX项目 属性-配置属性-链接器-清单文件-附加清单依赖项”改成:

type="win32"name="Microsoft.Windows.Common-Controls"version="6.0.0.0"processorArchitecture="*"publicKeyToken="6595b64144ccf1df"language="*"


 

完成。

=======================================

2009年7月20日更新

突然从MSDN里找到一个更简单的方法。

只要使用这么一句预编译指令即可。

#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

=======================================

同时如果想在程序运行过程中取消XP风格(单个控件、窗体)

//预编译
#include "uxtheme.h"
#pragma comment (lib, "uxtheme.lib")

//hWnd是指定窗口、控件的句柄。
SetWindowTheme(hWnd,L" ",L" ");

 

0 0
原创粉丝点击