C#自定义控件开发(1)

自定义控件可以用组件或者用户控件来开发,如果是基于基础控件,进行一些扩展,那么可以使用组件,其它的情况可以使用用户控件。

首先新建一个类库项目,然后添加一个组件,取名为ButtonExtend,再添加一个窗体进行测试:

 

 因为是要在Button的基础上进行开发,所以将继承Component改为继承Button,同时将图片添加到资源中,方便使用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JSControl
{
    public partial class ButtonExtend : Button
    {
        public ButtonExtend()
        {
            InitializeComponent();
            this.Size = new System.Drawing.Size(50,100);//初始化控件大小
        }

        public ButtonExtend(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        #region Filed
        private ButtonPresetImage buttonType;

        [Browsable(true)]
        [Category("ButtonType")]
        [Description("设置图片")]
        public ButtonPresetImage ButtonType
        {
            get { return buttonType; }
            set
            {
                switch (buttonType)
                {
  buttonType = value;
case ButtonPresetImage.None: this.Image = null; this.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; break; case ButtonPresetImage.Check: this.Image = Properties.Resources.check; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Close: this.Image = Properties.Resources.close; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Cancel: this.Image = Properties.Resources.cancel; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Back: this.Image = Properties.Resources.back; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Down: this.Image = Properties.Resources.down; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Go: this.Image = Properties.Resources.go; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Up: this.Image = Properties.Resources.up; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Folder: this.Image = Properties.Resources.folder; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Refresh: this.Image = Properties.Resources.refresh; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Setting: this.Image = Properties.Resources.setting; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.FolderOpen: this.Image = Properties.Resources.folder_open; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.DocumentDelete: this.Image = Properties.Resources.document_delete; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Document: this.Image = Properties.Resources.document; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.DocumentEdit: this.Image = Properties.Resources.document_edit; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Info: this.Image = Properties.Resources.info; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.DocumentAdd: this.Image = Properties.Resources.document_add; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Global: this.Image = Properties.Resources.web; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Calculator: this.Image = Properties.Resources.calculator; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Calendar: this.Image = Properties.Resources.calendar; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; case ButtonPresetImage.Printer: this.Image = Properties.Resources.printer; this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.TextAlign = System.Drawing.ContentAlignment.MiddleRight; break; default: break; } } } #endregion /// <summary> /// 枚举图标类型 /// </summary> public enum ButtonPresetImage { None, Check, Close, Cancel, Back, Down, Go, Up, Folder, Refresh, Setting, FolderOpen, DocumentDelete, Document, DocumentEdit, Info, DocumentAdd, Global, Calculator, Calendar, Printer } } }

生成之后就会在工具箱找到该控件

 

 

同时在属性栏里可以选择图片

 

 

posted @ 2021-06-01 00:01  WellMandala  阅读(889)  评论(0编辑  收藏  举报