重新設定Combo Box下拉框寬度

來源:互聯網
上載者:User

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

標題:重新設定Combo Box下拉框寬度

備忘:

日期:2011.4.12

姓名:朱銘雷

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

Combo Box控制項,受介面布局影響,有時其寬度不足以將每行內容都顯示完全。最近在項目中遇到了這種問題。

一種簡單的解決方案:

1 建立一個CZyfComboBox類派生自CComboBox。

2 override “=CBN_DROPDOWN”訊息。CBN_DROPDOWN前面有一個等號,代表這是一個reflected message。所謂reflected message是控制項發送給其父視窗的訊息,但父視窗允許控制項預先處理它。

 

3 完成“=CBN_DROPDOWN”的訊息處理函數,在該訊息處理函數中,重新設定下拉框的寬度。

void CZyfComboBox::OnCbnDropdown()

{

    // 重新設定下拉框的寬度

    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);

    }

    nWidth += dc.GetTextExtent(TEXT("0")).cx;

 

    dc.RestoreDC(nSave);

    SetDroppedWidth(nWidth);

}

4 使用CZyfComboBox。

CZyfComboBox m_Combo;

DDX_Control(pDX, IDC_COMBO1, m_Combo);

效果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.