1. 程式人生 > WINDOWS開發 >winform 自定義控制元件圓按鈕外掛

winform 自定義控制元件圓按鈕外掛

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 WGMF.TMS.CLIENT.ClientUI
{
public partial class RoundButton : Button
{
private int radius;//半徑

//圓形按鈕的半徑屬性
[CategoryAttribute("佈局"),BrowsableAttribute(true),ReadOnlyAttribute(false)]
public int Radius
{
set
{
radius = value;
this.Height = this.Width = Radius;
}
get
{
return radius;

}
}

private Image imageEnter;
[CategoryAttribute("外觀"),ReadOnlyAttribute(false)]
public Image ImageEnter
{
set
{
imageEnter = value;
}
get
{
return imageEnter;
}

}

private Image imageNormal;
[CategoryAttribute("外觀"),ReadOnlyAttribute(false)]
public Image ImageNormal
{
set
{
imageNormal = value;
BackgroundImage = imageNormal;
}
get
{
return imageNormal;
}
}

//以下程式碼用於在VS中隱藏BackgroundImage屬性,使得只能通過Diameter設定Height和Width
[BrowsableAttribute(false)]
public new Image BackgroundImage
{
get
{
return base.BackgroundImage;
}
set
{
base.BackgroundImage = value;

}
}

//以下程式碼用於在VS中隱藏Size屬性,使得只能通過Diameter設定Height和Width
[BrowsableAttribute(false)]
public new Size Size
{
get
{
return base.Size;
}
set
{
base.Size = value;

}
}

public RoundButton()
{
Radius = 64;
this.Height = this.Width = Radius;
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.BackgroundImage = imageEnter;
this.BackgroundImageLayout = ImageLayout.Stretch;
}

//重寫OnPaint
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(0,Radius,Radius);
this.Region = new Region(path);
}

//重寫OnMouseEnter
//protected override void OnMouseEnter(EventArgs e)
//{
// Graphics g = this.CreateGraphics();
// g.DrawEllipse(new Pen(Color.Blue),this.Width,this.Height);
// g.Dispose();
//}

//重寫OnSizeChanged
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if(Height != Radius)
{
Radius = Width = Height;
}
else if(Width != Radius)
{
Radius = Height = Width;
}

}

//重寫OnMouseEnter
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
BackgroundImage = ImageEnter;
}

//重寫OnMouseLeave
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
BackgroundImage = ImageNormal;
}

}
}