FotoVision学习手记(2)

默认情况下,Vs.net提供的控件即使在WinXp下运行,也不能获得XP风格的界面,因此,FotoVision里加入了一个Manifest文件。这是一个XML文件,这个文件明确指定了窗体控件使用的是版本为6.0Comctl32.dll。这个dll文件中包含了一些新的控件以及一些控件的新特性,它最大的好处是支持控件外观的改变。

 

一般将Manifest文件放在可执行文件目录下,命名方法为:<可执行文件名>.exe.manifest,因此我们可以在bin目录下看到FotoVision.exe.manifest。项目中就是通过manifest文件下的ThemeManifest.xmlThemeManifest.cs文件实现的。前者就是能够让应用程序具有XP效果的manifest文件,后者是一个工具类,用于提取ThemeManifest.xml的内容,并写入到可执行文件所在的目录下。因此,当程序第一次运行时,会在同一目录下生成FotoVision.exe.manifest

 

ThemeManifest.xml

<?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.0"

   processorArchitecture
="X86"
   name
="PhotoVision"
   type
="win32"
/>
<description>.NET control deployment tool</description>
<dependency>
   
<dependentAssembly>
     
<assemblyIdentity
       
type="win32"

       name
="Microsoft.Windows.Common-Controls"
       version
="6.0.0.0"
       processorArchitecture
="X86"
       publicKeyToken
="6595b64144ccf1df"
       language
="*"
     
/>
   
</dependentAssembly>
</dependency>
</assembly>


   对这个文件来说,在不同的应用程序中,只需要修改assemblyIdentity中的name的属性,其他都是一样的,因此可以直接复制过去使用。


ThemeManifest.cs

 

这是一个工具类,用于提取ThemeManifest.xml的内容,并写入到可执行文件所在的目录下,并且这也解决了当可执行文件拷贝到其他目录下运行时无法具有XP外观的问题。

    /// <summary>
    
/// ThemeManifest 的摘要说明。
    
/// </summary>

    public  sealed class ThemeManifest
    
{
        
private class Consts
        
{
            
public const String ResourceName = "PhotoVision.manifest.ThemeManifest.xml";
        }

        
private ThemeManifest()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
public static bool Create()
        
{
            String path 
= System.Windows.Forms.Application.ExecutablePath+".manifest";
            
if(File.Exists(path))
                
return false;
            
try
            
{
                System.Reflection.Assembly assem 
= System.Reflection.Assembly.GetExecutingAssembly();//获取当前执行的程序集
                System.IO.TextReader reader = new StreamReader(assem.GetManifestResourceStream(Consts.ResourceName));
                
                String xml 
= reader.ReadToEnd();
                reader.Close();

                StreamWriter writer 
= new StreamWriter(path);
                writer.Write(xml);
                writer.Close();

                
return true;

            }

            
catch(System.Exception ex)
            
{
                System.Diagnostics.Trace.WriteLine(ex.Message.ToString());
                
return false;
            }

        }

    }

 

并且在Mainform 中修改Main()函数如下:

 

        /// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            
bool owned;
            System.Threading.Mutex mutex 
= new Mutex(true,Application.ProductName+"manifest",out owned);
            
if(owned)
            
{
                
if(ThemeManifest.Create())
                
{//程序首次运行,生成manifest文件
                    System.Diagnostics.Process process = Process.Start(Application.ExecutablePath);
                
                    process.WaitForInputIdle();
                    Application.Exit();
                }

                
else    
                    Run();
//manifest文件已经存在,直接运行
                mutex.ReleaseMutex();//释放 Mutex 一次。
            }

            
else
            
{
                Run();
            }

        }

        
static void Run()
        
{
            
bool owned;
            Mutex mutex 
= new Mutex(true,Application.ProductName+"single",out owned);

            
if(owned)
            
{
                Application.Run(
new Form1());
                mutex.ReleaseMutex();
            }


        }


下图就是使用了XP风格的FotoVision系统运行后的情形:


2006100101.JPG

posted on   Phinecos(洞庭散人)  阅读(1055)  评论(2编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

统计

点击右上角即可分享
微信分享提示