gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:30:13 25 4
gpt4 key购买 nike

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

最佳答案

如果您的 PictureBox 处于 Sizemodes 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,则像素不会缩放,但很可能已转置,因此也需要进行校正。

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

关于c# - 如何控制标签或任何控件 PictureBox 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35027931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com