我有一个自定义PictureBox
,可以使用放大MouseWheel
事件。 现在我想平移功能添加到它。 我的意思是,当图片框处于放大状态,如果用户离开点击并保持点击,然后移动鼠标时,图像会在PictureBox内平移。
这里是我的代码,但遗憾的是它不工作! 我不知道去哪里找了...
private Point _panStartingPoint = Point.Empty;
private bool _panIsActive;
private void CurveBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Focus();
_panIsActive = true;
_panStartingPoint = e.Location;
}
}
private void CurveBox_MouseUp(object sender, MouseEventArgs e)
{
_panIsActive = false;
}
private void CurveBox_MouseLeave(object sender, EventArgs e)
{
_panIsActive = false;
}
private void CurveBox_MouseMove(object sender, MouseEventArgs e)
{
if(_panIsActive && IsZoomed)
{
var g = CreateGraphics(); //Create graphics from PictureBox
var nx = _panStartingPoint.X + e.X;
var ny = _panStartingPoint.Y + e.Y;
var sourceRectangle = new Rectangle(nx, ny, Image.Width, Image.Height);
g.DrawImage(Image, nx, ny, sourceRectangle, GraphicsUnit.Pixel);
}
}
我怀疑的MouseMove
事件......我不知道,如果有什么事情发生在这个事件和/或nx
和ny
确实包含正确的点。
任何帮助/提示真的appriciated!