2011-02-24 74 views
1

Win32 TreeView控件沒有內置消息/宏來獲取其可滾動寬度,例如,如果想設置TreeView的寬度,所以它不需要有滾動條。獲取Win32 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; 
}