Partager via


CListBox::SetHorizontalExtent

Sets the width, in pixels, by which a list box can be scrolled horizontally.

void SetHorizontalExtent(
   int cxExtent 
);

Parameters

  • cxExtent
    Specifies the number of pixels by which the list box can be scrolled horizontally.

Remarks

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

// Find the longest string in the list box.
CString    str;
CSize      sz;
int        dx = 0;
TEXTMETRIC tm;
CDC*       pDC = m_myListBox.GetDC();
CFont*     pFont = m_myListBox.GetFont();

// Select the listbox font, save the old font
CFont* pOldFont = pDC->SelectObject(pFont);
// Get the text metrics for avg char width
pDC->GetTextMetrics(&tm); 

for (int i = 0; i < m_myListBox.GetCount(); i++)
{
   m_myListBox.GetText(i, str);
   sz = pDC->GetTextExtent(str);

   // Add the avg width to prevent clipping
   sz.cx += tm.tmAveCharWidth;

   if (sz.cx > dx)
      dx = sz.cx;
}
// Select the old font back into the DC
pDC->SelectObject(pOldFont);
m_myListBox.ReleaseDC(pDC);

// Set the horizontal extent so every character of all strings 
// can be scrolled to.
m_myListBox.SetHorizontalExtent(dx);

Requirements

Header: afxwin.h

See Also

Concepts

CListBox Class

CListBox Members

Hierarchy Chart

CListBox::GetHorizontalExtent

CListBox::SetColumnWidth

LB_SETHORIZONTALEXTENT