winforms 树景闪烁?

piv4azn7  于 2022-11-17  发布在  其他
关注(0)|答案(3)|浏览(179)

我知道通过添加TreeView.BeginUpdate可以防止树视图闪烁,但是当我将它添加到我的项目中时,我的树视图的所有节点都消失了,有人能告诉我为什么会发生这种情况吗?这里是我使用TreeView.BeginUpdate和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);
mwngjboj

mwngjboj1#

开始/EndUpdate()方法并不是为了消除闪烁而设计的。在EndUpdate()中出现闪烁是不可避免的,它会重绘控件。它们是为了加速添加大量节点而设计的,这在默认情况下会很慢,因为每一项都会导致重绘。你把它们放在for循环中会使情况变得更糟,把它们移到循环外会立即得到改善。
这可能足以解决您的问题。但您可以做得更好,抑制闪烁需要双缓冲。.NET TreeView类重写DoubleBuffered属性并 * 隐藏 * 它。这是一个历史性的意外,本机Windows控件只在Windows XP和更高版本中支持双缓冲。.NET曾经支持Windows 2000和Windows 98。
现在,这已经不太重要了。您可以通过从TreeView派生自己的类来将其放回原处。向项目中添加一个新类,并粘贴下面所示的代码。编译。将新控件从工具箱顶部拖到窗体上,替换现有的TreeView。效果非常明显,尤其是在滚动时。

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);
}
hwamh0ep

hwamh0ep2#

如果你和我一样是新手,需要在www.example.com上找到答案vb.net这里是@Hans Passant的答案。我用过了,变化很大

Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)
    SendMessage(Me.Handle, TVM_SETEXTENDEDSTYLE, CType(TVS_EX_DOUBLEBUFFER, IntPtr), CType(TVS_EX_DOUBLEBUFFER, IntPtr))
    MyBase.OnHandleCreated(e)
End Sub

Private Const TVM_SETEXTENDEDSTYLE As Integer = &H1100 + 44
Private Const TVM_GETEXTENDEDSTYLE As Integer = &H1100 + 45
Private Const TVS_EX_DOUBLEBUFFER As Integer = &H4
<DllImport("user32.dll")>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
End Function
whlutmcx

whlutmcx3#

并不是绝对需要为此派生一个新类,您也可以编写一个静态类函数,将控件传递给它以启用双缓冲:

public static class Helper
{
    public static void EnableDoubleBuffering(Control control)
    {
         SendMessage(control.Handle, TVM_SETEXTENDEDSTYLE, (IntPtr)TVS_EX_DOUBLEBUFFER, (IntPtr)TVS_EX_DOUBLEBUFFER);
    }
        
    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);
}

相关问题