在 Windows XP 及后续版本中, .Net Framework 1.0 和 1.1 下对 .Net WinForm 启用 XP Style,单纯的调用 Application.EnableVisualStyles(); 会有很多问题,尤其是 Toolbar 图标不能正确绘制的问题尤为明显。

针对这个 Framework 的 bug,一般有下面三种方法来解决:

  • Application.EnableVisualStyles() 执行后直接执行一次 Application.EnableVisualStyles() 如:
     /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main()
      {
       Application.EnableVisualStyles();
       Application.DoEvents();

       Application.Run(new Form());
    }
  • 启用一个线程来运行程序:
     /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main()
      {
      
    Application.EnableVisualStyles();
       new Thread(new ThreadStart(Runner)).Start();

    }
    static void Runner()
    {
      Application.Run(new Form());
    }
  • 启用 .manifest :在应用程序目录下启用 .manifest 文件。

Risisi Reader 开始用的是第一种,但运行过程中不时的会抛出系统异常,然后就直接推出程序。最开始没有意识到是启用XP效果带来的问题,跟了一下跟不出结果,索性查看了异常的详细信息,如下:

System.Runtime.InteropServices.SEHException: 外部组件发生异常。

   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

   at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

   at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

   at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

   ......

Google 了一下 System.Runtime.InteropServices.SEHException,结论是启用XP效果措施不当的问题。立即改为第三种,在应用程序目录下加了一个 Risisi.Reader.exe.manifest :

<?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="Risisi.Reader"
    type="win32" />
<description>Risisi.Reader</description>
<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="X86"
            publicKeyToken="6595b64144ccf1df"
            language="*" />
    </dependentAssembly>
</dependency>
</assembly>

问题立刻解决。


不过不要忘了给控件的属性:FlatStyle = System

.Net Framework 2.0 中没有具体测试,不过 2.0 中 WinForm 的界面都自动处理成了 Office 11 的效果,应该没有这个bug了吧。大约。 

转:http://blog.csdn.net/rambler8139/archive/2006/07/20/947867.aspx 
posted on 2007-03-21 16:35  Dragon-China  阅读(831)  评论(0编辑  收藏  举报