简体   繁体   中英

How to control position of label or any control PictureBox?

I'm moving control(label or image) success in PictureBox. When I move, it will save control position(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. But result image doesn't save label in center screen.

I was set PictureBox attribute to StretchImage.

My code to get position and DrawText in PictureBox like:

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.

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

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:

pictureBox1.Controls.Add(label1);

If your PictureBox is in Sizemodes StretchImage or Zoom then the pixels are scaled; the Label 's Location however is not. 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);

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.

For the other direction simply switch denominator and numerator in the fractions!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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