简单label控件 自制

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
public class MyLabel : Control
{
 
    protected override void OnPaint(PaintEventArgs e)
    {
        DrawPaint();
        base.OnPaint(e);
    }
    private string showString = "";
    /// <summary>
    /// 显示字符串
    /// </summary>
    public string ShowString
    {
        get { return showString; }
        set { showString = value; 
                 this.Refresh();
              }
    }
    /// <summary>
    /// 先在缓存内画好
    /// </summary>
    public void DrawPaint()
    {
        Bitmap offBm = new Bitmap(Width, Height);
        Graphics offerSreen = Graphics.FromImage(offBm);//定义画画到图片上
        SolidBrush tempsb = new SolidBrush(Color.White);//定义画笔
        offerSreen.FillRectangle(tempsb, 0, 0, Width, Height);//填充颜色
        tempsb.Color=Color.Black;
        offerSreen.DrawString(showString, this.Font, tempsb, 0, 0);//写文字
        this.CreateGraphics().DrawImage(offBm, 0, 0);//贴出来显示
        offBm.Dispose();//释放
        offerSreen.Dispose();
        tempsb.Dispose();

    }
    /// <summary>
    /// 不让重画背景
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        //base.OnPaintBackground(e);
    }
}
 
原文地址:https://www.cnblogs.com/FuYan/p/4142187.html