On the form I hang up the component "Panel" I throw on the panel label.

To move the panel without a border, I throw the code in MouseDown :


 private void Panel1_MouseDown(object sender, MouseEventArgs e) { Panel1.Capture = false; var m = Message.Create(Handle, 0xa1, new IntPtr(2), IntPtr.Zero); WndProc(ref m); } 

So just the panel moves, and how to make the panel move even while holding the label?

  • Make a label a child of the panel - Vadim Prokopchuk

1 answer 1

There are two options.

  1. All child controls on this panel will subscribe to the event and likewise move the panel.

    Let's slightly remake the event handler so that one method can be used for all controls:

     private void Controls_MouseDown(object sender, MouseEventArgs e) { var control = (Control)sender; control.Capture = false; var m = Message.Create(panel1.Handle, 0xa1, new IntPtr(2), IntPtr.Zero); WndProc(ref m); } 

    We sign both the panel itself and all controls located on it to this event:

     panel1.MouseDown += Controls_MouseDown; label2.MouseDown += Controls_MouseDown; 

    Naturally, this can be done in the designer by selecting the desired event in the Properties window (Properties).

  1. Make Label (and all other controls used) on this panel transparent to clicks.

     public class HitTransparentLabel : Label { 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); } } 

    Add the code of this class to the project, compile. After that, a new component will appear in the Toolbox. We use it in the same way as a regular Label . Well, almost the same: do not forget that it is transparent to clicks.

    So that the control can be dragged with the mouse in designer mode, you need to add a check:

     if (m.Msg == WM_NCHITTEST && !DesignMode) 

    For manual creation, simply write new HitTransparentLabel instead of new Label .

  • 2nd how to call ?. And how to sign the event? - GooliveR
  • @ArteS - added the answer. - Alexander Petrov
  • It works as it should, thanks a lot) - GooliveR