Requirements: Placed on the layer of a picture, to achieve the mouse can be selected, and implement drag-and-drop function.
Code implementation:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceworkflowpro{ Public Partial classForm1:form {Private intOffsetX =0; Private intOffsetY =0; PublicList<control> Panelcontrols =NewList<control>(); PublicDictionary<control, list<list<point>>> rules =NewDictionary<control, list<list<point>>>(); PublicForm1 () {InitializeComponent (); PictureBox PictureBox=NewPictureBox (); Picturebox.image= System.Drawing.Bitmap.FromFile (AppDomain.CurrentDomain.BaseDirectory +"\\201208241209467792.png"); Picturebox.height= the; Picturebox.width= the; Picturebox.mousedown+=MouseDown; Picturebox.mouseup+=mouseUp; Picturebox.mousemove+=MouseMove; Panelcontrols.add (PictureBox); OffsetX= This. Panel1. location.x; OffsetY= This. Panel1. LOCATION.Y; This. Panel1. Controls.Add (PictureBox); } PrivatePoint mousedownpoint =NewPoint (); PrivateControl Selectedcontrol =NewControl (); PrivateControl Movetocontrol =NewControl (); voidMouseDown (Objectsender, MouseEventArgs e) { This. Selectedcontrol = Sender asControl; if(E.button = =System.Windows.Forms.MouseButtons.Left) { This. Lblposition.text ="("+ e.x +","+ E.y +")|"; Mousedownpoint=e.location; } } voidMouseMove (Objectsender, MouseEventArgs e) { if(E.button = = System.Windows.Forms.MouseButtons.Left && This. Selectedcontrol! =NULL) { point point= This. PointToClient ( This. Selectedcontrol.pointtoscreen (NewPoint (E.x-mousedownpoint.x, E.y-( mousedownpoint.y))); This. selectedcontrol.location =Point ; This. SelectedControl.Location.Offset (OffsetX,-OffsetY); This. Lblposition.text ="| ("+ point. X +","+ point. Y +")"; } } voidMouseUp (Objectsender, MouseEventArgs e) { This. Selectedcontrol =NULL; } Private voidmovetowho (MouseEventArgs e) {foreach(Control controlinchpanelcontrols) { intX1 = control. Location.x +OffsetX; intY1 = control. LOCATION.Y +OffsetY; intx2 = x1 +control. Width; inty2 = y1 +control. Height; if(e.x > X1 && e.x < x2 && e.y > Y1 && e.y <y2) { This. Movetocontrol =control; } } } }}
C #: Drag Features