Two ways to drag a form or control

Source: Internet
Author: User

First Kind

Example of volume adjustment

#regionVolume controlPrivatePoint Mouse_offset; Private voidPicturebox7_mousedown (Objectsender, MouseEventArgs e) {Mouse_offset=NewPoint (-e.x,-e.y);//        }        Private voidPicturebox7_mousemove (Objectsender, MouseEventArgs e) {(Control) sender). Cursor= Cursors.arrow;//set mouse arrows when dragging            if(E.button = =mousebuttons.left) {point Mousepos=control.mouseposition; Mousepos.offset (Mouse_offset. X, Mouse_offset. Y);//Set OffsetMousepos.y =Pointtoscreen (picturebox7.location).                 Y Point Mouse_new=(Control) sender.                Parent.pointtoclient (Mousepos); if(Mouse_new.x <Panel1. location.x) {mouse_new.x=Panel1.                location.x; }                if(Mouse_new.x > Panel1. Width +Panel1. location.x) {mouse_new.x= Panel1. width+Panel1.                location.x; } ((Control) sender). Location=mouse_new; Panel2. Width= mouse_new.x-Panel1.            location.x; }        }        #endregion


The second Kind

namespace using System.Runtime.InteropServices;

Drag #region Form
[DllImport ("User32.dll")]
Public static extern bool ReleaseCapture ();
[DllImport ("User32.dll")]
Public static extern bool SendMessage (INTPTR hwnd, int wmsg, int wParam, int lParam);
// <summary>
// Pa_top Events
// </summary>
/// <param name= "Sender" ></param>
/// <param name= "E" ></param>
private void Pa_top_mousedown (object sender, MouseEventArgs e)
        {
releasecapture ();
SendMessage (this. Handle, 0x0112, 0xf012, 0);
        }
#endregion

Both forms and controls can be dragged in both ways

The second way is more suitable for dragging the form

Related Article

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.