2

My question is about combo boxes in Windows MFC applications.

The dropdown part of the combo box contains items composed of a bitmap and a string.

Sometimes, the strings are too long and I have to adjust the width of the dropdown part of the combo box using the CComboBox::SetDroppedWidth() method.

My problem is that when the combo box is near the right edge of the computer screen, the right part of the dropdown is hidden (see image_1 and image_2 below).

I would like it to behave like in Excel (see image_3 below) meaning I would like the dropdown list to be shifted accordingly so that all its items can be seen without being cropped.

How can this be achieved?

image_1: right part of the dropdown is NOT hidden right part of the dropdown is NOT hidden

image_2: near the computer right edge, the right part of the dropdown is hidden right part of the dropdown is hidden

image_3: Excel combo box Excel combo box

=================================================================

EDIT 1 handles are null

=================================================================

EDIT 2

Ok. I forgot to mention that m_cbXmodels is a CComboBoxEx object. This is why the handles are NULL. I could get the handles via GetComboBoxCtrl()...

Léa Massiot
  • 1,928
  • 6
  • 25
  • 43
  • 3
    Possible duplicate of [Forcing a combobox to "dropdown" above instead of below](https://stackoverflow.com/questions/36307412/forcing-a-combobox-to-dropdown-above-instead-of-below) – Sheng Jiang 蒋晟 Oct 20 '17 at 09:35

1 Answers1

3

Handle the CBN_DROPDOWN notification.

Get the handle for the list control with GetComboBoxInfo.

Now use MoveWindow to adjust the window as needed.

Getting the current screen size is available with MonitorFromWindow. See rcWork member in MONITORINFO. You just need to adjust the left and right coordinates.

EDIT: As you can read in the comments: My Approach with CBN_DROPDOWN is to early Thanks to zett42). It is not possible to resize the combo box list part here.
But it is possible to post a user defined message to the same window and to reposition the window than.

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • Thank you. When I call GetComboBoxInfo(), I notice that the handle for the list control is NULL (hwndCombo, hwndItem and hwndList are NULL). – Léa Massiot Oct 20 '17 at 15:17
  • You must preset the cbSize member! – xMRi Oct 20 '17 at 15:55
  • New problem: the dropdown list doesn't move. The code is: HWND hwndList = info.hwndList; ::MoveWindow(hwndList, 40, 40, 100, 100, 1); – Léa Massiot Oct 20 '17 at 16:58
  • 2
    @LéaMassiot From MSDN reference of `CBN_DROPDOWN`: _Sent when the list box of a combo box is *about* to be made visible._ - so `MoveWindow` propably comes to early. Try to do a `PostMessage(WM_APP + n)`, add a message map entry for `WM_APP + n` and do the `MoveWindow` in your handler of this message. This should schedule your `MoveWindow` after the combobox has done the default positioning of the listbox. – zett42 Oct 21 '17 at 13:17
  • It sounds to me that the combo box class should really be updated by default to do this anyway. – Andrew Truckle Oct 22 '17 at 19:47