#if !defined(AFX_MYCOMBOBOX_H__5E3BB93E_0385_11D4_A2E3_0048543D92F7__INCLUDED_)
#define AFX_MYCOMBOBOX_H__5E3BB93E_0385_11D4_A2E3_0048543D92F7__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyComboBox.h : header file
///////////////////////////////////////////////////////////////////////////////
// CMyComboBox windowclass CMyComboBox : public CComboBox
{
// Construction
public:
CMyComboBox();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyComboBox)
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CMyComboBox();protected:
    void RecalcDropWidth(); // Generated message map functions
protected:
//{{AFX_MSG(CMyComboBox)
afx_msg void OnDropdown();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYCOMBOBOX_H__5E3BB93E_0385_11D4_A2E3_0048543D92F7__INCLUDED_)///.cpp// MyComboBox.cpp : implementation file
//#include "stdafx.h"
#include "ComboWidthDemo.h"
#include "MyComboBox.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMyComboBoxCMyComboBox::CMyComboBox()
{
}CMyComboBox::~CMyComboBox()
{
}/////////////////////////////////////////////////////////////////////////////
// Implementationvoid CMyComboBox::RecalcDropWidth()
{
    // Reset the dropped width
    int nNumEntries = GetCount();
    int nWidth = 0;
    CString str;    CClientDC dc(this);
    int nSave = dc.SaveDC();
    dc.SelectObject(GetFont());    int nScrollWidth = ::GetSystemMetrics(SM_CXVSCROLL);
    for (int i = 0; i < nNumEntries; i++)
    {
        GetLBText(i, str);
        int nLength = dc.GetTextExtent(str).cx + nScrollWidth;
        nWidth = max(nWidth, nLength);
    }
    
    // Add margin space to the calculations
    nWidth += dc.GetTextExtent("0").cx;    dc.RestoreDC(nSave);
    SetDroppedWidth(nWidth);
}BEGIN_MESSAGE_MAP(CMyComboBox, CComboBox)
//{{AFX_MSG_MAP(CMyComboBox)
ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyComboBox message handlersvoid CMyComboBox::OnDropdown() 
{
    RecalcDropWidth();
}