List Box 控件,当用AddString加入的字符串宽度大于控件的宽度时,如何才能显示左右滚动条?我已选择了Style中的 Horizontal scroll,vertical scroll,No integral height.当用这些参数时,增加的字符串个数超出了控件高度时,上下滚动条会自动出现。

解决方案 »

  1.   

    CListBox::SetHorizontalExtent 
    void SetHorizontalExtent( int cxExtent );ResSets the width, in pixels, by which a list box can be scrolled horizontally. If the size of the list box is smaller than this value, the horizontal scroll bar will horizontally scroll items in the list box. If the list box is as large or larger than this value, the horizontal scroll bar is hidden. To respond to a call to SetHorizontalExtent, the list box must have been defined with the WS_HSCROLL style.  This member function is not useful for multicolumn list boxes. For multicolumn list boxes, call the SetColumnWidth member function.Example// The pointer to my list box.
    extern CListBox* pmyListBox;// Find the longest string in the list box.
    CString str;
    CSize   sz;
    int     dx=0;
    CDC*    pDC = pmyListBox->GetDC();
    for (int i=0;i < pmyListBox->GetCount();i++)
    {
       pmyListBox->GetText( i, str );
       sz = pDC->GetTextExtent(str);   if (sz.cx > dx)
          dx = sz.cx;
    }
    pmyListBox->ReleaseDC(pDC);// Set the horizontal extent so every character of all strings 
    // can be scrolled to.
    pmyListBox->SetHorizontalExtent(dx);