现在的位置: 首页 > 综合 > 正文

FotoVision学习手记(2)

2013年10月06日 ⁄ 综合 ⁄ 共 3590字 ⁄ 字号 评论关闭

默认情况下,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

抱歉!评论已关闭.