解决EnableVisualStyles Bug

文章引用自:http://www.bestwaytech.cn/News_Detail.aspx?ID=22 

一位朋友碰到了一个WinForm的问题,在网上搜了一通,没找到能解决问题的方案,

正好我以前以碰到过,在这里把解决方案呈上,以便有遇到此问题的朋友能有帮助。

 

问题是这样的,当启用了虚拟样式后,设置好的TeeView 的图标就不见了,代码如下:

Application.EnableVisualStyles();

在Debug模式生成的程序没问题,但用Release模式生成则不见了。

 

 在以下地址有一个方法,但不一定能解决问题,

http://www.codeproject.com/KB/bugs/EnableVisualStylesBug.aspx?msg=984714

方法是在调用Application.EnableVisualStyles(); 后再加入代码:Application.DoEvents(); 网上有的朋友说有效,我当时也试过这个方法,没效,不知道是什么原因,VS2005出来时有这个Bug, VS2008出来时问题依旧,不知那位牛人能够出来指点一下。

 

闲话少说,不能因为不知道原因就不解决问题吧。

方法超简单,先把 treeView1.DrawMode 设为 TreeViewDrawMode.OwnerDrawAll,然后再响应一下DrawNode事件。

            treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);

事件方法是:

        void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {

            e.DrawDefault = true;

             if (e.Node.Bounds.X > 10 && this.treeView1.ImageList != null && this.treeView1.ImageList.Images.Count > e.Node.ImageIndex)
            {
                Image img = this.treeView1.ImageList.Images[e.Node.ImageIndex];
                if (img != null)
                {
                    e.Graphics.DrawImage(img, e.Node.Bounds.X - 19, e.Node.Bounds.Y);
                }
            }
        }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值