Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a ComboBox capture mouse when it is dropped-down?

I am trying to model the behavior of a ComboBox dropdown (or other drop downs for that matter, including context menus) where the drop down closes when you click anywhere else, even on something that can't be focused.

I've tried subscribing for events such as MouseCaptureChanged, LostFocus, and Leave. I have a custom UserControl which is acting as a dropdown and I just want to close it up when the user clicks anywhere else.

This seems like something that's done in many controls so I'd be surprised if there wasn't a simple way to do it.

So far the overcomplicated methods I can come up with to do this are using pinvoke and the SetCapture() function, or to create a MessageFilter. If these are the only options I am not sure which is better.

like image 881
Trevor Elliott Avatar asked Feb 14 '12 16:02

Trevor Elliott


1 Answers

The ComboBox is constructed from 2 controls.

  • Base - visible when not active (Control)
  • DropDownList - visible during edit mode or list selection mode (Window or Form)

Normally the Base is visible. When the user clicks onto the ComboBox, the Base control hides and the DropDownList control shows up. This switch is done on the background, so for the user it seems the control just expanded.

The event you want to catch is done through the DropDownList Window. If you click somewhere onto your client area, the DropDownList Window receives the WM_KILLFOCUS event through it's WndProc(Message% m) method. Then sends to the parent window (the Base control) a WM_COMMAND (OCM_COMMAND) message with WParam=526318 (HIWORD(WParam)=8) and the Base control knows he should hide the DropDownList Window.

So, what you need to do is implement the additional DropDown Window and catch the WM_KILLFOCUS event.

like image 169
Zoltan Tirinda Avatar answered Nov 04 '22 13:11

Zoltan Tirinda