当前位置: 首页 > 编程语言 > .NET综合 > 正文

winform重绘

时间:2017-02-25

1.重绘文字

#多行文字

a.先定义一个矩形

Rectangle p1 = new Rectangle(10, 0, 200, this.Height);
Rectangle p2 = new Rectangle(210, 0, 200, this.Height);
Rectangle p3 = new Rectangle(410, 0, 100, this.Height);

b.在矩形中写入文字

TextRenderer.DrawText(g,name,Font,p1,ForeColor,TextFormatFlags.HorizontalCenter|TextFormatFlags.VerticalCenter);
TextRenderer.DrawText(g, time, Font, p2, ForeColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
TextRenderer.DrawText(g, content, Font, p3, ForeColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);

#单行文字

a.先定义一个点

Point P1 = new Point(this.Width - 100, this.Height - 100);

b.在点后写入文字

TextRenderer.DrawText(g, "dsadhskldhashdalks", Font, P1, Color.YellowGreen);

#TextFormatFlags.HorizontalCenter将边框内的文本水平居中对齐 TextFormatFlags.VerticalCenter在边框内部垂直居中对齐文本

 

2.画线

#画横线

g.DrawLine(new Pen(Color.Brown), 0, this.Height-1, this.Width, this.Height-1);//画横线铺满,对于高度不变,横坐标从0到当前宽度,当前高度从-1到-1,就是画出一像素的高度

#画竖线

g.DrawLine(new Pen(Color.Purple), this.Width - 15, 0, this.Width - 15, this.Height - 14);//从(-15,0)到(-15,-14)绘制一条竖线,this.Width - 15-----当前宽度从右边减去15像素处开始,this.Height - 14------当前高度从下边减去14像素处开始

#画虚线

private void panel1_Paint(object sender, PaintEventArgs e)
{
Control P = (Control)sender;
Pen pen = new Pen(Color.FromArgb(255, 0, 0), 5);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;//虚线的样式
pen.DashPattern = new float[] { 2, 2 };//设置虚线中实点和空白区域之间的间隔
Graphics g = e.Graphics;
g.DrawLine(pen, 0, 0, 0, P.Height - 1);
}

#对panel1重绘,this:当前页面为panel1所在的页面

来源: http://www.cnblogs.com/12jh23/archive/2017/02/24/6439722.html