手动设置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);
复制代码

效果图如下:

 

posted @   unicornsir  阅读(533)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示