MFC 下拉框下拉条目宽、高根据内容自动设置

有时候下拉框(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);

效果图:



posted on 2013-02-27 22:21 wrh 阅读(1300) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2010年7月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

常用链接

留言簿(19)

随笔档案

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜