C# TreeView.EnsureVisible() - 我还能如何滚动?

标签 c# treeview

在我的 Treeview 上:左边是 EnsureVisible() 之前的 TreeView,右边是之后

该图标被忽略。我不知道如何在使用 EnsureVisible() 后显示图标,我会使用 EnsureVisible() 的替代方案,但我找不到任何手动滚动的方法。有没有? 也许一些带有 user32.dll 的 NativeMethods 或其他东西?

enter image description here

“左:EnsureVisible 之前的 TreeView,右:之后”

最佳答案

您必须使用一些外部魔法:

using System.Runtime.InteropServices;
//..

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetScrollPos(IntPtr hWnd, int nBar);

[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

private const int SB_HORZ = 0x0;
private const int SB_VERT = 0x1;

 // bring your node into the display
someNode.EnsureVisible();

// now you can scroll back all the way to the left:
SetScrollPos(treeView1.Handle, SB_HORZ, 0, true);
// ..or just a few pixels:
int spos = GetScrollPos( treeView1.Handle, SB_HORZ);
SetScrollPos(treeView1.Handle, SB_HORZ, spos - 20, true);

或者您可以使用 SB_VERT 常量通过此函数完成整个滚动。不过,您必须计算所选节点的像素位置,这可能是一个痛苦..

如果您看到闪烁,您应该将滚动包装在 SuspendLayout()ResumeLayout() block 中。

关于C# TreeView.EnsureVisible() - 我还能如何滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25352834/

相关文章:

c# - 禁用或删除所有 Console#WriteLine 调用?

c# - WPF ComboBox BorderBrush 不会改变

c# - 如何在 MVVM Light WPF C# 中动态更改 TreeViewItem 选择上的 UserControl(没有在 View 中硬编码 TreeView Selection 事件)

c# - WPF:TreeView 或 TreeListView header 水平滚动问题

python - 打开(树) View 时的 Odoo 8 函数调用

c# - 在 Visual Studio 中编辑的 Unity 脚本不提供自动完成功能

c# - (276/304)*304 的小数舍入关闭

java - 以 Composite 作为内容的 TreeViewer

c# - .NET 解决方案 - 许多项目与一个项目

c# - WPF 拖动距离阈值