在鼠标移动的面板内平移PictureBox

时间:2016-06-01 03:08:36

标签: c# pan

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

以下代码位于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 Beyond Point(0,0),如果用户尝试将其移动到Beyond Point(minPointX,minPointY)将pictureBox移向右上角。 minPointXminPointY的计算方法是将panel.Width分别减去pictureBox.Widthpanel.Heigh减去pictureBox.Height。 minPointX和minPointY是用户可以将pictureBox移向负x轴和y轴的最小点。

2 个答案:

答案 0 :(得分:4)

您可以使用面板SELECT CASE WHEN (SUM(cashBySum) > SUM(cashByName)) THEN ( SELECT Sale.partyDBId ,Sale.id ,Sale_Products.quantity ,Sale_Products.FinalWeight ,Sale_Products.finalRate ,Sale_Products.SaleDBid ,product.productNameUR FROM Sale INNER JOIN Sale_Products ON Sale.id = Sale_Products.SaleDBid INNER JOIN product ON Sale_Products.productBDid = product.id WHERE (Sale.partyDBId = '20031') AND ( CAST(Sale.SaleDate AS DATE) BETWEEN '2015-04-09 12:31:41.000' AND '2016-04-09 12:31:41.000' ) ) WHEN (SUM(cashBySum) < SUM(cashByName)) THEN N' بنام' ELSE '' END AS balancetype ,SUM(cashBySum) - SUM(cashByName) AS balance ,ABS(SUM(cashBySum) - SUM(cashByName)) AS blc ,partyId FROM leger AS leger_1 WHERE (partyId = '20031') AND (DATE < '2016-04-09 12:31:41.000') GROUP BY partyId 属性。确保面板内的autoScroll未锚定。然后将面板pictureBox属性设置为autoScroll

现在当true变大时,面板会自动显示滚动条。 现在在鼠标移动事件中设置pictureBox,如下面的代码所示。希望能帮助到你。 在下面的代码中,e是AutoScrollPosition

MouseEventArgs

答案 1 :(得分: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);
}