Home > MFC, Windows API > Add horizontal scrollbar to a combo box

Add horizontal scrollbar to a combo box

CComboBox has a function called SetHorizontalExtent which doesn’t work. SDK equivalent is CB_SETHORIZONTALEXTENT which also doesn’t work. The reason for this bug is pretty lame, because WS_HSCROLL style for combo box is not set, which in turn the VS dialog editor does not provide :(. So a workaround is to open .rc file in a text editor and add WS_HSCROLL style manually for this combo.

This is how a combo box will look with a horizontal scrollbar…

Horizontal scrollbar in a combo box

Horizontal scrollbar in a combo box

Here is a sample code from MSDN which is adapted a bit for this purpose…

void CDialoTestDlg::AddHScroll( CComboBox& Cmb )
{
  // Find the longest string in the combo box.
  CString str;
  CSize sz;
  int dx = 0;

  TEXTMETRIC tm = { 0 };
  CDC* pDC = Cmb.GetDC();
  CFont* pFont = Cmb.GetFont();

  // Select the listbox font, save the old font
  CFont* pOldFont = pDC->SelectObject(pFont);

  // Get the text metrics for avg char width
  pDC->GetTextMetrics(&tm);

  for (int i = 0; i < Cmb.GetCount(); i++)
  {
    Cmb.GetLBText(i, str);
    sz = pDC->GetTextExtent(str);

    // Add the avg width to prevent clipping
    sz.cx += tm.tmAveCharWidth;

    if (sz.cx > dx)
      dx = sz.cx;
  }

  // Select the old font back into the DC
  pDC->SelectObject(pOldFont);
  Cmb.ReleaseDC(pDC);

  // Set the horizontal extent so every character of all strings can
  // be scrolled to.
  Cmb.SetHorizontalExtent(dx);
}// End AddHScroll

Setting horizontal extent is useful to prevent the combo’s drop down width from extending beyond screen limits. Note that there is another function called SetDroppedWidth which sets the drop down’s width but with no scrolling.

Advertisements
  1. Fabio Ceconello
    August 17, 2009 at 8:36 am

    Hi Nibu,

    I just found your article, I had the same problem some months ago and solved it in a similar way. But then I found another problem that’s driving me crazy, and I believe you probably also found. When you have a combobox with an horizontal scrollbar and a few items (let’s say 2), less than the maximum height for the dropdown list, you’ll notice that the vertical scrollbar appears always. That’s because when the horizontal scrollbar was added the dropdown list wasn’t resized, so its height is not enough to display all of the items. So let’s say you have 2 items, only one will be displayed and there’ll be a tiny vertical scrollbar on the right.
    I tried to resize the dropdown list window, the combobox window… to no avail. Now I was going to catch WM_SIZE messages, but if you have any hints…

    Thanks

    • Alexey Osipov
      October 29, 2009 at 6:55 pm

      Fabio, did you solve this? I’m interested in the solution too.

  2. May 28, 2009 at 9:01 pm

    Hi Nibu…

    Was hoping I’d see you at this year’s summit … maybe the next one… assuming I get re-awarded 🙂

    Great post. It’s amazing the horizontal extents still don’t work right. I have a post on that in my legacy menu section:

    http://www.wynapse.com/LegacyCode/Custom_CListBox.aspx

    I originally wrote the article in 1999 and haven’t recompiled since VS2005, but the problem I addressed was still there then.

    -Dave

    • May 29, 2009 at 10:47 am

      Hi Dave,

      This years summit was a big miss for me, poor planning from side. Hope if re-awarded I’ll there next time and see you then.

      Well I too didn’t check this with VS2005/2008. I still use VC6 for compiling most of my apps since my company uses VC6. We’re waiting for MS to deliver us something like VC6. 🙂

      I’ll have to test this behavior with latest compiler and SDK to see if this problem still exists. This post was in response to a user’s query in a forum. So I guess this problem is still there.

      -Nibu-

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: