C# 自定义控件,自定义属性,自定义事件

42 篇文章 0 订阅
4 篇文章 0 订阅




SplitLineHorizontal.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskManager
{
    // 此类用于实现绘制一条水平分隔线, 按F5运行一次,再拖动当前控件到Form窗体即可看到
    // 自定义属性:DrawColor和自定义事件This_Selected
    public partial class SplitLineHorizontal : UserControl
    {
        public SplitLineHorizontal()
        {
            InitializeComponent();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //不进行背景的绘制
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
                return cp;
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Pen pen = new Pen(drawColor);
            pen.Width = 2;

            // 绘制一条水平分割线
            e.Graphics.DrawLine(pen, new Point(5, 5), new Point(this.Width - 5, 5));

            绘制panel的背景图像
            //if (BackgroundImage != null) e.Graphics.DrawImage(this.BackgroundImage, new Point(0, 0));
        }


        
        private Color drawColor = Color.Black;  // 存储颜色值的内部变量
        [Description("修改此值,可修改分割线的颜色"), Category("自定义属性")]
        public Color DrawColor                  // 控件的自定义属性值
        {
            get
            {
                return drawColor;
            }
            set
            {
                drawColor = value;

                // 此处修改,为自定义属性变动时,执行的操作
                this.Invalidate();  // 此处当颜色值属性变动时,使用新的颜色,使自定义控件重绘
            }
        }


        public delegate void select_Handle(object sender, EventArgs e, string orther);  // 自定义事件的参数类型
        [Description("当点击控件时发生,调用选中当前控件逻辑"), Category("自定义事件")]
        public event select_Handle This_Selected;                                       // 自定义事件名,

        // 在当前控件的某个默认事件中调用自定义事件,此处使用默认的Click事件时,调用自定义的选中事件This_Selected
        private void SplitLineHorizontal_Click(object sender, EventArgs e)
        {
            //DrawColor = Color.Red;      // 定义选中控件时,显示为红色
            if (This_Selected != null) This_Selected(this, new EventArgs(), "其它参数");//调用自定义事件
        }
    }
}

SplitLineHorizontal.Designer.cs

namespace TaskManager
{
    partial class SplitLineHorizontal
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // SplitLineHorizontal
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "SplitLineHorizontal";
            this.Size = new System.Drawing.Size(328, 11);
            this.Click += new System.EventHandler(this.SplitLineHorizontal_Click);
            this.ResumeLayout(false);

        }

        #endregion
    }
}


拓展:类似的自定义控件(界面效果

透明Label控件  

透明PictureBox控件 

透明背景的分割线 

索引选择控件 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值