其他分享
首页 > 其他分享> > 手动设置Combox的下拉宽度

手动设置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);

效果图如下:

 

标签:sz,Combox,手动,height,item,宽度,pmyComboBox,str,pDC
来源: https://www.cnblogs.com/unicornsir/p/14931309.html