2016-06-01 98 views
1

我有一个pictureBox1,它在Panel1内部,两者的大小相同。 pictureBox1在MouseWheel事件上调整大小,当pictureBox1大小大于Panel1大小时,则用户可以在Mouse_Move事件上平移PictureBox1(想要用鼠标拖动而不是滚动条移动)。我编写了一个代码,可以防止用户跨过Panel1边框。现在,代码只能防止左上角和右下角。我的代码中的问题是当用户平移到右上角或左下角时,pictureBox1仍然能够平移。但是,如果一次只平移一侧的任一侧,则PictureBox1将停留在Panel1内。 我试着编辑我的代码,但我无法获得适当的解决方案。如果有人能帮我弄清楚这个问题在我的代码中会有很大的帮助。PanBox在鼠标移动面板内部

下面的代码是在pictureBox1_MouseMove事件

左上角 Top Left Corner

右下角 Bottom Right Corner

右上角 Top Right Corner

左下角 Bottom Left Corner

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移向右上方。 minPointXminPointY分别通过减去panel.WidthpictureBox.Widthpanel.HeighpictureBox.Height来计算。 minPointX和minPointY是用户可以将pictureBox移向负x和y轴的最小点。

+1

如果你只是想允许用户移动PictureBox的面板内,那么你为什么不设置面板AutoSrcoll财产?它将创建滚动条以允许移动内部控件(平移它)。 – Gusman

+0

我不想使用滚动条。我想在鼠标移动事件上进行平移。 –

+0

您可以更改Horizo​​ntalScroll.Value和VerticalScroll.Value以使用鼠标移动的增量移动内容,这样您将为用户提供两种平移内容的方法。 – Gusman

回答

3

您可以使用面板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)); 
+0

OP已经明确表示他不需要滚动条。你可以隐藏它们吗? – TaW

+0

哦。当自动滚动设置为true时,否不能隐藏滚动条。 –

+0

我敢打赌,你可能会弄清楚一种隐藏滚动条的方法。但即使你可以,我也强烈建议不要这样做。滚动条都清楚地表明该区域是可移动的,并且为不能或不想用鼠标平移的用户提供另一种滚动方式。拖动平移是一个很好的选择,为方便起见应该支持,但它不应该是* only *选项。就像触摸一样。除非您正在编写内置触摸屏的硬件,没有其他可行的交互方式,否则您应该支持多种范例。滚动条没有错。 –

2

这是一个例程,禁止一个控件留在视口内。它假定这两个方面比视大..

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); 
}