C#继承Control实用自定义控件

start
步骤一:新建类库项目,新建FirstControl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace UcDll
{
     public  class FirstControl : Control
    {

         public FirstControl()
        {

        }

         //  ContentAlignment is an enumeration defined in the System.Drawing
        
//  namespace that specifies the alignment of content on a drawing 
        
//  surface.
         private ContentAlignment alignmentValue = ContentAlignment.MiddleLeft;

        [
        Category( " Alignment "),
        Description( " Specifies the alignment of text. ")
        ]
         public ContentAlignment TextAlignment
        {

             get
            {
                 return alignmentValue;
            }
             set
            {
                alignmentValue = value;

                 //  The Invalidate method invokes the OnPaint method described 
                
//  in step 3.
                Invalidate();
            }
        }


         protected  override  void OnPaint(PaintEventArgs e)
        {
             base.OnPaint(e);
            StringFormat style =  new StringFormat();
            style.Alignment = StringAlignment.Near;
             switch (alignmentValue)
            {
                 case ContentAlignment.MiddleLeft:
                    style.Alignment = StringAlignment.Near;
                     break;
                 case ContentAlignment.MiddleRight:
                    style.Alignment = StringAlignment.Far;
                     break;
                 case ContentAlignment.MiddleCenter:
                    style.Alignment = StringAlignment.Center;
                     break;
            }

             //  Call the DrawString method of the System.Drawing class to write   
            
//  text. Text and ClientRectangle are properties inherited from
            
//  Control.
            e.Graphics.DrawString(
                Text,
                Font,
                 new SolidBrush(ForeColor),
                ClientRectangle, style);

        }
    }
}
步骤二:编译,添加项目引用即可。
这里的重点是控件界面也要由自己绘。
url: http://greatverve.cnblogs.com/archive/2012/04/27/control-Inherit.html
参考:
http://www.cnblogs.com/guanjinke/category/77694.html
http://blog.csdn.net/yysyangyangyangshan/article/details/7078471
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值