1.CircleButtonというプロジェクトを作る
2.プロジェクトにクラスファイルを追加する
3.クラスファイルにコードを書く
4.一度コンパイル(ビルド)する
5.ツールボックスから対応したコントロールをフォームに張り付ける
6.RoundButtonはプロパティRoundで丸みを調整できる
※貼り付けたコントロールのFlatAppearaanceのBorderSizeを0、FlatStyleをFlatにして下さい
以下クラスファイルに書くコード
EllipseButton.cs
using System; using System.Collections.Generic; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CircleButton { class EllipseButton : Button { protected override void OnPaint(PaintEventArgs pevent) { GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0,0,ClientSize.Width,ClientSize.Height); this.Region = new System.Drawing.Region(gp); base.OnPaint(pevent); } } }
RoundButton.cs
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CircleButton { class RoundButton : Button { protected override void OnPaint(PaintEventArgs pevent) { float r = pr; float x = 0.0f; float y = 0.0f; float w = ClientSize.Width; float h = ClientSize.Height; GraphicsPath gp = new GraphicsPath(); gp.StartFigure(); gp.AddArc(x , y , r , r , 180.0f , 90.0f); gp.AddArc(w - r, y , r , r, 270.0f , 90.0f); gp.AddArc(w - r , h - r , r , r, 0.0f , 90.0f); gp.AddArc(x , h - r , r , r , 90.0f, 90.0f); gp.CloseFigure(); this.Region = new System.Drawing.Region(gp); base.OnPaint(pevent); } private int pr = 50; public int Round { set { pr = value; } get { return pr; } } } }
SideRoundButton.cs
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CircleButton { class SideRoundButton : Button { protected override void OnPaint(PaintEventArgs pevent) { float x = 0.0f; float y = 0.0f; float w = ClientSize.Width; float h = ClientSize.Height; GraphicsPath graphics = new GraphicsPath(); graphics.StartFigure(); graphics.AddArc(x, y, h, h, 90.0f, 180.0f); graphics.AddArc(w - h, y, h, h, 270.0f, 180.0f); graphics.CloseFigure(); this.Region = new System.Drawing.Region(graphics); base.OnPaint(pevent); } } }