鼠标拖动和放缩,已经加载到picture控件的图片
时间: 2024-02-28 07:53:35 浏览: 88
如果您想实现鼠标拖动和放缩功能,可以参考以下代码实现:
```csharp
public partial class Form1 : Form
{
private bool isDragging = false;
private Point lastPoint;
private int currentScale = 100;
public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
lastPoint = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
int deltaX = e.Location.X - lastPoint.X;
int deltaY = e.Location.Y - lastPoint.Y;
pictureBox1.Location = new Point(pictureBox1.Location.X + deltaX, pictureBox1.Location.Y + deltaY);
lastPoint = e.Location;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = false;
}
}
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0 && currentScale < 500)
{
currentScale += 10;
}
else if (e.Delta < 0 && currentScale > 10)
{
currentScale -= 10;
}
pictureBox1.Width = pictureBox1.Image.Width * currentScale / 100;
pictureBox1.Height = pictureBox1.Image.Height * currentScale / 100;
}
}
```
这段代码实现了以下功能:
1. 鼠标左键拖动Picture控件移动图片位置;
2. 鼠标滚轮向上滚动放大图片,向下滚动缩小图片;
3. 放大缩小的倍数限制在10%到500%之间。
您可以将以上代码复制到Windows窗体应用程序的代码文件中,并将Picture控件的MouseDown、MouseMove、MouseUp和MouseWheel事件与对应的事件处理程序关联,然后加载一张图片到Picture控件中,就可以实现鼠标拖动和放缩已经加载到Picture控件的图片了。
阅读全文