重新发明标签控件

发布于 2024-08-30 13:26:22 字数 259 浏览 6 评论 0原文

我需要从头开始重新发明/重新创建标签控件,以添加我自己的魔力。是的,我知道你在想什么(如果你不这么想,你不应该这样想吗?)。

有人能指出我正确的方向吗?

谢谢。

重新创建标签的全部目的是我想要完全控制它如何绘制到屏幕上,这样我也可以拥有它的 KeyDown 事件处理程序。例如,用户可以像编辑 TextBox 控件的内容一样编辑标签的内容。

另外,我不能简单地使用 TextBox 控件,因为它几乎需要(甚至更多)工作才能获得我想要的结果。

I need to reinvent/recreate the Label Control from scratch to add my own mojoness to it. Yes, I know what you're thinking (and if you're not thinking that, shouldn't you be?).

Can somebody point me in the right direction?

Thank you.

The whole purpose for recreating the label is that I want full control over how it is drawn onto screen, and so that I can have KeyDown Event Handlers for it, too. For example, the user can edit the contents of a label the same way they would edit the contents of a TextBox control.

Also, I cannot just simply use a TextBox control, as it would require almost, if not more work to get my desired result.

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

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

发布评论

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

评论(5

深居我梦 2024-09-06 13:26:22

为什么不直接延长当前的时间呢?

class MyMojoLabel :标签 // 类似的东西

Why not just extend the current one?

class MyMojoLabel : Label // Kind of thing

阳光①夏 2024-09-06 13:26:22

一个好的起点也许是了解微软如何实现该标签。

为了更好地深入了解,您应该使用 Reflector 查看类或 Label 控件的调试源代码

A good starting point would maybe to understand how Microsoft implemented the label.

To get a better in-depth look you should take a look with Reflector into the class or debugging the source code of the Label control.

风筝有风,海豚有海 2024-09-06 13:26:22
public class MyLabel : System.Windows.Forms.Label
{
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        base.OnPaint(e);
        // or leave base out

        // you would determine these values by the length of the text
        e.Graphics.DrawEllipse(new System.Drawing.Pen(System.Drawing.Color.Red), 
                               0, 0, 50, 12);

    }

    protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
    {
        base.OnKeyDown(e);

        // although a label has a KeyDown event I don't know how it would 
        // receive focus, maybe you should create a text box that looks
        // like a label
    }
}

这个怎么样?

public class MyLabel : System.Windows.Forms.Label
{
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        base.OnPaint(e);
        // or leave base out

        // you would determine these values by the length of the text
        e.Graphics.DrawEllipse(new System.Drawing.Pen(System.Drawing.Color.Red), 
                               0, 0, 50, 12);

    }

    protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
    {
        base.OnKeyDown(e);

        // although a label has a KeyDown event I don't know how it would 
        // receive focus, maybe you should create a text box that looks
        // like a label
    }
}

How about this?

瞄了个咪的 2024-09-06 13:26:22

创建一个继承自 Control 的类。使用 SetStyle() 调用启用用户绘画和双缓冲,并重写 OnPaint() 以及您需要的任何其他方法。

class MyLabel : System.Windows.Forms.Control
{
    public MyLabel()
    {
        this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        base.OnPaint(e);
        ControlPaint.DrawBorder3D(  e.Graphics, this.ClientRectangle, Border3DStyle.Etched, Border3DSide.All);
        e.Graphics.DrawString(this.Text, this.Font, Brushes.Red, 0, 0);
    }
}

Create a class that inherits from Control. Use the SetStyle() call to enable user painting and double-buffering, and override the OnPaint(), and any other methods that you need.

class MyLabel : System.Windows.Forms.Control
{
    public MyLabel()
    {
        this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        base.OnPaint(e);
        ControlPaint.DrawBorder3D(  e.Graphics, this.ClientRectangle, Border3DStyle.Etched, Border3DSide.All);
        e.Graphics.DrawString(this.Text, this.Font, Brushes.Red, 0, 0);
    }
}
稚气少女 2024-09-06 13:26:22

创建您自己的标签控件非常简单,您只需从 Control 开始并重写 OnPaint() 即可。去byte就是把它变成一个也有聚焦行为的控件。并允许用户编辑文本。完成后,您将重新发明 TextBox 控件。这比看起来要难得多。

首先集中注意力,这是最棘手的问题。用户不太可能想要频繁地聚焦控件。也许是某种秘密握手,例如双击。当您检测到一个时,您可以创建一个 TextBox 控件并将其放在标签前面。并在失去焦点时将其丢弃,更新标签的 Text 属性。或者一个显示小型编辑对话框的简单上下文菜单。

使用双击编辑方法的示例:

using System;
using System.Windows.Forms;

class MyLabel : Label {
  private TextBox mEditor;
  protected override void OnDoubleClick(EventArgs e) {
    if (mEditor == null) {
      mEditor = new TextBox();
      mEditor.Location = this.Location;
      mEditor.Width = this.Width;
      mEditor.Font = this.Font;
      mEditor.Text = this.Text;
      mEditor.SelectionLength = this.Text.Length;
      mEditor.Leave += new EventHandler(mEditor_Leave);
      this.Parent.Controls.Add(mEditor);
      this.Parent.Controls.SetChildIndex(mEditor, 0);
      mEditor.Focus();
    }
    base.OnDoubleClick(e);
  }
  void mEditor_Leave(object sender, EventArgs e) {
    this.Text = mEditor.Text;
    mEditor.Dispose();
    mEditor = null;
  }
  protected override void Dispose(bool disposing) {
    if (disposing && mEditor != null) mEditor.Dispose();
    base.Dispose(disposing);
  }
}

Creating your own label control is simple enough, you just need to start with Control and override OnPaint(). What is going to byte is to turn it into a control that also has focusing behavior. And allows the user to edit the text. By the time you're done, you'll have re-invented the TextBox control. Which is a lot harder than it looks.

Focus on the focusing first, that's the trickiest problem. It isn't very likely that the user will want to focus the control frequently. Maybe some kind of secret handshake, like a double-click. When you detect one, you could create a TextBox control and put it in front of the label. And dispose it when it loses focus, updating the label's Text property. Or a simple context menu that displays a small editing dialog.

An example that uses the double-click to edit approach:

using System;
using System.Windows.Forms;

class MyLabel : Label {
  private TextBox mEditor;
  protected override void OnDoubleClick(EventArgs e) {
    if (mEditor == null) {
      mEditor = new TextBox();
      mEditor.Location = this.Location;
      mEditor.Width = this.Width;
      mEditor.Font = this.Font;
      mEditor.Text = this.Text;
      mEditor.SelectionLength = this.Text.Length;
      mEditor.Leave += new EventHandler(mEditor_Leave);
      this.Parent.Controls.Add(mEditor);
      this.Parent.Controls.SetChildIndex(mEditor, 0);
      mEditor.Focus();
    }
    base.OnDoubleClick(e);
  }
  void mEditor_Leave(object sender, EventArgs e) {
    this.Text = mEditor.Text;
    mEditor.Dispose();
    mEditor = null;
  }
  protected override void Dispose(bool disposing) {
    if (disposing && mEditor != null) mEditor.Dispose();
    base.Dispose(disposing);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文