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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值