2011-02-24 8 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; 
} 
関連する問題