在我的 Treeview 上:左边是 EnsureVisible() 之前的 TreeView,右边是之后
该图标被忽略。我不知道如何在使用 EnsureVisible() 后显示图标,我会使用 EnsureVisible() 的替代方案,但我找不到任何手动滚动的方法。有没有? 也许一些带有 user32.dll 的 NativeMethods 或其他东西?
“左: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/