關於自訂控制項

來源:互聯網
上載者:User

 

 前段時間糾結於自訂控制項,現在想總結出來供大家分享,分享;

就此總結,自訂控制項我將其分為三種

第一種,

也就是最簡單的一種,就只直接繼承自某個已經封裝好的控制項,如:Label,Button,PicBox等等

public class UserLabel:System.Windows.Forms.Label

{

        public string AAA { get; set; }

        public UserLabel() { }

        public void ABC()

        { 

        }

//在該類裡面,可以為UserLabel添加欄位,屬性,方法等;

}

第二種,

就是群組控制項,在VS2010的程式集中,右鍵,添加使用者控制項就是可以添加使用者控制項了,然後通過拖動左側的"工具列"可以添加控制項到自己要定義的控制項中

比如右圖,就是Label和PicBox的合成的;

然後可以像編輯表單一樣,設計控制項了;

第三種,也就是最麻煩的一種

首先要懂一些,C#中GDI的一些相關知識

比如說,現在你需要自訂一個圓形的控制項

要將其繼承字Control類;

using System.ComponentModel;

public partial class UserControlMake:System.Windows.Forms.Control

{

        //控制項大體架構,內圓

        protected Rectangle inrect = new Rectangle();

        //外圓

        protected Rectangle outrect = new Rectangle();

        //字型風格大小

        protected Font font = new System.Drawing.Font("華文行楷", 40);

        protected string ChessName = "預設字串";

//自己定義一些圖形的模板

        public UserControlMake()

        {

            InitializeComponent();

PS:如果你想控制項多一個背景顏色,透明

            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

        }

//以下分別是設定控制項的預設值,描述,一個類別;

//這些將在VS2010的屬性欄中顯示出來

        [DefaultValue("預設字串"), Description("一個屬性的描述"), Category("Chess")]

        public string CHESSNAME

        {

            get

            {

                return this.ChessName;

            }

            set

            {

                this.ChessName = value;

                this.Invalidate(); //重繪該控制項

            }

        }

注意:類似這種屬性添加的方式可以重載Control的的屬性

eg.

        [DefaultValue("Transparent"), Description("背?景°顏?色?")]

        public override Color BackColor

        {

            get

            {

                return base.BackColor;

            }

            set

            {

                base.BackColor = value;            }

        }

現在要畫控制項,重載Control中的OnPaint()方法

eg.

        protected override void OnPaint(PaintEventArgs e)

        {

PS.

在此控制項類中寫一個Reactant,或者是Point

該圖形的座標是相對的,也就是,比如一下矩形的左上方座標是相對於該控制項(2,2)的位置

            //裡面畫一個圓

            inrect.X = 2;

            inrect.Y = 2;

            inrect.Width = this.Width - 2;

            inrect.Height = this.Height - 2;

            e.Graphics.FillEllipse(new SolidBrush(Color.Black), inrect);

        }

}

關於"事件"

如果想定一些事件,可以在該控制項的構造方法中,為該事件添加訂閱者法

eg.

        public UserControl1()

        {

            InitializeComponent();

            this.Click += new EventHandler(UserControl1_Click);

        }

        void UserControl1_Click(object sender, EventArgs e)

        {

            throw new NotImplementedException();

        }

當然也可重載繼承自Control事件的的方法

eg.在該類中寫下

        protected override void OnClick(EventArgs e)

        {

            base.OnClick(e);

        }

PS.

當然也可以在該控制項拖入表單後再進行添加事件,

如果寫的時同一個事件,觸發的時間先後順序當然也不一樣

確實想寫一個控制項;這些類似的這些語句是必不可少的

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.