The idea is this. Have to have three variables.
Record x coordinates: int xpos;
Record y coordinates: int yPos;
Record whether the mouse is pressed: bool Moveflag;
The code is as follows |
Copy Code |
Record three variables in the PictureBox mouse press event. private void Picbox_mousedown (object sender, MouseEventArgs e) { Moveflag = true;//has been pressed. Xpos = e.x;//the current X coordinate. YPos = e.y;//The current Y-coordinate. } In the PictureBox Mouse press event. private void Picbox_mouseup (object sender, MouseEventArgs e) { Moveflag = false; } Mouse movement in PictureBox private void Picbox_mousemove (object sender, MouseEventArgs e) { if (Moveflag) { Picbox.left + = convert.toint16 (e.x-xpos);//set X coordinates. Picbox.top + = convert.toint16 (e.y-ypos);//Set Y-coordinate. } } |
This enables the control to move through the container
Drag the implementation code of the picture in PictureBox bool wselected = false;
The code is as follows |
Copy Code |
Point P = new Point (); private void Picturebox1_mousedown (object sender, MouseEventArgs e) { Picturebox1.cursor = Cursors.hand; When the mouse is pressed, change the mouse shape to a hand type Wselected = true; p.x = e.x; P.Y = e.y; } int DRIFTX = 0, drifty = 0; int mx = 0, my = 0; private void Picturebox1_mousemove (object sender, MouseEventArgs e) { if (wselected) { DRIFTX = p.x-e.x; Drifty = P.y-e.y; mx = MX-DRIFTX; my = My-drifty; Bitmap BM = new Bitmap (this.pictureBox1.Image); Graphics g = picturebox1.creategraphics (); G.clear (Picturebox1.backcolor); G.drawimage (BM, MX, my); p.x = e.x; P.Y = e.y; Bm. Dispose (); G.dispose (); } } private void Picturebox1_mouseup (object sender, MouseEventArgs e) & nbsp; { Picturebox1.cursor = Cursors.Default; When you release the mouse, the shape reverts to the arrow wselected = false; } |