winfrom 改變button的形狀爲橢圓形

自定義一個控件,重寫button類,然後使用Region重繪區域

public partial class ShapeButton : Button
    {
        public ShapeButton()
        {
            InitializeComponent();
        }
 
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
 
        bool flag;
        [Description(" 獲取或設置按鈕橢圓效果。"), DefaultValue(false)]
        public bool Circle
        {          
            set
            {
                flag = value;
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(this.ClientRectangle);//圓形
                this.Region = new Region(gp);
                FlatAppearance.BorderSize = 0;//去掉邊框
                FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));//背景顏色
                this.Invalidate();
            }
            get { return flag; }
        }
    }

在winform窗體添加了一個shapeButton,然後在屬性窗體修改Circle屬性爲true,就可以改變button的形狀
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章