.Net WinForm 启用XP效果的问题

Risisi Reader 启用 XP 后界面还比较和谐。

在 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>

问题立刻解决。

.Net Framework 2.0 中没有具体测试,不过 2.0 中 WinForm 的界面都自动处理成了 Office 11 的效果,应该没有这个bug了吧。大约。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值