在鼠标移动时在面板内平移 PictureBox
Pan PictureBox inside a Panel on Mouse Move
我有一个 pictureBox1,它位于 Panel1 中,两者大小相同。 pictureBox1 在 MouseWheel 事件上调整大小,当 pictureBox1 尺寸大于 Panel1 尺寸时,用户可以在 Mouse_Move 事件上平移 PictureBox1(想要通过鼠标拖动而不是滚动条移动)。我已经编写了一个代码来防止用户平移过去 Panel1 边界。现在代码只能防止左上角和右下角。我的代码中的问题是当用户平移到右上角或左下角时 pictureBox1 仍然能够平移。但是,如果一次只平移一侧的任一侧,则 PictureBox1 将保留在 Panel1 内。
我尝试编辑我的代码,但无法获得合适的解决方案。如果有人能帮我解决我的代码中的这个问题,那将是一个很大的帮助。
下面的代码在pictureBox1_MouseMove
事件
下
左上角
右下角
右上角
左下角
if (pictureBox1.Width > panel1.Width || pictureBox1.Height > panel1.Height)
{
int count = 0; // Counter to check Top-Left points, if crossed panel's (0,0) points
// If count = 1, Set pictureBox point X or Y to 0.
// If count = 2, Set both the points of pictureBox to (0,0)
int count2 = 0; // Counter to check Bottom-Right points, if crossed Panels negative values calculated by panel1.Width-pictureBox1.Width
// If count2 = 1, Set pictureBox point X or Y to minPointX or minPointY .
// If count2 = 2, Set both the points of pictureBox to (minPointX, minPointY )
int minPointX = panel1.Width - pictureBox1.Width;
int minPointY = panel1.Height - pictureBox1.Height;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculation for Left Top corner.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if ((e.X - startPoint.X) >= 0 && pictureBox1.Location.X >= 0)
{
pictureBox1.Location = new Point(0, pictureBox1.Location.Y);
count++;
}
if((e.Y - startPoint.Y) >= 0 && pictureBox1.Location.Y >= 0)
{
pictureBox1.Location = new Point(pictureBox1.Location.X, 0);
count++;
}
if (count == 1)
{
if(pictureBox1.Location.X == 0)
pictureBox1.Location = new Point(0, pictureBox1.Location.Y + e.Y - startPoint.Y);
if( pictureBox1.Location.Y == 0)
pictureBox1.Location = new Point(pictureBox1.Location.X + e.X - startPoint.X, 0);
}
if (count == 2)
pictureBox1.Location = new Point(0, 0);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculation for Bottom Right corner.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if((e.X - startPoint.X) <= 0 && pictureBox1.Location.X <= minPointX)
{
pictureBox1.Location = new Point(minPointX, pictureBox1.Location.Y);
count2++;
}
if((e.Y - startPoint.Y) <= 0 && pictureBox1.Location.Y <= minPointY)
{
pictureBox1.Location = new Point(pictureBox1.Location.X, minPointY);
count2++;
}
if(count2 == 1)
{
if (pictureBox1.Location.X == minPointX)
pictureBox1.Location = new Point(minPointX, pictureBox1.Location.Y + e.Y - startPoint.Y);
if (pictureBox1.Location.Y == minPointY)
pictureBox1.Location = new Point(pictureBox1.Location.X + e.X - startPoint.X, minPointY);
}
if (count2 == 2)
pictureBox1.Location = new Point(minPointX, minPointY);
if (count == 0 && count2 == 0)
pictureBox1.Location = new Point(pictureBox1.Location.X + e.X - startPoint.X, pictureBox1.Location.Y + e.Y - startPoint.Y);
}
如果用户试图将图片框向右下移动,则当前代码会阻止用户将图片框移动到左上角的点 (0,0) 之外,如果用户试图将 pictureBox 移到右上角。
minPointX
和minPointY
分别是panel.Width
减去pictureBox.Width
和panel.Heigh
减去pictureBox.Height
。 minPointX 和 minPointY 是用户可以将 pictureBox 向负 x 和 y 轴移动到的最小点。
您可以使用面板 autoScroll
属性。确保面板内的 pictureBox
未锚定。然后将面板 autoScroll
属性 设置为 true
.
现在,当 pictureBox
变得比面板大时,它会自动显示滚动条。
现在在鼠标移动事件中设置 AutoScrollPosition
,如下面的代码所示。希望能帮助到你。
在下面的代码中,e 是 MouseEventArgs
.
panel1.AutoScrollPosition = new Point(-(panel1.AutoScrollPosition.X + e.X - startPoint.X),
-(panel1.AutoScrollPosition.Y + e.Y - startPoint.Y));
这是一个例程,其中包含一个留在视口内的控件。它假定两个尺寸都大于视口..
void constrain(Control ctl, Control view)
{
Rectangle pr = view.ClientRectangle;
Rectangle cr = ctl.ClientRectangle;
int x = Math.Min(0, Math.Max(ctl.Left, pr.Width - cr.Width));
int y = Math.Min(0, Math.Max(ctl.Top, pr.Height - cr.Height));
ctl.Location = new Point(x,y);
}
我有一个 pictureBox1,它位于 Panel1 中,两者大小相同。 pictureBox1 在 MouseWheel 事件上调整大小,当 pictureBox1 尺寸大于 Panel1 尺寸时,用户可以在 Mouse_Move 事件上平移 PictureBox1(想要通过鼠标拖动而不是滚动条移动)。我已经编写了一个代码来防止用户平移过去 Panel1 边界。现在代码只能防止左上角和右下角。我的代码中的问题是当用户平移到右上角或左下角时 pictureBox1 仍然能够平移。但是,如果一次只平移一侧的任一侧,则 PictureBox1 将保留在 Panel1 内。 我尝试编辑我的代码,但无法获得合适的解决方案。如果有人能帮我解决我的代码中的这个问题,那将是一个很大的帮助。
下面的代码在pictureBox1_MouseMove
事件
左上角
右下角
右上角
左下角
if (pictureBox1.Width > panel1.Width || pictureBox1.Height > panel1.Height)
{
int count = 0; // Counter to check Top-Left points, if crossed panel's (0,0) points
// If count = 1, Set pictureBox point X or Y to 0.
// If count = 2, Set both the points of pictureBox to (0,0)
int count2 = 0; // Counter to check Bottom-Right points, if crossed Panels negative values calculated by panel1.Width-pictureBox1.Width
// If count2 = 1, Set pictureBox point X or Y to minPointX or minPointY .
// If count2 = 2, Set both the points of pictureBox to (minPointX, minPointY )
int minPointX = panel1.Width - pictureBox1.Width;
int minPointY = panel1.Height - pictureBox1.Height;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculation for Left Top corner.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if ((e.X - startPoint.X) >= 0 && pictureBox1.Location.X >= 0)
{
pictureBox1.Location = new Point(0, pictureBox1.Location.Y);
count++;
}
if((e.Y - startPoint.Y) >= 0 && pictureBox1.Location.Y >= 0)
{
pictureBox1.Location = new Point(pictureBox1.Location.X, 0);
count++;
}
if (count == 1)
{
if(pictureBox1.Location.X == 0)
pictureBox1.Location = new Point(0, pictureBox1.Location.Y + e.Y - startPoint.Y);
if( pictureBox1.Location.Y == 0)
pictureBox1.Location = new Point(pictureBox1.Location.X + e.X - startPoint.X, 0);
}
if (count == 2)
pictureBox1.Location = new Point(0, 0);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculation for Bottom Right corner.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if((e.X - startPoint.X) <= 0 && pictureBox1.Location.X <= minPointX)
{
pictureBox1.Location = new Point(minPointX, pictureBox1.Location.Y);
count2++;
}
if((e.Y - startPoint.Y) <= 0 && pictureBox1.Location.Y <= minPointY)
{
pictureBox1.Location = new Point(pictureBox1.Location.X, minPointY);
count2++;
}
if(count2 == 1)
{
if (pictureBox1.Location.X == minPointX)
pictureBox1.Location = new Point(minPointX, pictureBox1.Location.Y + e.Y - startPoint.Y);
if (pictureBox1.Location.Y == minPointY)
pictureBox1.Location = new Point(pictureBox1.Location.X + e.X - startPoint.X, minPointY);
}
if (count2 == 2)
pictureBox1.Location = new Point(minPointX, minPointY);
if (count == 0 && count2 == 0)
pictureBox1.Location = new Point(pictureBox1.Location.X + e.X - startPoint.X, pictureBox1.Location.Y + e.Y - startPoint.Y);
}
如果用户试图将图片框向右下移动,则当前代码会阻止用户将图片框移动到左上角的点 (0,0) 之外,如果用户试图将 pictureBox 移到右上角。
minPointX
和minPointY
分别是panel.Width
减去pictureBox.Width
和panel.Heigh
减去pictureBox.Height
。 minPointX 和 minPointY 是用户可以将 pictureBox 向负 x 和 y 轴移动到的最小点。
您可以使用面板 autoScroll
属性。确保面板内的 pictureBox
未锚定。然后将面板 autoScroll
属性 设置为 true
.
现在,当 pictureBox
变得比面板大时,它会自动显示滚动条。
现在在鼠标移动事件中设置 AutoScrollPosition
,如下面的代码所示。希望能帮助到你。
在下面的代码中,e 是 MouseEventArgs
.
panel1.AutoScrollPosition = new Point(-(panel1.AutoScrollPosition.X + e.X - startPoint.X),
-(panel1.AutoScrollPosition.Y + e.Y - startPoint.Y));
这是一个例程,其中包含一个留在视口内的控件。它假定两个尺寸都大于视口..
void constrain(Control ctl, Control view)
{
Rectangle pr = view.ClientRectangle;
Rectangle cr = ctl.ClientRectangle;
int x = Math.Min(0, Math.Max(ctl.Left, pr.Width - cr.Width));
int y = Math.Min(0, Math.Max(ctl.Top, pr.Height - cr.Height));
ctl.Location = new Point(x,y);
}