Graphics graphics = e.Graphics;
//繪製實線
using (Pen pen = new Pen(Color.Black, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; //實線
graphics.DrawLine(pen,0,10,100,10);
}
//畫出虛線
using (Pen pen = new Pen(Color.Black, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虛線
graphics.DrawLine(pen, 0, 15, 100, 15);
}
//畫出矩形
using (Pen pen = new Pen(Color.Black, 2))
{
Rectangle rectangle = new Rectangle(2,30,100,50);
graphics.DrawRectangle(pen,rectangle);
}
//繪製字符串
string str = "http://61office.cn/";
graphics.DrawString(str, this.Font, Brushes.Black, 2, 100);
//測量字符串的高度和寬度
SizeF szfTitle = graphics.MeasureString(str, this.Font);
graphics.DrawString(str, this.Font, Brushes.Black, 2, 200-(int)szfTitle.Height);
graphics.DrawString("該字符串高度:"+szfTitle.Height+" 字符串寬度:"+szfTitle.Width, this.Font, Brushes.Black, 2, 200);
//繪製圓
using(Pen pen = new Pen(Color.Black,2))
{
//圓
graphics.DrawEllipse(pen,2,230,100,100); //在畫板上畫橢圓,起始座標爲(10,10),外接矩形的寬爲100,高爲100 此時就是一個圓
//橢圓
graphics.DrawEllipse(pen, 2, 400, 100, 50); //在畫板上畫橢圓,起始座標爲(10,10),外接矩形的寬爲100,高爲50 此時就是一個橢圓
}
我這裏繪製的是panel的邊框,須要減去線寬。ide
using (Pen pen = new Pen(Color.LightGray, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虛線
e.Graphics.DrawRectangle(pen, pen.Width, pen.Width, panel1.Width-2*pen.Width, panel1.Height - 2*pen.Width);
}