La Code
Time to code. In the following, I describe briefly the steps in which I developed this button.
Step 1
Simple round button. Visuals done. Behaviorally similar to a rectangular button. We create one pen and one brush. The pen, to draw the boundary ellipse/ circle; the brush: to fill interior of the ellipse.
constructor:
{
_pen = new Pen(_color);
_brush = new SolidBrush(Color.FromKnownColor (KnownColor.Control));
_brushInside = new SolidBrush(_color);
}
// OnPaint…
protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics;//创建图形对象
g.FillRectangle(_brush, 0, 0, ClientSize.Width, ClientSize.Height);//用brush填充矩形
g.DrawEllipse(_pen, 0, 0, ClientSize.Width, ClientSize.Height);//画椭圆
g.FillEllipse(_brushInside, 0, 0, ClientSize.Width, ClientSize.Height);
}
Additionally, you want to expose a color property that the end user can populate in design mode.
Step 2
The implementation in Step 1 has a bug: if you click at a point which lies outside the bounding circle but inside the bounding rectangle, that gets interpreted as a click.
To fix that, we add the following code in OnPaint
to set the window region correctly:
GraphicsPath path = new GraphicsPath();
path.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
this.Region = new Region(path);
Step 3