首先添加鼠标滚轮事件
public Form1()
{
InitializeComponent();
//捕捉滚轮事件
this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pMouseWheel);
this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);
}
添加鼠标事件
UP 和Down主要切换是否要移动图片的标志位
wheel 做为更新的功能在上面实现
enter获得坐标?
//申明坐标和标志位
private bool BeginDraged = false;
private int xx, xy, xx1, xy1;
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Focus();
}
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta >= 0)
{
pictureBox1.Width = (int)(pictureBox1.Width * 1.1);//因为Widthh和Height都是int类型,所以要强制转换一下-_-||
pictureBox1.Height = (int)(pictureBox1.Height * 1.1);
}
else
{
pictureBox1.Width = (int)(pictureBox1.Width * 0.9);
pictureBox1.Height = (int)(pictureBox1.Height * 0.9);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (BeginDraged)
{
xx1 = pictureBox1.Left + e.X - xx; //当鼠标被按下的时候
xy1 = pictureBox1.Top + e.Y - xy; //标记控件的位置为控件当前的位置-鼠标在空间里的坐标+鼠标移动的距离
pictureBox1.Location = new Point(xx1, xy1);//使控件跟随光标移动
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
xx = e.X; xy = e.Y; //标记鼠标在控件里的坐标
BeginDraged = true;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
BeginDraged = false; //转换鼠标被按下的状态
}