鼠标单击图片框时弹出文本框,以便为图片添加自定义注释

发布于 2024-10-30 12:56:28 字数 213 浏览 5 评论 0原文

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

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

谢谢。

In my C# winforms application, I have a picturebox which some bitmap images can be loaded into it. 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. How this textbox and its properties should be defined?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残月升风 2024-11-06 12:56:28

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

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

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. 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

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();
  }
}
怪我太投入 2024-11-06 12:56:28

我想,您可以在鼠标单击时动态创建文本框,并使用它的 BringToFront() 方法,以防它不会出现在图片框上方。当用户按 Enter 时,处理此事件,从文本框中获取文本,并根据需要删除文本。

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. When the user presses Enter, handle this event, get text from Textbox and remove if if needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文