(C#)用Brush画图(myDrawBrushA)
//实现功能:用Brush画图
//
1)创建的渐变色Brush(LinearGradientBrush,用完后应及时Dispose.)
//
2)用Brushes绘图.(无须创建Brush)
// 3)创建自定义颜色的SolidBrush
//
4)画矩形,椭圆,扇形,多边形
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;//另添加
namespace myDrawBrushA
{
public
partial class Form1 :
Form
{
public
Form1()
{
InitializeComponent();
SetStyle(ControlStyles.Opaque,
true);//本例由于在OnPaint()中将客户区先涂上白色后再绘画,所以此句可有可无。
}
protected
override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
Graphics g =
e.Graphics;
g.FillRectangle(Brushes.White, ClientRectangle);//全部客户区涂上白色
g.FillRectangle(Brushes.Red, new Rectangle(20, 20, 80, 80));//红色正方形
//以下创建一个Brush画渐变色正方形
Brush
linearGradientBrush = new
LinearGradientBrush(
new Rectangle(20, 110, 80,80), Color.Blue, Color.White,45);//创建渐变色brush,渐变方向:45-左上至右下;90-向下:......
g.FillRectangle(linearGradientBrush, new Rectangle(20, 110, 80, 80));//画渐变色正方形
linearGradientBrush.Dispose();//注意及时删除渐变色brush
//以下用Brushes(不要创建Brush)
g.FillEllipse(Brushes.Aquamarine, new Rectangle(110,20, 100, 60));//椭圆(前两个参数是外接矩形左上角坐标)
g.FillPie(Brushes.Chartreuse, new Rectangle(110, 110, 80, 80), 90,
270);//扇形(207为圆心角度数,90为扇形旋转的角度
g.FillPolygon(Brushes.BlueViolet, new Point[]{
new Point(220,10),
new Point(300,10),
new Point(250,40),
new Point(350,80),
new Point(230,100)
});
//多边形
//以下用创建自定义颜色的SolidBrush画圆
SolidBrush
trnsRedBrush = new SolidBrush(Color.FromArgb(120, 255, 0,
0));
SolidBrush
trnsGreenBrush = new SolidBrush(Color.FromArgb(120, 0, 255,
0));
SolidBrush
trnsBlueBrush = new SolidBrush(Color.FromArgb(120, 0, 0,
255));
// Base and height of
the triangle that is used to position the
// circles. Each vertex
of the triangle is at the center of one of
the
// 3 circles. The base
is equal to the diameter of the circles.
float triBase =
50;
float triHeight =
(float)Math.Sqrt(3 * (triBase * triBase)
/4);
// Coordinates of first
circle's bounding rectangle.
float x1 =
200;
float y1 =
100;
// Fill 3 over-lapping
circles. Each circle is a different color.
g.FillEllipse(trnsRedBrush, x1, y1, 2 * triHeight, 2 *
triHeight);
g.FillEllipse(trnsGreenBrush, x1 + triBase / 2, y1 +
triHeight,
2 * triHeight, 2 * triHeight);
g.FillEllipse(trnsBlueBrush, x1 + triBase, y1, 2 * triHeight, 2 *
triHeight);
}
}
}