asp.net C # PictureBox control drag and picture drag code

Source: Internet
Author: User

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;
       }

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.