========================================================== ==================================
Title: reset the Combo Box drop-down Box width
Note:
Date: 2011.4.12
Name: Zhu minglei
========================================================== ====================
The Combo Box control is affected by the interface layout. Sometimes the width of the Combo Box control is insufficient to display the content of each line completely. This problem has recently been encountered in the project.
A simple solution:
1. Create a New CZyfComboBox class derived from CComboBox.
2 override "= CBN_DROPDOWN" message. There is an equal sign before CBN_DROPDOWN, which indicates a reflected message. The so-called reflected message is the message sent by the control to its parent window, but the parent window allows the control to process it in advance.
3. Complete the "= CBN_DROPDOWN" message processing function. In this function, reset the width of the drop-down box.
Void CZyfComboBox: OnCbnDropdown ()
{
// Reset the width of the drop-down box
Int nNumEntries = GetCount ();
Int nWidth = 0;
CString str;
CClientDC dc (this );
Int nSave = dc. SaveDC ();
Dc. SelectObject (GetFont ());
Int nScrollWidth =: GetSystemMetrics (SM_CXVSCROLL); // width of the vertical scroll bar
For (int I = 0; I <nNumEntries; I ++)
{
GetLBText (I, str );
Int nLength = dc. GetTextExtent (str). cx + nScrollWidth;
NWidth = max (nWidth, nLength );
}
NWidth + = dc. GetTextExtent (TEXT ("0"). cx;
Dc. RestoreDC (nSave );
SetDroppedWidth (nWidth );
}
4. Use CZyfComboBox.
CZyfComboBox m_Combo;
DDX_Control (pDX, IDC_COMBO1, m_Combo );
Effect: