简体   繁体   English

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

[英]How to control position of label or any control PictureBox?

I'm moving control(label or image) success in PictureBox. 我在PictureBox中移动控件(标签或图像)成功。 When I move, it will save control position(x, y). 当我移动时,它将保存控制位置(x,y)。

Like this: 像这样:

这将节省位置

But problem is: the image result is: 但是问题是:图像结果是:

图片结果

Before gif image, I was drag and drop a label control in center screen. 在gif图像之前,我将标签控件拖放到中心屏幕中。 But result image doesn't save label in center screen. 但是结果图像不会在中心屏幕中保存标签。

I was set PictureBox attribute to StretchImage. 我将PictureBox属性设置为StretchImage。

My code to get position and DrawText in PictureBox like: 我的代码来获取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();
    }
}

My code is parent control(PictureBox) contain all control. 我的代码是父控件(PictureBox)包含所有控件。

In my class, I'm using this: 在我的课堂上,我使用的是:

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

Draw image using CtrlPost(x, y): 使用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.

Updated: I was using code to add label1 to pictureBox1 like this: 更新:我正在使用代码像这样将label1添加到pictureBox1

pictureBox1.Controls.Add(label1);

If your PictureBox is in Sizemodes StretchImage or Zoom then the pixels are scaled; 如果您的PictureBox处于Sizemodes StretchImageZoom Sizemodes StretchImage Zoom像素;否则,将缩放比例。 the Label 's Location however is not. 但是LabelLocation不是。 So you would have the calculate the position where to draw: 这样就可以计算出绘制位置:

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

You would call it as PointF p1 = stretched(p0, pictureBox1); 您将其称为PointF p1 = stretched(p0, pictureBox1);

You would draw maybe like this: 您可能会这样绘制:

g.DrawImage(  DrawText(image, new Font(cbxFont.Text, fontSize), 
                       colorInput, Color.Transparent),
              Point.Round(stretched( CtrlPos.Location, picPreview));

If you also want to correct the size you can use a similar function.. 如果您还想更正尺寸,可以使用类似的功能。

If the SizeMode is CenterImage the pixels are not scaled but most likely transposed and a correction is necessary as well. 如果SizeModeCenterImage ,则不会缩放像素,但很可能会转置像素,因此也必须进行校正。

For the other direction simply switch denominator and numerator in the fractions! 对于另一个方向,只需在分数中切换分母和分子!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM