C # Implement a drag without a title bar form

Source: Internet
Author: User

    • The title bar area is encapsulated within the Panel container (recommended), then the panel's MouseDown event handler is written.

[C-sharp]View Plaincopyprint?
  1. [DllImport ("user32.dll", CharSet = CharSet.Unicode)]
  2. Public static extern IntPtr SendMessage (IntPtr hwnd, int wmsg, IntPtr wParam, IntPtr lParam);
  3. [DllImport ("user32.dll", CharSet = CharSet.Unicode)]
  4. Public static extern bool ReleaseCapture ();
  5. Private void Panelcaption_mousedown (object sender, MouseEventArgs e)
  6. {
  7. const int wm_nclbuttondown = 0x00A1;
  8. const int htcaption = 2;
  9. if (E.button = = MouseButtons.Left) //Press the left mouse button
  10. {
  11. ReleaseCapture ();
  12. SendMessage (This.    Handle, Wm_nclbuttondown, (INTPTR) htcaption, IntPtr.Zero); //Drag the form
  13. }
  14. }
[DllImport ("user32.dll", CharSet = charset.unicode)]public static extern IntPtr SendMessage (IntPtr hwnd, int wmsg, INTPTR WParam, IntPtr LParam); [DllImport ("user32.dll", CharSet = charset.unicode)]public static extern bool ReleaseCapture ();p rivate void Panelcaption_mousedown (object sender, MouseEventArgs e) {const int wm_nclbuttondown = 0x00A1; Const int htcaption = 2; if (E.button = = mousebuttons.left)//press the left mouse button {releasecapture (); SendMessage (this. Handle, Wm_nclbuttondown, (INTPTR) htcaption, IntPtr.Zero); Drag the form}}

    • The title bar area is placed directly on the form, overriding the form's onmousedown function.

[C-sharp]View Plaincopyprint?
  1. [DllImport ("user32.dll", CharSet = CharSet.Unicode)]
  2. Public static extern IntPtr SendMessage (IntPtr hwnd, int wmsg, IntPtr wParam, IntPtr lParam);
  3. [DllImport ("user32.dll", CharSet = CharSet.Unicode)]
  4. Public static extern bool ReleaseCapture ();
  5. Protected override void OnMouseDown (MouseEventArgs e)
  6. {
  7. const int wm_nclbuttondown = 0x00A1;
  8. const int htcaption = 2;
  9. base.  OnMouseDown (e);
  10. if (E.button = = MouseButtons.Left) //Press the left mouse button
  11. {
  12. ReleaseCapture (); //Release capture
  13. SendMessage (This.    Handle, Wm_nclbuttondown, (INTPTR) htcaption, IntPtr.Zero); //Drag the form
  14. }
  15. }

C # Implement a drag without a title bar form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.