C# 学习新建一个自己的button

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


namespace UserCtr
{
    public partial class UserCtrBtn : Button
    {
        private bool m_bChecked = false;//按钮被按下
        private Image m_PressDown_image = null;
        [Description("按下图片"), Category("特有属性")]
        public Image PressDownImage
        {
            get
            {
                return m_PressDown_image;
            }
            set
            {
                m_PressDown_image = value;
            }
        }


        private Image m_PressOn_Image = null;
        [Description("弹起图片"), Category("特有属性")]




        public Image PressOn_Image
        {
            get
            {
                return m_PressOn_Image;
            }
            set
            {
                m_PressOn_Image = value;
            }
        }
        public UserCtrBtn()
        {
            InitializeComponent();
        }
        // protected override void OnPaint(PaintEventArgs e)
        protected void OnPaint1(PaintEventArgs e)
        {
            //Bitmap bitMapOn = null;
            //Bitmap bitMapOff = null;




            //bitMapOn = global::UserCtr.Properties.Resources.btncheckon1;
            //bitMapOff = global::UserCtr.Properties.Resources.btncheckoff1;






            //Graphics g = e.Graphics;
            //Rectangle rec = new Rectangle(0, 0, this.Size.Width, this.Size.Height);


            //if (m_bChecked)
            //{
            //    g.DrawImage(bitMapOn, rec);
            //}
            //else
            //{
            //    g.DrawImage(bitMapOff, rec);
            //}
        }
        public UserCtrBtn(IContainer container)
        {
            InitializeComponent();
            container.Add(this);
            
            this.BackgroundImage = m_PressOn_Image;
            this.BackgroundImageLayout = ImageLayout.Stretch;
            this.BackColor = Color.Transparent;
            //设置Style支持透明背景色并且双缓冲
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.BackColor = Color.Transparent;


            this.Cursor = Cursors.Hand;
            //this.Size = new Size(87, 27);




            this.HandleCreated += (EventHandler)delegate (object sender, EventArgs e)
             {
                 //this.BackgroundImage = m_PressOn_Image;
             };


            this.MouseClick += (MouseEventHandler)delegate (object sender, MouseEventArgs e)
            {
                m_bChecked = !m_bChecked;
                //this.Invalidate();
                if (m_bChecked)
                    this.BackgroundImage = m_PressDown_image;
                else
                    this.BackgroundImage = m_PressOn_Image;
            };


            


        }
    }
}

C  #   三种控件 一般步骤:  https://www.cnblogs.com/rainbow70626/p/4589907.html
C  #   添加到工具栏问题  https://www.cnblogs.com/daoge/p/4505660.html

猜你喜欢

转载自blog.csdn.net/wuan584974722/article/details/79851118