C#鼠标拖动图片picturebox

时间:2022-06-11 15:51:20
 
/// 设置鼠标单击的坐标,以及图片的坐标
///
int mouseX;
int mouseY;
int picX;
int picY;

///
/// 当鼠标单击时,给鼠标设定值。初始化。
///
///
///
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mouseX = Cursor.Position.X;
mouseY = Cursor.Position.Y;
picX = this.pictureBox1.Left;
picY = this.pictureBox1.Top;

//if (isMouseMoveEventAviable == false)
// //添加鼠标移动事件
// this.movablePic.MouseMove += this.movablePic_MouseMove;
}

///
/// 根据鼠标的移动的值,设置
///
///
///
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
int y = Cursor.Position.Y - mouseY + picY;
int x = Cursor.Position.X - mouseX + picX;
if (e.Button == MouseButtons.Left)
{
this.pictureBox1.Top = y;
this.pictureBox1.Left = x;
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
mouseX = 0;
mouseY = 0;
if (this.pictureBox1.Location.X < 0 )
{
this.pictureBox1.Left = 0;

}
if ( this.pictureBox1.Location.Y < 0)
{
this.pictureBox1.Top = 0;
}
if ((this.pictureBox1.Left + this.pictureBox1.Width) > this.ClientSize.Width )
{
this.pictureBox1.Left = this.ClientSize.Width - this.pictureBox1.Width;
}
if ((this.pictureBox1.Top + this.pictureBox1.Height) > this.ClientSize.Height)
{
this.pictureBox1.Top = this.ClientSize.Height - this.pictureBox1.Height;
}
}

private void Form1_Click(object sender, EventArgs e)
{
this.pictureBox1.Cursor = Cursors.SizeAll;
}


////////////////////////////////////////////////////////////////////////////

<summary>
/// 设置鼠标单击的坐标,以及图片的坐标
/// </summary>
int mouseX;
int mouseY;
int picX;
int picY;

/// <summary>
/// 当鼠标单击时,给鼠标设定值。初始化。
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
mouseX = Cursor.Position.X;
mouseY = Cursor.Position.Y;
picX = this.pictureBox1.Left;
picY = this.pictureBox1.Top;

//if (isMouseMoveEventAviable == false)
// //添加鼠标移动事件
// this.movablePic.MouseMove += this.movablePic_MouseMove;
}

/// <summary>
/// 根据鼠标的移动的值,设置
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
int y = Cursor.Position.Y - mouseY + picY;
int x = Cursor.Position.X - mouseX + picX;
if (e.Button == MouseButtons.Left)
{
this.pictureBox1.Top = y;
this.pictureBox1.Left = x;
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (this.pictureBox1.Left > this.Width || (this.pictureBox1.Left + pictureBox1.Width < 0))
{
this.pictureBox1.Left = picX;
this.pictureBox1.Top = picY;
}
if (this.pictureBox1.Top > this.Height || this.pictureBox1.Top + pictureBox1.Height < 0)
{
this.pictureBox1.Left = picX;
this.pictureBox1.Top = picY;
}
mouseX = 0;
mouseY = 0;

}
private void Form1_Load(object sender, EventArgs e)
{
this.pictureBox1.Cursor = Cursors.SizeAll;
}
////////////////////////////////////////////////////////////////////////////////////////////

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

Point offset = new Point(0, 0);

Point p;

Bitmap srcBitmap;

Panel Panel1;

public Form1()

{

InitializeComponent();

}

private void Panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

{

p = e.Location;

Panel1.Cursor = Cursors.Hand;

}

private void Panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)

{

if (e.Button == MouseButtons.Left)

{

offset.Offset(e.X - p.X, e.Y - p.Y);

this.MyReDrawTest(offset.X, offset.Y);

p = e.Location;

}

}

private void Panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

{

Panel1.Cursor = Cursors.Default;

}

private void Panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

{

this.MyReDrawTest(offset.X, offset.Y);

}

private void MyReDrawTest(int x, int y)

{

if (srcBitmap == null)

return;

BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;

BufferedGraphics myBuffer = currentContext.Allocate(this.Panel1.CreateGraphics(), this.Panel1.DisplayRectangle);

myBuffer.Graphics.Clear(this.Panel1.BackColor);

myBuffer.Graphics.DrawImage(srcBitmap, offset);

myBuffer.Render( this.Panel1 .CreateGraphics() );

myBuffer.Dispose();

}

private void Form1_Load(object sender, System.EventArgs e)

{

Panel1 = new Panel();

Panel1.Dock = DockStyle.Fill;

Panel1.MouseDown += Panel1_MouseDown;

Panel1.MouseMove += Panel1_MouseMove;

Panel1.MouseUp += Panel1_MouseUp;

Panel1.Paint += Panel1_Paint;

this.Controls.Add(Panel1);

OpenFileDialog cd = new OpenFileDialog();

cd.Filter = "bmp文件 (*.bmp)|*.bmp|jpg文件 (*.jpg)|*.jpg|gif文件 (*.gif)|*.gif|所有文件|*.*";

if (cd.ShowDialog() == DialogResult.OK)

{

srcBitmap = new Bitmap(cd.FileName);

offset = new Point(0, 0);

Panel1.Invalidate();

}

}

}

}