关于如何在.net下使用XP Style界面

MSDN中关于这方面的介绍请看文章:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwxp/html/xptheming.asp

文中对如何实现XP Style的解释是:
If you want your application to use visual styles, you must add an application manifest that indicates that ** ComCtl32.dll version 6 ** should be used if it is available. Version 6 includes some new controls and new options for other controls, but the biggest change is support for changing the appearance of controls in a window.

要想使用XP Style,最简单的办法是创建一个.manifest文件。文件必须与目标程序在同一个文件夹(例如你的工程的\bin文件夹),并且名字必须是
MyApp.exe .manifest,其中红色部分是你的程序文件名。文件的内容如下:

1<assembly manifestversion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
2<assemblyidentity name="Microsoft.Winweb.  MyApp.exe  " processorarchitecture="X86" type="win32" version="1.0.0.0"></assemblyidentity>
3<description> Your application description here.  </description>
4<dependency>
5<dependentassembly>
6<assemblyidentity language="*" name="Microsoft.Windows.Common-Controls" processorarchitecture="X86" publickeytoken="6595b64144ccf1df" type="win32" version="6.0.0.0"></assemblyidentity>
7</dependentassembly>
8</dependency>
9</assembly>

其中红色部分是可替换部分。将 MyApp.exe 替换为目标程序文件名。你可以用记事本来建立这个文件。

然后在.net工程中,将窗体的Button、Label的FlatStyle属性设置为System,如下图:

这样,你建立的.net程序就可以呈现XP风格了。

这里就出现了一个问题:无论更改MyApp.exe还是 MyApp.exe .manifest的文件名,XP Sytle都会消失。因为:

The operating system will load the manifest from the file system first, and then check the resource section of the executable. The file system version takes precedence.

解决办法就是将manifest文件直接嵌入.exe文件中去。方法是这样的:

用Visual Studio.net打开.exe文件(这里是test_WMPSDK.exe),将会看到下面的东西:

我们给它添加一个资源:

在对话框中,我们找不到manifest类型。没关系,我们使用“导入”来导入:

注意文件类型是“所有文件”。添加完后,要求资源类型,我们写入RT_MANIFEST:

此时完成了资源添加:

还没完,我们还要将资源的ID从101改为1:

这样,保存.exe文件,运行一下看看(此时可以把.manifest文件删掉了):

即使更改.exe文件名,程序仍然可以呈现XP Style。但是还有一个问题。因为资源是后期加入的,所以一旦你的工程重新生成,XP Style又会消失。比较彻底的办法是将manifest文件加入工程的资源当中去。我正在为这一步苦恼中,希望哪位能帮帮我……

今天又发现一件有趣的事情。
参见 http://www.codearchive.com/rate.php?rid=1172&amp;cmnt=1&amp;go=1004
有人认为只要在Form_Load中创建manifest文件,就可以实现XP样式了,但是这是不可能的,因为manifest必须在程序运行之前就存在。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus