用c#写画图形

 

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.

Collapse Copy Code
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:

Collapse Copy Code
      GraphicsPath path = new GraphicsPath();
      path.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
      this.Region = new Region(path);

Step 3

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值