获取 Win32 TreeView 控件的宽度

Posted

技术标签:

【中文标题】获取 Win32 TreeView 控件的宽度【英文标题】:Getting the width of Win32 TreeView control 【发布时间】:2011-02-24 09:51:45 【问题描述】:

Win32 TreeView 控件没有内置消息/宏来获取其(可滚动)宽度,例如如果要设置 TreeView 的宽度,则不需要滚动条。

如何做到这一点?

【问题讨论】:

【参考方案1】:

这是一个执行此操作的 C 函数:

int TreeView_GetWidth(HWND hTreeWnd)

    SCROLLINFO scrollInfo;
    SCROLLBARINFO scrollBarInfo;

    scrollInfo.cbSize = sizeof(scrollInfo);
    scrollInfo.fMask = SIF_RANGE;

    scrollBarInfo.cbSize = sizeof(scrollBarInfo);

    // To find the whole (scrollable) width of the tree control,
    // we determine the range of the scrollbar.
    // Unfortunately when a scrollbar isn't needed (and is invisible),
    // its range isn't zero (but rather 0 to 100),
    // so we need to specifically ignore it then.
    if (GetScrollInfo(hTreeWnd, SB_HORZ, &scrollInfo) &&
        GetScrollBarInfo(hTreeWnd, OBJID_HSCROLL, &scrollBarInfo))
    
        // Only if the scrollbar is displayed
        if ((scrollBarInfo.rgstate[0] & STATE_SYSTEM_INVISIBLE) == 0)
        
            int scrollBarWidth = GetSystemMetrics(SM_CXVSCROLL);
            // This is a hardcoded value to accomodate some extra pixels.
            // If you can find a cleaner way to account for them (e.g. through
            // some extra calls to GetSystemMetrics), please do so.
            // (Maybe less than 10 is also enough.)
            const int extra = 10;

            return (scrollInfo.nMax - scrollInfo.nMin) + scrollBarWidth + extra;
        
    

    return 0;

【讨论】:

以上是关于获取 Win32 TreeView 控件的宽度的主要内容,如果未能解决你的问题,请参考以下文章

win32项目 树形控件TreeView的使用

[WTL/ATL]_[初级]_[TreeView控件如何显示ToolTip]

树视图中的 Win32 文本框

C# winform 编程 自定义combobx控件,将treeview控件嵌入combobox中

[ATL/WTL]_[初级]_[自定义多列TreeView]

防止拖放到当前控件之外(TreeView 中的 TreeNodes)