Re-Inventing The Label Control

我的未来我决定 提交于 2019-12-05 08:45:59

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

Why not just extend the current one?

class MyMojoLabel : Label // Kind of thing

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.

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?

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!