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

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

如何做到这一点?

【问题讨论】:

    标签: winapi user-interface 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-12
      • 2011-09-18
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多