Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass-through mouse events to parent control

Environment: .NET Framework 2.0, VS 2008.

I am trying to create a subclass of certain .NET controls (label, panel) that will pass through certain mouse events (MouseDown, MouseMove, MouseUp) to its parent control (or alternatively to the top-level form). I can do this by creating handlers for these events in instances of the standard controls, e.g.:

public class TheForm : Form {     private Label theLabel;      private void InitializeComponent()     {         theLabel = new Label();         theLabel.MouseDown += new MouseEventHandler(theLabel_MouseDown);     }      private void theLabel_MouseDown(object sender, MouseEventArgs e)     {         int xTrans = e.X + this.Location.X;         int yTrans = e.Y + this.Location.Y;         MouseEventArgs eTrans = new MouseEventArgs(e.Button, e.Clicks, xTrans, yTrans, e.Delta);         this.OnMouseDown(eTrans);     } } 

I cannot move the event handler into a subclass of the control, because the methods that raise the events in the parent control are protected and I don't have a qualifier for the parent control:

Cannot access protected member System.Windows.Forms.Control.OnMouseDown(System.Windows.Forms.MouseEventArgs) via a qualifier of type System.Windows.Forms.Control; the qualifier must be of type TheProject.NoCaptureLabel (or derived from it).

I am looking into overriding the WndProc method of the control in my sub-class, but hopefully someone can give me a cleaner solution.

like image 790
GentlemanCoder Avatar asked Feb 13 '09 18:02

GentlemanCoder


People also ask

How are mouse events triggered?

Every mouse move over an element triggers that event. click. Triggers after mousedown and then mouseup over the same element if the left mouse button was used.

What is a mouse event handler?

The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse). Common events using this interface include click , dblclick , mouseup , mousedown . MouseEvent derives from UIEvent , which in turn derives from Event .


2 Answers

Yes. After a lot of searching, I found the article "Floating Controls, tooltip-style", which uses WndProc to change the message from WM_NCHITTEST to HTTRANSPARENT, making the Control transparent to mouse events.

To achieve that, create a control inherited from Label and simply add the following code.

protected override void WndProc(ref Message m) {     const int WM_NCHITTEST = 0x0084;     const int HTTRANSPARENT = (-1);      if (m.Msg == WM_NCHITTEST)     {         m.Result = (IntPtr)HTTRANSPARENT;     }     else     {         base.WndProc(ref m);     } } 

I have tested this in Visual Studio 2010 with .NET Framework 4 Client Profile.

like image 93
akatran Avatar answered Oct 03 '22 08:10

akatran


You need to write a public/protected method in your base class which will raise the event for you. Then call this method from the derived class.

OR

Is this what you want?

public class MyLabel : Label {     protected override void OnMouseDown(MouseEventArgs e)     {         base.OnMouseDown(e);         //Do derived class stuff here     } } 
like image 30
Autodidact Avatar answered Oct 03 '22 07:10

Autodidact