如何控制标签或任何控件PictureBox的位置?

时间:2016-01-27 02:47:56

标签: c#

我在PictureBox中移动控件(标签或图像)成功。当我移动时,它将保存控制位置(x,y)。

像这样:

This will save position

但问题是:图像结果是:

Image result

在gif图像之前,我将标签控件拖放到中心屏幕。但结果图像不会在中心屏幕上保存标签。

我将PictureBox属性设置为StretchImage。

我在PictureBox中获取位置和DrawText的代码如下:

public PositionControl CtrlPos = new PositionControl();
private void control_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Control control = (Control)sender;
        Point nextPosition = new Point();
        nextPosition = picPreview.PointToClient(MousePosition);
        nextPosition.Offset(mouseX, mouseY);
        control.Location = nextPosition;
        CtrlPos.x = nextPosition.X;
        CtrlPos.y = nextPosition.Y;
        Invalidate();
    }
}

我的代码是父控件(PictureBox)包含所有控件。

在我的班上,我正在使用它:

private void picPreview_MouseMove(object sender, MouseEventArgs e)
{
    if (SelectedControl != null && e.Button == MouseButtons.Left)
    {
        timer1.Stop();
        Invalidate();

        if (SelectedControl.Height < 20)
        {
            SelectedControl.Height = 20;
            direction = Direction.None;
            Cursor = Cursors.Default;
            return;
        }
        else if (SelectedControl.Width < 20)
        {
            SelectedControl.Width = 20;
            direction = Direction.None;
            Cursor = Cursors.Default;
            return;
        }
        //get the current mouse position relative the the app
        Point pos = picPreview.PointToClient(MousePosition);
        #region resize the control in 8 directions
        if (direction == Direction.NW)
        {
            //north west, location and width, height change
            newLocation = new Point(pos.X, pos.Y);
            newSize = new Size(SelectedControl.Size.Width - (newLocation.X - SelectedControl.Location.X),
                SelectedControl.Size.Height - (newLocation.Y - SelectedControl.Location.Y));
            SelectedControl.Location = newLocation;
            CtrlPos.x = newLocation.X;
            CtrlPos.y = newLocation.Y;
            SelectedControl.Size = newSize;
        }
        else if (direction == Direction.SE)
        {
            //south east, width and height change
            newLocation = new Point(pos.X, pos.Y);
            newSize = new Size(SelectedControl.Size.Width + (newLocation.X - SelectedControl.Size.Width - SelectedControl.Location.X),
                SelectedControl.Height + (newLocation.Y - SelectedControl.Height - SelectedControl.Location.Y));
            SelectedControl.Size = newSize;
        }
        else if (direction == Direction.N)
        {
            //north, location and height change
            newLocation = new Point(SelectedControl.Location.X, pos.Y);
            newSize = new Size(SelectedControl.Width,
                SelectedControl.Height - (pos.Y - SelectedControl.Location.Y));
            SelectedControl.Location = newLocation;
            SelectedControl.Size = newSize;
        }
        else if (direction == Direction.S)
        {
            //south, only the height changes
            newLocation = new Point(pos.X, pos.Y);
            newSize = new Size(SelectedControl.Width,
                pos.Y - SelectedControl.Location.Y);
            SelectedControl.Size = newSize;
        }
        else if (direction == Direction.W)
        {
            //west, location and width will change
            newLocation = new Point(pos.X, SelectedControl.Location.Y);
            newSize = new Size(SelectedControl.Width - (pos.X - SelectedControl.Location.X),
                SelectedControl.Height);
            SelectedControl.Location = newLocation;
            SelectedControl.Size = newSize;
        }
        else if (direction == Direction.E)
        {
            //east, only width changes
            newLocation = new Point(pos.X, pos.Y);
            newSize = new Size(pos.X - SelectedControl.Location.X,
                SelectedControl.Height);
            SelectedControl.Size = newSize;
        }
        else if (direction == Direction.SW)
        {
            //south west, location, width and height change
            newLocation = new Point(pos.X, SelectedControl.Location.Y);
            newSize = new Size(SelectedControl.Width - (pos.X - SelectedControl.Location.X),
                pos.Y - SelectedControl.Location.Y);
            SelectedControl.Location = newLocation;
            SelectedControl.Size = newSize;
        }
        else if (direction == Direction.NE)
        {
            //north east, location, width and height change
            newLocation = new Point(SelectedControl.Location.X, pos.Y);
            newSize = new Size(pos.X - SelectedControl.Location.X,
                SelectedControl.Height - (pos.Y - SelectedControl.Location.Y));
            SelectedControl.Location = newLocation;
            SelectedControl.Size = newSize;
        }
        #endregion
    }
}

使用CtrlPost(x,y)绘制图像:

g.DrawImage(
    DrawText(image, new Font(cbxFont.Text, fontSize), colorInput,
        Color.Transparent),
    new Point(CtrlPos.x, CtrlPos.y));
// g is Graphics object.

更新: 我正在使用代码将label1添加到pictureBox1,如下所示:

pictureBox1.Controls.Add(label1);

1 个答案:

答案 0 :(得分:2)

如果SELECT * INTO #Table1 FROM ( SELECT COUNT(Cases.pk_Cases_CaseID) TotalCases, Districts.DistrictName FROM Cases INNER JOIN ConcernedOffices ON ConcernedOffices.pk_ConcernedOffices_ID = Cases.fk_ConcernedOffices_Cases_ConcernedOfficeID INNER JOIN Districts ON Districts.pk_Districts_DistrictID = ConcernedOffices.fk_Districts_ConcernedOffices_DistrictID INNER JOIN CaseHearings ON CaseHearings.fk_Cases_CaseHearings_CaseID = Cases.pk_Cases_CaseID WHERE CaseHearings.IsClosingDate = 1 GROUP BY Districts.DistrictName ) d SELECT * INTO #Table2 FROM ( SELECT COUNT(Cases.pk_Cases_CaseID) TotalPedningCases, Districts.DistrictName FROM Cases INNER JOIN ConcernedOffices ON ConcernedOffices.pk_ConcernedOffices_ID = Cases.fk_ConcernedOffices_Cases_ConcernedOfficeID INNER JOIN Districts ON Districts.pk_Districts_DistrictID = ConcernedOffices.fk_Districts_ConcernedOffices_DistrictID INNER JOIN CaseHearings ON CaseHearings.fk_Cases_CaseHearings_CaseID = Cases.pk_Cases_CaseID WHERE CaseHearings.IsClosingDate = 0 GROUP BY Districts.DistrictName ) d SELECT #Table1.TotalCases AS TotalSolvedCases, #Table2.TotalPedningCases, #Table1.DistrictName FROM #Table1 INNER JOIN #Table2 ON #Table2.DistrictName = #Table1.DistrictName GROUP BY #Table1.TotalCases, #Table2.TotalPedningCases, #Table1.DistrictName 位于PictureBox SizemodesStretchImage,则缩放像素;然而,Zoom的{​​{1}}却没有。所以你可以计算绘制的位置:

Label

您可以将其称为Location

你可能会这样画:

PointF stretched(Point p0, PictureBox pb)
{
    if (pb.Image == null) return PointF.Empty;

    float scaleX = 1f * pb.Image.Width  / pb.ClientSize.Width;
    float scaleY = 1f * pb.Image.Height / pb.ClientSize.Height;

    return new PointF(p0.X * scaleX, p0.Y * scaleY);
}

如果您还想更正尺寸,可以使用类似的功能..

如果PointF p1 = stretched(p0, pictureBox1);g.DrawImage( DrawText(image, new Font(cbxFont.Text, fontSize), colorInput, Color.Transparent), Point.Round(stretched( CtrlPos.Location, picPreview)); ,则像素不会缩放但很可能会进行转置,并且还需要进行修正。

对于另一个方向,只需在分数中切换分母和分子!