每復制一個方法都要綁定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);
}