树形闪烁? [英] Treeview flickering?

查看:300
本文介绍了树形闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我才知道,通过增加TreeView.BeginUpdate将$树状的对$ pvent闪烁,但是当我在这添加到我的项目的所有节点我树状消失的,任何机构可以告诉我为什么会发生,这里是code段我曾经TreeView.BeginUpdate和TreeView.EndUpdate

 树节点treeNode的=新的TreeNode(视窗);
        treeView1.Nodes.Add(treeNode节点);
        //
        //第一个节点之后的另一个节点。
        //
        treeNode的=新的TreeNode(Linux的);
        treeView1.Nodes.Add(treeNode节点);
        //
        //创建两个子节点,并把它们放入数组。
        // ...添加第三个节点,并指定这些作为其子。
        //
        树节点节点2 =新的TreeNode(C#);
        树节点节点3 =新的TreeNode(VB.NET);
        树节点[]数组=新的TreeNode [] {节点2,节点3};
        //
        //最后一个节点。
        //
        treeNode的=新的TreeNode(点网皮尔斯,数组);
        treeView1.Nodes.Add(treeNode节点);


解决方案

开始/ EndUpdate()方法是的的旨在消除闪烁。获取闪烁在EndUpdate()是不可避免的,它重绘控制。它们被设计来加速增加了批量的节点,这将是默认慢,因为每一个项目导致重绘。你做了很多糟糕的通过将它们内部的for循环,外面移动他们立即得到改善。

这可能足以解决你的问题。但是你可以做的更好,SUP pressing闪烁需要双缓冲。在.NET的TreeView类重写了DoubleBuffered财产的隐藏的它。这是历史的偶然,本机Windows控件只支持在Windows XP和更高双缓冲。 .NET曾经支持Windows 2000和Windows 98。

这是不完全相关了这些天。你可以把它放回去从TreeView的派生自己的类。添加一个新类到您的项目并粘贴如下所示的code。编译。从工具箱的上方新的控制到窗体,替换现有的TreeView。其效果是非常明显的,特别是当滚动。

 使用系统;
使用System.Windows.Forms的;
使用System.Runtime.InteropServices;类BufferedTreeView:树视图{
    保护覆盖无效OnHandleCreated(EventArgs的发送){
       SendMessage函数(this.Handle,TVM_SETEXTENDEDSTYLE,(IntPtr的)TVS_EX_DOUBLEBUFFER,(IntPtr的)TVS_EX_DOUBLEBUFFER);
        base.OnHandleCreated(E);
    }
    // PInvoke的:
    私人const int的TVM_SETEXTENDEDSTYLE =到0x1100 + 44;
    私人const int的TVM_GETEXTENDEDSTYLE =到0x1100 + 45;
    私人const int的TVS_EX_DOUBLEBUFFER = 0x0004单元;
    函数[DllImport(user32.dll中)]
    私人静态外部的IntPtr SendMessage函数(IntPtr的的HWND,INT味精,IntPtr的WP,IntPtr的LP);
}

I came to know that by adding TreeView.BeginUpdate will prevent flickering of treeview, but when i added it in to my project all nodes of my treeview disappears, Can any body tell me why it happens, here is the code snippet where i used TreeView.BeginUpdate and TreeView.EndUpdate

  TreeNode treeNode = new TreeNode("Windows");
        treeView1.Nodes.Add(treeNode);
        //
        // Another node following the first node.
        //
        treeNode = new TreeNode("Linux");
        treeView1.Nodes.Add(treeNode);
        //
        // Create two child nodes and put them in an array.
        // ... Add the third node, and specify these as its children.
        //
        TreeNode node2 = new TreeNode("C#");
        TreeNode node3 = new TreeNode("VB.NET");
        TreeNode[] array = new TreeNode[] { node2, node3 };
        //
        // Final node.
        //
        treeNode = new TreeNode("Dot Net Perls", array);
        treeView1.Nodes.Add(treeNode);

解决方案

The Begin/EndUpdate() methods were not designed to eliminate flicker. Getting flicker at EndUpdate() is inevitable, it repaints the control. They were designed to speed-up adding a bulk of nodes, that will be slow by default since every single item causes a repaint. You made it a lot worse by putting them inside the for loop, move them outside for an immediate improvement.

That will probably be sufficient to solve your problem. But you can make it better, suppressing flicker requires double-buffering. The .NET TreeView class overrides the DoubleBuffered property and hides it. Which is a historical accident, the native Windows control only supports double buffering in Windows XP and later. .NET once supported Windows 2000 and Windows 98.

That's not exactly relevant anymore these days. You can put it back by deriving your own class from TreeView. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the existing TreeView. The effect is very noticeable, particularly when scrolling.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class BufferedTreeView : TreeView {
    protected override void OnHandleCreated(EventArgs e) {
       SendMessage(this.Handle, TVM_SETEXTENDEDSTYLE, (IntPtr)TVS_EX_DOUBLEBUFFER, (IntPtr)TVS_EX_DOUBLEBUFFER);
        base.OnHandleCreated(e);
    }
    // Pinvoke:
    private const int TVM_SETEXTENDEDSTYLE = 0x1100 + 44;
    private const int TVM_GETEXTENDEDSTYLE = 0x1100 + 45;
    private const int TVS_EX_DOUBLEBUFFER = 0x0004;
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}

这篇关于树形闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆