我有一个pictureBox1,它在Panel1内部,两者的大小相同。 pictureBox1在MouseWheel事件上调整大小,当pictureBox1大小大于Panel1大小时,则用户可以在Mouse_Move事件上平移PictureBox1(想要用鼠标拖动而不是滚动条移动)。我编写了一个代码,可以防止用户跨过Panel1边框。现在,代码只能防止左上角和右下角。我的代码中的问题是当用户平移到右上角或左下角时,pictureBox1仍然能够平移。但是,如果一次只平移一侧的任一侧,则PictureBox1将停留在Panel1内。 我试着编辑我的代码,但我无法获得适当的解决方案。如果有人能帮我弄清楚这个问题在我的代码中会有很大的帮助。PanBox在鼠标移动面板内部
下面的代码是在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的超越点(0,0),以及超过点(minPointX,minPointY)如果用户试图将pictureBox移向右上方。 minPointX
和minPointY
分别通过减去panel.Width
到pictureBox.Width
和panel.Heigh
到pictureBox.Height
来计算。 minPointX和minPointY是用户可以将pictureBox移向负x和y轴的最小点。
如果你只是想允许用户移动PictureBox的面板内,那么你为什么不设置面板AutoSrcoll财产?它将创建滚动条以允许移动内部控件(平移它)。 – Gusman
我不想使用滚动条。我想在鼠标移动事件上进行平移。 –
您可以更改HorizontalScroll.Value和VerticalScroll.Value以使用鼠标移动的增量移动内容,这样您将为用户提供两种平移内容的方法。 – Gusman