1,先添加panel控件,panel上放置picturebox控件,同尺寸大小。
2,接下来就是代码缩放和拖动。
鼠标滚轮事件:
this.pictureBox1.MouseWheel +=new MouseEventHandler(PictureBox1_MouseWheel);//鼠标滚轮事件
private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
int height = this.pictureBox1.Size.Height;
int width = this.pictureBox1.Size.Width;
if (e.Delta > 0)
{
pictureBox1.Size = new System.Drawing.Size((width - 10), (height - 10));
}
else if (e.Delta < 0)
{
pictureBox1.Size = new System.Drawing.Size((width + 10), (height + 10));
}
}
鼠标按下事件:
bool isMouseUp;
int xPos = 0;
int yPos = 0;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isMouseUp = false;
if (e.Button == MouseButtons.Middle)
{
this.pictureBox1.Size = new System.Drawing.Size(640,480);
this.pictureBox1.Location = new System.Drawing.Point(2,2);
}
if (e.Button == MouseButtons.Left)
{
xPos = e.X;//当前x坐标.
yPos = e.Y;//当前y坐标.
}
}
鼠标移动事件:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseUp)
return;
if ((pictureBox1.Top + Convert.ToInt16(e.Y - yPos)) <= 0
&& (pictureBox1.Left + Convert.ToInt16(e.X - xPos)) <= 0
&& (pictureBox1.Right + Convert.ToInt16(e.X - xPos)) >= panel1.Width
&& (pictureBox1.Bottom + Convert.ToInt16(e.Y - yPos)) >= panel1.Height)
{
pictureBox1.Left += Convert.ToInt16(e.X - xPos);//设置x坐标.
pictureBox1.Top += Convert.ToInt16(e.Y - yPos);//设置y坐标.
}
}
mouseUp事件
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isMouseUp = true;
}
连续拖动会卡顿,实际使用一般是点动拖动