码迷,mamicode.com
首页 > 其他好文 > 详细

简单label控件 自制

时间:2014-12-04 11:32:19      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   on   

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

 

简单label控件 自制

标签:style   blog   io   ar   color   os   sp   for   on   

原文地址:http://www.cnblogs.com/FuYan/p/4142187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!