Reset combo box drop-down box width

Source: Internet
Author: User

========================================================== ==================================

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.