我們知道萬事萬物都是類 視窗可以用視窗類(From)執行個體化出來 按鈕可以用Button類執行個體化出來
例如 下面代碼就可以建立一個按鈕 (在視窗運作的時候 就可以在視窗的(100,100)位置中出現這個按鈕了)

當然點選事件我們需要的時候才添加。
事實上 上面的代碼就是 你平時在工具箱中 将一個按鈕拉到視窗面闆(100,100)位置并在屬性視窗中将Text屬性更改為“顯示字元串” 的操作 。
你利用工具箱建立 控件 設定屬性事件等等 通通可以用代碼來實作 甚至在解決方案視窗中的檔案也可以用代碼來建立。
有了上面的基礎 我們就可以利用繼承的方式建立自定義的控件 并且重寫該控件的事件
建立一個視窗應用
首先建立一個視窗程式 打開代碼編輯區
在視窗類的下面 定義一個繼承于Button類的按鈕類
public class myButton : Button
{
//構造函數
public myButton()
{
this.Click += MyButton_Click;
}
//重寫按鈕的click事件
private void MyButton_Click(object sender, EventArgs e)
{
this.Text = "點選了按鈕";
}
//重寫按鈕onpaint屬性
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
Rectangle tang = this.ClientRectangle; //擷取視窗矩形 為了下面得到視窗的寬高
Graphics g3 = pevent.Graphics; //建立一個畫布
Color c3 = Color.FromArgb(46, 204, 113); //聲明一個 顔色
Pen p3 = new Pen(c3); //建立一支畫筆
g3.DrawLine(p3, 10, 10, 10, tang.Height - 10);
g3.DrawLine(p3, 10, tang.Height - 10, tang.Width - 10, tang.Height - 10); //注意必須減1 不然顯示不出來 因為 如果假設視窗的高度是3像素 我們知道(0,0)位置代表 視窗最左上角的像素點 那麼最左下角的像素點應該是(0,2) 而不是(0,3) 因為0,1,2 已經三個像素點了
g3.DrawLine(p3, tang.Width - 10, tang.Height - 10, tang.Width - 10, 10);
g3.DrawLine(p3, tang.Width - 10, 10, 10, 10);
}
}
這樣就可以定義一個屬于自己的按鈕了
按鈕的執行個體化
為視窗添加一個load事件 然後執行個體化自定義的按鈕
private void Form1_Load(object sender, EventArgs e)
{
myButton myButton1 = new myButton(); //執行個體化
myButton1.Location = new Point(300, 300); //設定按鈕的位置為(300,300)
myButton1.Text = "mybutton"; //設定文本内容是mybutton
myButton1.Height = 40; //注意記得添加高度和寬度
myButton1.Width = 100;
Controls.Add(myButton1); //将控件添加到視窗上面 不添加不會顯示出來
}
按鈕展示
我們在工具箱拉一個按鈕到視窗上 與自定義的按鈕對比看看
可以看到我們重寫的onpaint事件 發生了作用 多了一個綠色的框
點選mybutton後
可以看到 我們重寫的click事件發生了作用 文本改變了
所有代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _123
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
myButton myButton1 = new myButton();
myButton1.Location = new Point(300, 300);
myButton1.Text = "mybutton";
myButton1.Height = 40;
myButton1.Width = 100;
Controls.Add(myButton1);
}
}
public class myButton : Button
{
//構造函數
public myButton()
{
this.Click += MyButton_Click;
}
//重寫按鈕的click事件
private void MyButton_Click(object sender, EventArgs e)
{
this.Text = "點選了按鈕";
}
//重寫按鈕onpaint屬性
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
Rectangle tang = this.ClientRectangle; //擷取視窗矩形 為了下面得到視窗的寬高
Graphics g3 = pevent.Graphics; //建立一個畫布
Color c3 = Color.FromArgb(46, 204, 113); //聲明一個 顔色
Pen p3 = new Pen(c3); //建立一支畫筆
g3.DrawLine(p3, 10, 10, 10, tang.Height - 10);
g3.DrawLine(p3, 10, tang.Height - 10, tang.Width - 10, tang.Height - 10); //注意必須減1 不然顯示不出來 因為 如果假設視窗的高度是3像素 我們知道(0,0)位置代表 視窗最左上角的像素點 那麼最左下角的像素點應該是(0,2) 而不是(0,3) 因為0,1,2 已經三個像素點了
g3.DrawLine(p3, tang.Width - 10, tang.Height - 10, tang.Width - 10, 10);
g3.DrawLine(p3, tang.Width - 10, 10, 10, 10);
}
}
}