解决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);
                }
            }
        }

  

  
原文地址:https://www.cnblogs.com/roy/p/1703379.html