设置Combo box控件的显示宽度

2011-01-01 wcdj

 

CComboBox::SetDroppedWidth
int SetDroppedWidth( UINT nWidth );
Call this function to set the minimum allowable width, in pixels, of the list box of a combo box.

Parameters
nWidth
The minimum allowable width of the list-box portion of the combo box, in pixels.
Return Value
If successful, the new width of the list box, otherwise CB_ERR.
Remarks
This function only applies to combo boxes with the CBS_DROPDOWN or CBS_DROPDOWNLIST style.
By default, the minimum allowable width of the drop-down list box is 0. When the list-box portion of the combo box is displayed, its width is the larger of the minimum allowable width or the combo box width.

SetDroppedWidth函数用于设定下拉列表框的最小宽度, 当下拉列表框中的数据很长不能完全显示时, 可以使用这个函数把列表框的宽度设置的大一些,同样GetDroppedWidth()是返回列表框的宽度。

在需要设置的地方使用下面函数修改combo控件的宽度。

// set width of combo control MySetDroppedWidth((CComboBox*)GetDlgItem(IDC_COMBO)); // set width of combo control void CTool4UDlg::MySetDroppedWidth(CComboBox * pmyComboBox) { /* pmyComboBox is the pointer to my combo box */ // Find the longest string in the combo box CString str; CSize sz; int dx = 0; TEXTMETRIC tm; CDC* pDC = pmyComboBox->GetDC(); CFont* pFont = pmyComboBox->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 < pmyComboBox->GetCount(); i++) { pmyComboBox->GetLBText(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); pmyComboBox->ReleaseDC(pDC); // Adjust the width for the vertical scroll bar and the left and right border dx += ::GetSystemMetrics(SM_CXVSCROLL) + 2*::GetSystemMetrics(SM_CXEDGE); // If the width of the list box is too small, adjust it so that every // item is completely visible if (pmyComboBox->GetDroppedWidth() < dx) { pmyComboBox->SetDroppedWidth(dx); ASSERT(pmyComboBox->GetDroppedWidth() == dx); } }

 

更多内容参考MSDN:
http://msdn.microsoft.com/en-us/library/a66b856s%28VS.80%29.aspx

 

 

你可能感兴趣的:(function,list,String,dropdown,scroll)