gpt4 book ai didi

c# - 在鼠标点击 PictureBox 时弹出一个 TextBox 以向图片添加自定义注释

转载 作者:太空宇宙 更新时间:2023-11-03 20:34:44 25 4
gpt4 key购买 nike

在我的 C# winforms 应用程序中,我有一个图片框,可以将一些位图图像加载到其中。我想要做的是,如果用户点击图片框内的某处,在鼠标位置会出现一个小文本框,用户可以向图片添加自定义文本(注释)。

我知道如何将字符串写入位图文件,但我找不到在鼠标位置弹出文本框并在用户输入内容并按下回车键时自动将文本添加到图像的方法。应如何定义此文本框及其属性?

谢谢。

最佳答案

您可以在自定义弹出窗体中嵌入控件,如下所示。

PopupForm 构造函数中的最后一个参数指定当用户按下 Enter 时要执行的操作。在这个例子中指定了一个匿名方法,设置表单的标题。

用法

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
// in this case we create a TextBox, but the
// PopupForm can hold any type of control.
TextBox textBox = new TextBox();
Point location = pictureBox1.PointToScreen(e.Location);
PopupForm form = new PopupForm(textBox, location,
() => this.Text = textBox.Text);
form.Show();
}

PopupForm 类

public class PopupForm : Form
{
private Action _onAccept;
private Control _control;
private Point _point;

public PopupForm(Control control, int x, int y, Action onAccept)
: this(control, new Point(x, y), onAccept)
{
}

public PopupForm(Control control, Point point, Action onAccept)
{
if (control == null) throw new ArgumentNullException("control");

this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
this.KeyPreview = true;
_point = point;
_control = control;
_onAccept = onAccept;
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Controls.Add(_control);
_control.Location = new Point(0, 0);
this.Size = _control.Size;
this.Location = _point;
}

protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Enter)
{
_onAccept();
this.Close();
}
else if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}

protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
this.Close();
}
}

关于c# - 在鼠标点击 PictureBox 时弹出一个 TextBox 以向图片添加自定义注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5549150/

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