每复制一个方法都要绑定Paint事件
一、创建Windows窗体应用程序,要求如下:(源代码+运行界面,缺少任一项为0分,源代码只需粘贴绘制图形代码所在的方法,不用粘贴太多)
例如:
(1)添加一个窗体Form1,绘制两个矩形,一个为蓝色边框,另一个为红色边框,如下图所示。
using System;
using System.Windows.Forms;
using System.Drawing;
private void Form1_Paint(object sender,PaintEventArgs e){/*Graphics obj = this.CreateGraphics();int x, y, w, h;x = 10; y = 10; w = 150; h = 150;obj.DrawLine(Pens.Blue, x, y, x+w, y);obj.DrawLine(Pens.Blue, x, y, x, y+h);obj.DrawLine(Pens.Blue, x + w, y, x + w, y + h);obj.DrawLine(Pens.Blue, x, y + h, x + w, y + h);Graphics obj1 = this.CreateGraphics();int x1, y1, w1, h1;x1 = 50; y1 = 50; w1 = 150; h1 = 150;obj.DrawLine(Pens.Red, x1, y1, x1 + w1, y1);obj.DrawLine(Pens.Red, x1, y1, x1, y1 + h1);obj.DrawLine(Pens.Red, x1 + w1, y1, x1 + w1, y1 + h1);obj.DrawLine(Pens.Red, x1, y1 + h1, x1 + w1, y1 + h1);*/Graphics gobj = this.CreateGraphics();Pen bluePen = new Pen(Color.Blue, 5);Pen redPen = new Pen(Color.Red, 5);Rectangle myRectangle = new Rectangle(50, 50, 100, 80);gobj.DrawRectangle(bluePen, 30, 20, 100, 80);gobj.DrawRectangle(redPen, myRectangle);}
(2)添加一个窗体Form2,绘制两个填充饼形构成一个椭圆,如下图所示。
using System;
using System.Drawing;
using System.Windows.Forms;
private void Form2_Paint(object sender,PaintEventArgs s) {/*Graphics obj = this.CreateGraphics();Rectangle rec1 = new Rectangle(20, 20, 150, 90);obj.FillPie(Brushes.Red, rec1, 300, 60);//1点方向顺时针60°Rectangle rec2 = new Rectangle(20, 20, 150, 90);obj.FillPie(Brushes.Blue,rec2,0,300);//3点方向顺时针300°*/Graphics gobj = this.CreateGraphics();gobj.FillPie(Brushes.Blue, 30, 30, 130, 80, 0, 300);gobj.FillPie(Brushes.Red, 30, 30, 130, 80, 0, -60);}
(3)添加一个窗体Form3,绘制一个带边的填充椭圆,如下图所示。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
private void Form3_Paint(object sender, PaintEventArgs e)
{Graphics gobj = this.CreateGraphics();HatchBrush myBrush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.Blue, Color.Green);Pen myPen = new Pen(Color.Red, 8);gobj.DrawEllipse(myPen, 20, 20, 150, 100);gobj.FillEllipse(myBrush, 20, 20, 150, 100);
}
(4)添加一个窗体Form4,用不同的大小字体绘制3个文本,如下图所示。
using System;
using System.Drawing;
using System.Windows.Forms;
private void Form4_Paint(object sender, PaintEventArgs s)//加引用using System.Drawing.Drawing2D;{/*Graphics obj = this.CreateGraphics();StringFormat sf1 = new StringFormat();//StringFormat sf2 = new StringFormat();Font f1 = new Font("隶书",20,FontStyle.Bold);Font f2 = new Font("隶书", 5, FontStyle.Bold);HatchBrush obj1 = new HatchBrush(HatchStyle.Vertical,Color.Blue,Color.Green);SolidBrush obj2 = new SolidBrush(Color.Red);sf1.Alignment = StringAlignment.Far;//sf2.FormatFlags = StringFormatFlags.DirectionVertical;//竖直obj.DrawString("中华人民共和国",f1,obj1,220,15,sf1);obj.DrawString("中华人民共和国", f2, obj1, 300, 100, sf1);//obj.DrawString("中华人民共和国", f, obj2, 100, 5, sf1);*/int i;StringFormat strFormat;Font strFont;SolidBrush strBrush;Graphics gobj = this.CreateGraphics();for (i = 10; i <= 24; i += 6){strFormat = new StringFormat();strFont = new System.Drawing.Font("隶书", i);strBrush = new SolidBrush(Color.Red);gobj.DrawString("中华人民共和国", strFont, strBrush, 2 * i, 3 * i, strFormat);}}
二、创建Windows窗体应用程序,添加一个窗体Form1,添加3个命令按钮,单击时分别在窗体上画一条直线、一个形状和一个文本,如下图所示。(源代码+运行界面,缺少一项为0分,源代码只需粘贴绘制图形代码所在的方法,不用粘贴太多)
private void button1_Click(object sender, EventArgs e)
{Pen myPen = new Pen(System.Drawing.Color.Blue);Graphics gobj = this.CreateGraphics();gobj.DrawLine(myPen, 30, 30, 120, 120);
}private void button2_Click(object sender, EventArgs e)
{Pen myPen = new Pen(System.Drawing.Color.Blue);Graphics gobj = this.CreateGraphics();gobj.DrawEllipse(myPen, new Rectangle(30, 30, 100, 100));
}private void button3_Click(object sender, EventArgs e)
{Graphics gobj = this.CreateGraphics();string drawString = "使用DrawString方法";Font myFont = new Font("黑体", 16);Brush b = new SolidBrush(System.Drawing.Color.Blue);float x = 30.0F;float y = 30.0F;StringFormat myFormat = new StringFormat();gobj.DrawString(drawString, myFont, b, x, y, myFormat);
}