3

问候,

我正在寻找一种方法来将树视图的宽度设置为最大值,或者设置为其中最长的树节点的大小,每当节点被折叠或打开时。

我试过使用客户端大小..但这似乎不起作用。是否有不同的方法来检查哪个节点最长并将 TreeView.Width 设置为该大小?

4

1 回答 1

3

在网上搜索了一些之后,我发现了这种方式:

private const int GWL_STYLE = -16;
private const int WS_VSCROLL = 0x00200000;
private const int WS_HSCROLL = 0x00100000;


[DllImport("user32.dll", ExactSpelling = false, CharSet = CharSet.Auto)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

和:

//tree = instance of a treeview
tree.AfterExpand += (s, ea) =>
{
    int style = GetWindowLong(tree.Handle, GWL_STYLE);
    while ((style & WS_HSCROLL) != 0)
    {
        tree.Width++;
        style = GetWindowLong(tree.Handle, GWL_STYLE);
    }
};

当然,您也可以在按钮上使用它!

于 2011-05-10T13:34:03.707 回答