手动设置Combox的下拉宽度

有时候下拉框(MFC标准叫组合框,CComboBox)中条目文本很多,超过了下拉框的宽度,如果不加设置的话,超过的部分文本将无法显示,查找MSDN,发现解决方法,代码如下

 

// The pointer to my combo box.
    extern CComboBox* pmyComboBox;

    // Set the height of every item so the item
    // is completely visible.
    CString str;
    CSize   sz;
    int     dx=0;
    CDC*    pDC = pmyComboBox->GetDC();
    for (int i=0;i < pmyComboBox->GetCount();i++)
    {
        pmyComboBox->GetLBText( i, str );
        sz = pDC->GetTextExtent(str);

        // Only want to set the item height if the current height
        // is not big enough.
        if (pmyComboBox->GetItemHeight(i) < sz.cy)
            pmyComboBox->SetItemHeight( i, sz.cy );

        // Only want to set the item width if the current width
        // is not big enough.
        if (pmyComboBox->GetDroppedWidth() < sz.cx)
        {
            pmyComboBox->SetDroppedWidth(sz.cx + 20);
        }
    }
    pmyComboBox->ReleaseDC(pDC);

效果图如下:

手动设置Combox的下拉宽度

 

上一篇:Cheatsheet: 2017 02.01 ~ 02.28


下一篇:前端移动开发之rem