Application.EnableVisualStyles Bug

 

Introduction

Version 1.1 of the .NET framework introduced the method System.Windows.Forms.Application.EnableVisualStyles. Calling this method prior to the creation of any Forms or Controls, will cause Windows XP to apply a theme when rendering Windows Common Controls and many of the native .NET controls like Buttons and CheckBoxes.

Using .NET 1.0, MFC, WTL or VB6, it is necessary to include a manifest, either in the same directory as the executable, or compiled into it as a resource, in order to make use of Windows XP visual styles. While not overly difficult, providing a manifest for every executable one creates is tedious, and Visual Studio has very little tool support for helping you to do so.

The introduction of EnableVisualStyles to v1.1 of the framework is a nice addition because it allows WinForms applications to easily adopt the new look and feel of Windows XP styles.

The Bug

The problem is that there is bug in the implementation of EnableVisualStyles that interferes with Images stored in an ImageList component and Window Common Controls, like the TreeView or Toolbar classes. The effect is that if you call EnableVisualStyles, all of the images will disappear from your toolbars, treeviews and listviews.

To reproduce the bug:

  1. Create a WinForms application in VS.NET 2003
  2. Add a Toolbar and ImageList to Form1
  3. Add an image to the ImageList and a button to the Toolbar
  4. Assign the image to the button
  5. In the Main method add a call to Application.EnableVisualStyles just before the call to Application.Run

When you run the app on Windows XP, with a Visual Style active, there will be no image on the toolbar button.

Work Around

After some searching through Google groups I found some discussion of this issue and a work around that seems to work and hasn't caused any problems in my applications. (To read more about the work-around go here)

A call to Application.DoEvents just after EnableVisualStyles, seems to fix the problem. How or why, who knows. Most likely it causes some message that was sent via PostMessage to get flushed out to the correct place, before the creation of the first WinForms based window.

So the work around code looks like this:

 Form1()); 
}

As of yet, I haven't seen any ill effect from the work-around and it seems to always work.

 
 

转载于:https://www.cnblogs.com/noway586/archive/2006/02/15/1697359.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值