Mfc 计算带有图标的CComboxex的最大下拉宽度

Mfc 计算带有图标的CComboxex的最大下拉宽度,mfc,ccombobox,Mfc,Ccombobox,我已经看过这篇关于动态设置cmbobox宽度的文章 但是,我使用的是ccomboxex: 正如您在最后一个条目中看到的,它被裁剪了。因此,我想自动扩大下拉列表 它需要考虑到这样一个事实,即它们的左边也是一个图标的空间。所以这还不够好: BOOL CMyComboBox::OnCbnDropdown() { // Reset the dropped width CString str; CRect rect; int nWidth = 0; int n

我已经看过这篇关于动态设置
cmbobox
宽度的文章

但是,我使用的是
ccomboxex

正如您在最后一个条目中看到的,它被裁剪了。因此,我想自动扩大下拉列表

它需要考虑到这样一个事实,即它们的左边也是一个图标的空间。所以这还不够好:

BOOL CMyComboBox::OnCbnDropdown()
{ 
    // Reset the dropped width
    CString str;
    CRect rect;
    int nWidth  = 0;
    int nNumEntries = GetCount();;

    CClientDC dc(this);

    int nSave = dc.SaveDC();
    dc.SelectObject(GetFont());

    for (int i = 0; i < nNumEntries; i++)
    {
        GetLBText(i, str); 
        int nLength = dc.GetTextExtent(str).cx;
        if (nLength>nWidth)
            nWidth = nLength;
    }

    nWidth += 2*::GetSystemMetrics(SM_CXEDGE) + 4;

    // check if the current height is large enough for the items in the list
    GetDroppedControlRect(&rect);
    if (rect.Height() <= nNumEntries*GetItemHeight(0))
        nWidth +=::GetSystemMetrics(SM_CXVSCROLL);


    dc.RestoreDC(nSave);
    SetDroppedWidth(nWidth);

    return FALSE;
}
boolcmycombobox::OnCbnDropdown()
{ 
//重置下降的宽度
CString-str;
正确无误;
int nWidth=0;
int nNumEntries=GetCount();;
CClientDC dc(本);
int nSave=dc.SaveDC();
选择对象(GetFont());
对于(int i=0;inWidth)
nWidth=nLength;
}
nWidth+=2*::GetSystemMetrics(SM_CXEDGE)+4;
//检查当前高度是否足以容纳列表中的项目
GetDroppedControlRect(&rect);
如果(rect.Height()这起作用:

void CDatesComboBoxEx::OnCbnDropdown()
{
    CString str;
    CRect rect;
    int nWidth = 0, nImageWidth = 0;
    int nNumEntries = GetCount();

    if (nNumEntries > 0)
    {
        // Get the width of an image list entry
        auto pImageList = GetImageList();
        if (pImageList != nullptr && pImageList->GetImageCount() > 0)
        {
            IMAGEINFO sInfo;
            pImageList->GetImageInfo(0, &sInfo);
            nImageWidth = sInfo.rcImage.right - sInfo.rcImage.left;
        }

        CClientDC dc(this);

        int nSave = dc.SaveDC();
        dc.SelectObject(GetFont());

        for (int i = 0; i < nNumEntries; i++)
        {
            COMBOBOXEXITEM sItem;
            TCHAR szBuffer[_MAX_PATH] = _T("");
            sItem.mask = CBEIF_INDENT | CBEIF_TEXT;
            sItem.iItem = i;
            sItem.cchTextMax = _MAX_PATH;
            sItem.pszText = szBuffer;

            if (GetItem(&sItem))
            {
                int nLength = dc.GetTextExtent(szBuffer).cx + nImageWidth + (sItem.iIndent * 10);
                if (nLength > nWidth)
                    nWidth = nLength;
            }
        }

        nWidth += 2 * ::GetSystemMetrics(SM_CXEDGE) + 4;

        // check if the current height is large enough for the items in the list
        GetDroppedControlRect(&rect);
        if (rect.Height() <= nNumEntries * GetItemHeight(0))
            nWidth += ::GetSystemMetrics(SM_CXVSCROLL);

        dc.RestoreDC(nSave);
        SetDroppedWidth(nWidth);
    }
}
void CDatesComboBoxEx::OnCbnDropdown()
{
CString-str;
正确无误;
int nWidth=0,nImageWidth=0;
int nNumEntries=GetCount();
如果(nNumEntries>0)
{
//获取图像列表项的宽度
auto pImageList=GetImageList();
如果(pImageList!=nullptr&&pImageList->GetImageCount()>0)
{
IMAGEINFO-sInfo;
pImageList->GetImageInfo(0和sInfo);
nImageWidth=sInfo.rcImage.right-sInfo.rcImage.left;
}
CClientDC dc(本);
int nSave=dc.SaveDC();
选择对象(GetFont());
对于(int i=0;inWidth)
nWidth=nLength;
}
}
nWidth+=2*::GetSystemMetrics(SM_CXEDGE)+4;
//检查当前高度是否足以容纳列表中的项目
GetDroppedControlRect(&rect);

如果(rect.Height(),则还应考虑项目显示的缩进空格数。每个缩进等于10像素@sergiol这是一次性计算吗?我查看了链接,但不确定如何调整最终代码。我是否使用
GetItem
来获取缩进和文本?也许你可以用附录更新我的答案?我想知道同样的事情,目的完全相同:。但是
iIndent
表示左侧的间隔图标。它对按层次组织下拉列表中的项目很有用。@sergiol请参阅我的更新答案。