笔刷用Pen类表示,使用Pen后,必须释放资源。如下例
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Pen p = new Pen(Color.Black); g.DrawLine(p, 0, 0, 100, 100); p.Dispose(); }
可以设置Pen的线宽和对齐方式
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Pen p = new Pen(Color.Black,3); p.Alignment=.PenAlignment.Center; g.DrawRectangle(p,3,3,8,7); p.Dispose(); }
设置画笔的点划线属性用DashStyle属性
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Pen p = new Pen(Color.Black,2); p.DashStyle = DashStyle.Dash; g.DrawLine(p, 3, 3, 100, 3); p.Dispose(); }