A group box is a static control that houses other controls inside it. It is designed purely to "group" things together to make the User Interface intuitive if laid out correctly. Hence there is very little events that you can use on behalf of the GroupBox.
You may be able to create a new class that is inherited from GroupBox and subclass it to intercept mouse move event. There is a very useful class that I have used before and it is really easy to perform the subclassing and to trigger the event for MouseMove.
Have a look at this here to see how the subclassing would work...Ok, it is written in VB.NET, but it is really easy to translate it into C# if you so wish, the code I would imagine would look like this:
Note: This code I have included is top of my head so there might be an error in this...but that's the gist of it.
Edit: In response to Joe White's comment, I have included the revised code and it does send WM_MOUSEMOVE...look at the steps below on how I reproduced this under VS 2008 Pro.
public class MyGroupBox : System.Windows.Forms.GroupBox
{
private SubClass sc;
private const int WM_MOUSEMOVE = 0x200;
public delegate void MyMouseMoveEventHandler(object sender, System.EventArgs e);
public event MyMouseMoveEventHandler MyMouseMove;
public MyGroupBox()
: base()
{
sc = new SubClass(this.Handle, true);
sc.SubClassedWndProc += new SubClass.SubClassWndProcEventHandler(sc_SubClassedWndProc);
}
protected override void Dispose(bool disposing)
{
if (sc.SubClassed)
{
sc.SubClassedWndProc -= new SubClass.SubClassWndProcEventHandler(sc_SubClassedWndProc);
sc.SubClassed = false;
}
base.Dispose(disposing);
}
private void OnMyMouseMove()
{
if (this.MyMouseMove != null) this.MyMouseMove(this, System.EventArgs.Empty);
}
void sc_SubClassedWndProc(ref Message m)
{
if (m.Msg == WM_MOUSEMOVE) this.OnMyMouseMove();
}
}
#region SubClass Classing Handler Class
public class SubClass : System.Windows.Forms.NativeWindow
{
public delegate void
SubClassWndProcEventHandler(ref System.Windows.Forms.Message m);
public event SubClassWndProcEventHandler SubClassedWndProc;
private bool IsSubClassed = false;
public SubClass(IntPtr Handle, bool _SubClass)
{
base.AssignHandle(Handle);
this.IsSubClassed = _SubClass;
}
public bool SubClassed
{
get { return this.IsSubClassed; }
set { this.IsSubClassed = value; }
}
protected override void WndProc(ref Message m)
{
if (this.IsSubClassed)
{
OnSubClassedWndProc(ref m);
}
base.WndProc(ref m);
}
#region HiWord Message Cracker
public int HiWord(int Number)
{
return ((Number >> 16) & 0xffff);
}
#endregion
#region LoWord Message Cracker
public int LoWord(int Number)
{
return (Number & 0xffff);
}
#endregion
#region MakeLong Message Cracker
public int MakeLong(int LoWord, int HiWord)
{
return (HiWord << 16) | (LoWord & 0xffff);
}
#endregion
#region MakeLParam Message Cracker
public IntPtr MakeLParam(int LoWord, int HiWord)
{
return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
}
#endregion
private void OnSubClassedWndProc(ref Message m)
{
if (SubClassedWndProc != null)
{
this.SubClassedWndProc(ref m);
}
}
}
#endregion
- Create a simple blank form.
- Drag a group box from the tools palette and drop it into the form, default name would be
groupBox1
- In your Form's designer code, change the code reference by doing this:
System.Windows.Forms.GroupBox groupBox1;
to WindowsApplication.MyGroupBox groupBox1;
- Within the
InitializeComponent()
method, change the instantiation of the GroupBox to this: this.groupBox1 = new WindowsApplication.MyGroupBox();
- Save and just compile it.
- Go back into your designer window and click on the group box, look for the
MyMouseMove
event within the properties toolbox, and wire it up.
- Your event handler should look something like this:
private void groupBox1_MyMouseMove(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("MyMouseMove!");
}
Run the application and everytime you move the mouse inside the groupbox you will see the output 'MyMouseMove!'.
Hope this gives you the hint,
Best regards,
Tom.