简体   繁体   English

在鼠标上单击弹出的文本框,在图片框上添加图片自定义注释

[英]Popping up a TextBox on mouse click over a PictureBox for adding custom note to picture

In my C# winforms application, I have a picturebox which some bitmap images can be loaded into it. 在我的C#winforms应用程序中,我有一个图片框,可以将一些位图图像加载到其中。 What I want to do is if user clicks somewhere within picturebox, at the mouse location a small textbox will be appeared and user can add a custom text (note) to the picture. 我想做的是,如果用户单击图片框内的某个位置,则在鼠标位置处将出现一个小文本框,用户可以向图片添加自定义文本(注释)。

I know how to write a string into a bitmap file, but I couldnt find a way to POP UP a textbox at mouse location, and automaticly add the text to the image when user wrote something and hit enter key. 我知道如何将字符串写入位图文件,但是我找不到在鼠标位置弹出文本框的方法,并在用户写东西并按Enter键时自动将文本添加到图像中。 How this textbox and its properties should be defined? 如何定义此文本框及其属性?

Thanks. 谢谢。

You could embed a control in a custom popup form, as shown below. 您可以将控件嵌入自定义弹出窗体中,如下所示。

The last argument in the PopupForm constructor specifies the action to take, when the user presses Enter . 当用户按下Enter键时,PopupForm构造函数中的最后一个参数指定要执行的操作。 In this example an anonymous method is specified, setting the title of the form. 在此示例中,指定了一个匿名方法,用于设置表单的标题。

Usage 用法

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

I suppose, you could create Textbox dynamically on mouse click and use it's BringToFront() method in case is will not appear above the picture box. 我想,您可以在鼠标单击时动态创建Textbox,并使用它的BringToFront()方法,以防它不会出现在图片框上方。 When the user presses Enter, handle this event, get text from Textbox and remove if if needed. 当用户按下Enter键时,处理此事件,从“文本框”获取文本,并在需要时将其删除。

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

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