Advertisement
uwekeim

Bubble Mouse Move events from child controls up hierarchy

May 23rd, 2013
194
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public sealed class MouseEventBubbler
  2. {
  3.     // See http://blog.magerquark.de/bubble-mouse-move-events-from-child-controls-up-the-hierarchy-in-windows-forms
  4.  
  5.     private readonly Control _attachTo;
  6.  
  7.     public MouseEventBubbler(Control attachTo)
  8.     {
  9.         _attachTo = attachTo;
  10.  
  11.         _attachTo.MouseMove += _attachTo_MouseMove;
  12.  
  13.         _attachTo.ControlAdded += _attachTo_ControlAdded;
  14.         _attachTo.ControlRemoved += _attachTo_ControlRemoved;
  15.  
  16.         foreach (Control control in _attachTo.Controls)
  17.         {
  18.             AttachToControl(control);
  19.         }
  20.     }
  21.  
  22.     public void _attachTo_MouseMove(object sender, MouseEventArgs e)
  23.     {
  24.         OnMouseMove(e);
  25.     }
  26.  
  27.     public event MouseEventHandler MouseMove;
  28.  
  29.     private void _attachTo_ControlAdded(object sender, ControlEventArgs e)
  30.     {
  31.         AttachToControl(e.Control);
  32.     }
  33.  
  34.     private void _attachTo_ControlRemoved(object sender, ControlEventArgs e)
  35.     {
  36.         DetachFromControl(e.Control);
  37.     }
  38.  
  39.     private void AttachToControl(Control c)
  40.     {
  41.         c.MouseMove += Child_MouseMove;
  42.         c.ControlAdded += Child_ControlAdded;
  43.         c.ControlRemoved += Child_ControlRemoved;
  44.         AttachToChildren(c);
  45.     }
  46.  
  47.     private void AttachToChildren(Control parent)
  48.     {
  49.         foreach (Control child in parent.Controls)
  50.         {
  51.             AttachToControl(child);
  52.         }
  53.     }
  54.  
  55.     private void DetachFromControl(Control c)
  56.     {
  57.         DetachFromChildren(c);
  58.         c.MouseMove -= Child_MouseMove;
  59.         c.ControlAdded -= Child_ControlAdded;
  60.         c.ControlRemoved -= Child_ControlRemoved;
  61.     }
  62.  
  63.     private void DetachFromChildren(Control parent)
  64.     {
  65.         foreach (Control child in parent.Controls)
  66.         {
  67.             DetachFromControl(child);
  68.         }
  69.     }
  70.  
  71.     private void Child_ControlAdded(object sender, ControlEventArgs e)
  72.     {
  73.         AttachToControl(e.Control);
  74.     }
  75.  
  76.     private void Child_ControlRemoved(object sender, ControlEventArgs e)
  77.     {
  78.         DetachFromControl(e.Control);
  79.     }
  80.  
  81.     private void Child_MouseMove(object sender, MouseEventArgs e)
  82.     {
  83.         var pt = e.Location;
  84.         var child = (Control)sender;
  85.         do
  86.         {
  87.             pt.Offset(child.Left, child.Top);
  88.             child = child.Parent;
  89.         }
  90.         while (child != _attachTo);
  91.  
  92.         var newArgs = new MouseEventArgs(e.Button, e.Clicks, pt.X, pt.Y, e.Delta);
  93.         OnMouseMove(newArgs);
  94.     }
  95.  
  96.     private void OnMouseMove(MouseEventArgs newArgs)
  97.     {
  98.         var h = MouseMove;
  99.         if (h != null)
  100.         {
  101.             h(this, newArgs);
  102.         }
  103.     }
  104. }
Advertisement
RAW Paste Data Copied
Advertisement