[英]Pan PictureBox inside a Panel on Mouse Move
我在Panel1内有一个pictureBox1,两者的大小相同。 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);
}
如果用户尝试向右下移动pictureBox,则当前代码将停止用户将PictureBox移至左上角的Point(0,0)之外;如果用户尝试将PictureBox移至Point(minPointX,minPointY),则当前代码将停止该移动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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.