Programmers who often design graphical interfaces must know that controls can only be dragged at will when designing, and cannot be dragged during operation.
You will definitely ask: Can you drag controls at will during runtime? The answer is yes. Our project involves this. I have implemented it. It is actually very simple. I will show you it. Haha, please don’t laugh at me.
Let’s start with a simple step by step:
First, create a new Form1, drag a label1, and the text is: mouse drag control exercise. Next, register an event for it. Here we register two events label1_MouseUp and label1_MouseDown. Before implementing the code to register an event, you must first define a Point object (global).
private Point mouse_offset;
Used to record the mouse position so as to pan the mouse position.
Here is the code for label1_MouseDown:
private void label1_MouseDown(object sender, MouseEventArgs e) { mouse_offset = new Point(-, -); }
Isn't it very easy?
Here is the code for label1_MouseUp:
private void label1_MouseUp(object sender, MouseEventArgs e) { if ( == ) { Point mousePos = ; (mouse_offset.X, mouse_offset.Y); ((Control)sender).Location = ((Control)sender).(mousePos); } }
This is quite complicated, so it is necessary to introduce it.
Point mousePos = ;Define a Point that contains the coordinates of the mouse cursor relative to the upper left corner of the screen. At this time, the current mouse position is recorded, that is, the position where the control is placed last.
(mouse_offset.X, mouse_offset.Y);
It's a method,
(Int32,Int32)
Translates this Point to the specified amount.
((Control)sender).Location = ((Control)sender).(mousePos);
Everyone should be very clear about the sender in this, which is the label1 that triggers the event, ((Control)sender).Location The sentence is to specify the position of label1, ((Control)sender).Parent of course represents the current Form1. If Form1 is nested in another Form1, then there can be a.Parent, that is: ((Control)sender).,...and so on...
Method: Calculate the position of the specified screen point into the work area coordinates. The parameters are:
The screen coordinates to be converted Point.
The facts work has been completed, it’s very simple, I feel sweaty...C# creates controls that can be dragged at will
Next time, introduce how to dynamically add controls during runtime.C# create controls that can be dragged at will
The following is the code:
/////////////// Code
namespace Drag the form1 { partial class Form1 { /// <summary> /// Required designer variables. /// </summary> private components = null; /// <summary> /// Clean up all resources in use. /// </summary> /// <param name="dispose"> true if managed resources should be released; otherwise, false. </param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { (); } (disposing); } #region Code generated by Windows Form Designer /// <summary> /// Designer supports required methods - don't /// Use the code editor to modify the contents of this method. /// </summary> private void InitializeComponent() { this.label1 = new (); (); // // label1 // this. = true; this. = new (92, 86); this. = "label1"; this. = new (101, 12); this. = 0; this. = "Mouse drag control exercise"; this. += new (this.label1_MouseDown); this. += new (this.label1_MouseUp); // // Form1 // = new (6F, 12F); = ; = new (292, 266);
The above article C# can create any control dragging method is all the content I share with you. I hope you can give you a reference and I hope you can support me more.