天天看点

C# 运用继承的方式自定义控件

我们知道万事万物都是类 窗口可以用窗口类(From)实例化出来 按钮可以用Button类实例化出来

例如 下面代码就可以创建一个按钮 (在窗口运行的时候 就可以在窗口的(100,100)位置中出现这个按钮了)

C# 运用继承的方式自定义控件

当然点击事件我们需要的时候才添加。

事实上 上面的代码就是 你平时在工具箱中 将一个按钮拉到窗口面板(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事件 发生了作用 多了一个绿色的框

C# 运用继承的方式自定义控件

点击mybutton后

可以看到 我们重写的click事件发生了作用 文本改变了

C# 运用继承的方式自定义控件

所有代码

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

}

           

继续阅读