1

I have seen this article on CodeProject for setting the width of a CComboBox dynamically.

However, I am using a CComboBoxEx:

DropDown

As you can see with the last entry it is cropped. So I would like to automatically widen the drop-down list.

It needs to take into account the fact that their is a space for an icon on the left too. So this won't be good enough:

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;
}

How do we factor in for the icon on the left?

Drop-down with icon

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    You should also take into account `COMBOBOXEXITEM::iIndent` — https://learn.microsoft.com/en-us/windows/desktop/api/commctrl/ns-commctrl-tagcomboboxexitemw — "The number of indent spaces to display for the item. Each indentation equals 10 pixels." – sergiol Dec 14 '18 at 16:59
  • @sergiol Is this a one-off calculation? I had a look at the link but I am not sure how to adjust my final code. Do I use `GetItem` instead to get the indent and text? Perhaps you can update my answer with an addendum? – Andrew Truckle Dec 14 '18 at 21:47
  • I wanted to know the same thing for the exact same purpose: https://stackoverflow.com/questions/52428007/cccomboboxex-spacing-between-image-and-text . But `iIndent` represents interval at the **LEFT** of the icon. It is useful for hierarchically organize items on the dropdown. – sergiol Dec 14 '18 at 23:09
  • @sergiol See my updated answer. – Andrew Truckle Dec 15 '18 at 12:20

1 Answers1

2

This is working:

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);
    }
}

Result:

Drop-down List

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164