基于C#开发的扩展按钮控件

  最近在准备一套自定义控件开发的课程,下面将第一个做的按钮控件分享给大家。

其实这个控件属于自定义控件中的扩展控件,与组合控件和GDI+开发的控件不同,这个控件是继承原生的Button,

这个控件的目的在于把常用的图标集成到控件中,使用会更方便。大家都知道,软件开发UI设计是必不可免的一部分,

所以一个带有图标的按钮会比一个光秃秃的按钮要增色不少,但是大家很多时候不想或者没法找到图标,如果集成在

控件里,是不是会方便很多呢?其实实现原理很简单:

  实现效果如下图所示:

 

 

接着上代码:

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

namespace XktControl
{
    public partial class xktButton : Button
    {
        public xktButton()
        {
            InitializeComponent();        
            this.PreSetImage = ButtonPresetImage.None;
            this.Size = new Size(100, 32);

        }

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

            InitializeComponent();         
        }

        #region Enum
        /// <summary>
        /// EasyButton类型的类型
        /// </summary>
        public enum ButtonPresetImage
        {
            /// <summary>
            /// 没有图标
            /// </summary>
            None,
            /// <summary>
            /// 确认图标
            /// </summary>
            Check,
            /// <summary>
            /// 关闭图标
            /// </summary>
            Close,
            /// <summary>
            /// 取消图标
            /// </summary>
            Cancel,
            /// <summary>
            /// 退后图标
            /// </summary>
            Back,
            /// <summary>
            /// 向下图标
            /// </summary>
            Down,
            /// <summary>
            /// 前进图标
            /// </summary>
            Go,
            /// <summary>
            /// 向上图标
            /// </summary>
            Up,
            /// <summary>
            /// 文件夹图标
            /// </summary>
            Folder,
            /// <summary>
            /// 刷新图标
            /// </summary>
            Refresh,
            /// <summary>
            /// 设置图标
            /// </summary>
            Setting,
            /// <summary>
            /// 文件打开图标
            /// </summary>
            FolderOpen,
            /// <summary>
            /// 文件删除图标
            /// </summary>
            DocumentDelete,
            /// <summary>
            /// 文件图标
            /// </summary>
            Document,
            /// <summary>
            /// 文件编辑图标
            /// </summary>
            DocumentEdit,
            /// <summary>
            /// 信息图标
            /// </summary>
            Info,
            /// <summary>
            /// 文件添加图标
            /// </summary>
            DocumentAdd,
            /// <summary>
            /// 全局图标
            /// </summary>
            Gobal,
            /// <summary>
            /// 计算图标
            /// </summary>
            Calculator,
            /// <summary>
            /// 日期图标
            /// </summary>
            Calendar,
            /// <summary>
            /// 打印图标
            /// </summary>
            Printer
        }

        #endregion


        #region Fields
        ButtonPresetImage _buttontype;
        #endregion


        #region Properties
        /// <summary>
        /// To show different Image and Text
        /// </summary>
        [Browsable(true)]
        [Description("显示不同的图像")]
        [Category("自定义属性")]
        public ButtonPresetImage PreSetImage
        {
            get
            {
                return _buttontype;
            }

            set
            {
                _buttontype = value;
                switch (_buttontype)
                {
                    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;
                        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.Gobal:
                        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;
                }

                this.Invalidate();
            }
        }


        #endregion

    }
}

  如果大家还有什么不明白的地方,可以关注一下微信公众号:dotNet工控上位机

posted on 2019-09-06 09:17  付工上位机  阅读(2357)  评论(1编辑  收藏  举报

导航