Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GroupBox not have a MouseMove event?

The WinForms GroupBox control doesn't support MouseMove (or at least, not consistently), and I don't understand why.

Since it descends from Control, it does have a MouseMove event, but GroupBox explicitly reintroduces it with Browsable(false), so it's not shown in the Property Grid. You can hook the MouseMove event at runtime, and sometimes it works -- as long as FlatStyle is left at Standard. If the GroupBox's FlatStyle is set to System, then no MouseMove events are fired at all.

Reflector hasn't given me any clues. The GroupBox constructor doesn't seem to be setting any strange control styles, and GroupBox doesn't do anything silly like override MouseMove and fail to call base.

This also appears to be a WinForms-specific limitation, because Delphi group boxes support OnMouseMove just fine. Correction: the comparison to Delphi isn't valid. Delphi group boxes aren't actually standard BM_GROUPBOX controls; they're just painted to look like group boxes, without actually inheriting strange groupbox behaviors like this. So this may well be a limitation of the Windows groupbox control, though I haven't seen it documented anywhere.

Why does the WinForms GroupBox not support MouseMove?

like image 250
Joe White Avatar asked Mar 01 '23 02:03

Joe White


1 Answers

According to this thread, a standard Windows groupbox (i.e., a BUTTON control with BS_GROUPBOX style) appears to return HTTRANSPARENT in response to WM_NCHITTEST. Since the control claims to be transparent, Windows sends the mouse-move events to its parent window instead.

The thread confirms that, if you handle WM_NCHITTEST yourself and return HTCLIENT, then the groupbox will get mouse-move events. They're using MFC but it probably works for WinForms as well.

What's not clear is why Windows returns HTTRANSPARENT by default, but at least the problem has been independently confirmed.

like image 67
Joe White Avatar answered Mar 05 '23 16:03

Joe White