我开始知道,通过添加 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);
Begin/EndUpdate() 方法是not旨在消除闪烁。 EndUpdate() 处出现闪烁是不可避免的,它会重新绘制控件。它们旨在加速添加大量节点,默认情况下这会很慢,因为每个项目都会导致重新绘制。你把它们放在 for 循环中,把它们移到外面以获得立即的改进,这让情况变得更糟。
这可能足以解决您的问题。但你可以做得更好,抑制闪烁需要双缓冲。 .NET TreeView 类重写 DoubleBuffered 属性并hides它。这是一个历史意外,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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)