【问题标题】:How to control position of label or any control PictureBox?如何控制标签或任何控件PictureBox的位置?
【发布时间】:2016-05-03 20:20:56
【问题描述】:

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

像这样:

但问题是:图像结果是:

在使用 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);

【问题讨论】:

  • 尽管您做出了努力,但我发现这个问题还不清楚。首先想到的是:Label 是在 PictureBox 上还是在里面?见here for the difference。对您想要实现的目标进行更清晰的顶层描述也会有所帮助..
  • 我使用此代码将 label1 添加到 pictureBox1。喜欢pictureBox1.Controls.Add(label1);
  • 我只想把 PictureBox 当作图像。当我在位置设置标签时:结果图像中 PictureBox 的中心或左侧将导出图像。当前,当我在 PictureBox 中设置位置时,它不会在结果中准确显示 Point()。

标签: c#


【解决方案1】:

如果您的PictureBoxSizemodes StretchImageZoom 中,则像素会被缩放; LabelLocation 但不是。因此,您将计算要绘制的位置:

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

如果你也想修正尺寸,你可以使用类似的功能..

如果SizeModeCenterImage,则像素未缩放,但很可能已转置,并且还需要进行校正。

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

【讨论】:

  • 还有图片吗? scalX/Y 值可信吗?
  • 啊,那是另一个方向;有时你需要一个有时另一个。我会更新答案..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2011-02-18
  • 1970-01-01
相关资源
最近更新 更多