C# 在图片框上单击鼠标弹出一个文本框,用于向图片添加自定义注释
在我的C#winforms应用程序中,我有一个picturebox,可以将一些位图图像加载到其中。我想做的是,如果用户单击picturebox中的某个位置,在鼠标位置会出现一个小文本框,用户可以向图片添加自定义文本(注释) 我知道如何将字符串写入位图文件,但我找不到一种方法在鼠标位置弹出文本框,并在用户写入内容并按enter键时自动将文本添加到图像中。如何定义此文本框及其属性C# 在图片框上单击鼠标弹出一个文本框,用于向图片添加自定义注释,c#,textbox,popup,picturebox,drawstring,C#,Textbox,Popup,Picturebox,Drawstring,在我的C#winforms应用程序中,我有一个picturebox,可以将一些位图图像加载到其中。我想做的是,如果用户单击picturebox中的某个位置,在鼠标位置会出现一个小文本框,用户可以向图片添加自定义文本(注释) 我知道如何将字符串写入位图文件,但我找不到一种方法在鼠标位置弹出文本框,并在用户写入内容并按enter键时自动将文本添加到图像中。如何定义此文本框及其属性 谢谢。我想,您可以在鼠标单击时动态创建文本框,并使用它的BringToFront()方法,以防它不会出现在图片框上方。当
谢谢。我想,您可以在鼠标单击时动态创建文本框,并使用它的BringToFront()方法,以防它不会出现在图片框上方。当用户按Enter键时,处理此事件,从Textbox中获取文本,如果需要,将其删除。我想,您可以在鼠标单击时动态创建Textbox,并使用它的BringToFront()方法,以防图片框上方不会出现。当用户按Enter键时,处理此事件,从文本框中获取文本,并在需要时删除。您可以在自定义弹出窗体中嵌入控件,如下所示 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();
}
弹出窗体类
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();
}
}
您可以将控件嵌入自定义弹出窗体中,如下所示 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();
}
弹出窗体类
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();
}
}
谢谢,但是动态创建文本框意味着什么?!这意味着textbox在设计时不会出现在表单上,而是在需要时添加到代码中,即var textbox=new textbox()。谢谢,但动态创建textbox意味着什么?!这意味着textbox在设计时不会出现在表单上,而是在需要时添加到代码中,即var textbox=new textbox()。