Pan PictureBox inside a Panel on Mouse Move

天涯浪子 提交于 2019-12-11 11:07:21

问题


I've a pictureBox1 which is inside Panel1, both of same size. The pictureBox1 is resized on MouseWheel event, when the pictureBox1 size is bigger then Panel1 size then the user can Pan PictureBox1 on Mouse_Move event (want to move with mouse drag, not scroll bars). I've written a code which prevents users to Pan past the Panel1 borders. Right now the code can prevent only Top-Left orner and Bottom-Right corner. The problem in my code is when the user pans to Top-Right corner or Bottom-Left corner the pictureBox1 is still able to pan. But if pan only one either of one side at a time, the PictureBox1 stays inside the Panel1. I tried editing my code but I'm not able to get a proper solution. if anyone can help me figure out this problem in my code will be a great help.

The below code is under pictureBox1_MouseMove event

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

The Current code stops the user to move the pictureBox Beyond Point(0,0) on Top-Left Corner if the user tries to move the pictureBox towards right-down, and Beyond Point(minPointX, minPointY) if the user tries to move the pictureBox towards top-right. minPointX and minPointY is calculated by substracting the panel.Width to pictureBox.Width and panel.Heigh to pictureBox.Height respectively. minPointX and minPointY are the minimum points to which a user can move the pictureBox towards negative x and y axis.


回答1:


You can use the panel autoScroll property. Make sure the pictureBox inside the panel is not anchored. Then set panel autoScroll property to true.

Now when the pictureBox gets bigger then the panel it will automatically show the Scroll bars. Now in the mouse move event set the AutoScrollPosition as shown in the below code. Hope it helps. In the code below e is the MouseEventArgs.

panel1.AutoScrollPosition = new Point(-(panel1.AutoScrollPosition.X + e.X - startPoint.X),
                                      -(panel1.AutoScrollPosition.Y + e.Y - startPoint.Y));



回答2:


Here is a routine that contrains a control to stay within a viewport. It assumes that both dimensions are larger than the viewport..

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


来源:https://stackoverflow.com/questions/37558765/pan-picturebox-inside-a-panel-on-mouse-move

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!