using System; using System.Windows.Forms; using System.Drawing; public class Hello:Form { public Hello() { this.Paint += new PaintEventHandler(f1_paint); } private void f1_paint(object sender,PaintEventArgs e) { Graphics g = e.Graphics; g.DrawString("你好,C#!",new Font("Verdana",20), new SolidBrush(Color.Tomato),40,40); g.DrawRectangle(new Pen(Color.Pink,3),20,20,150,100); } public static void Main() { Application.Run(new Hello()); } } |
Graphics g = e.Graphics; g.PageUnit = GraphicsUnit.Inch |
ColorDialog cd = new ColorDialog(); |
using System; using System.Drawing; using System.Windows.Forms; public class Clr:Form{ Button b1 = new Button(); TextBox tb = new TextBox(); ColorDialog clg = new ColorDialog(); public Clr(){ b1.Click += new EventHandler(b1_click); b1.Text = "选择颜色"; tb.Location = new Point(50,50); this.Controls.Add(b1); this.Controls.Add(tb); } public void b1_click(object sender, EventArgs e){ clg.ShowDialog(); tb.BackColor = clg.Color; } public static void Main() { Application.Run(new Clr()); } } |
using System; using System.Drawing; using System.Windows.Forms; public class Fonts:Form { Button b1 = new Button(); TextBox tb = new TextBox(); FontDialog flg = new FontDialog(); public Fonts() { b1.Click += new EventHandler(b1_click); b1.Text = "选择字体"; tb.Location = new Point(50,50); this.Controls.Add(b1); this.Controls.Add(tb); } public void b1_click(object sender, EventArgs e) { clg.ShowDialog(); tb.FontName = flg.Font; } public static void Main() { Application.Run(new Fonts()); } } |
using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; public class Drawgra:Form { public Drawgra() { this.Text = "运用画笔示例"; this.Size = new Size(450,400); this.Paint += new PaintEventHandler(Draw_Graphics); } public void Draw_Graphics(object sender,PaintEventArgs e) { Graphics g = e.Graphics; Pen penline = new Pen(Color.Red,5); Pen penellipse = new Pen(Color.Blue,5); Pen penpie = new Pen(Color.Tomato,3); Pen penpolygon = new Pen(Color.Maroon,4); /*DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等风格*/ //以Dash风格画一条直线 penline.DashStyle = DashStyle.Dash; g.DrawLine(penline,50,50,100,200); //以DashDotDot风格画一个椭圆 penellipse.DashStyle = DashStyle.DashDotDot; g.DrawEllipse(penellipse,15,15,50,50); //以Dot风格画一个馅饼图形 penpie.DashStyle = DashStyle.Dot; g.DrawPie(penpie,90,80,140,40,120,100); //以Solid风格画一个多边形 g.DrawPolygon(penpolygon,new Point[]{ new Point(30,140), new Point(270,250), new Point(110,240), new Point(200,170), new Point(70,350), new Point(50,200)}); } public static void Main() { Application.Run(new Drawgra()); } } |
using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; public class Solidbru:Form { public Solidbru() { this.Text = "运用SolidBrush示例"; this.Paint += new PaintEventHandler(Fill_Graph); } public void Fill_Graph(object sender,PaintEventArgs e) { Graphics g = e.Graphics; //创建一把SolidBrush并用它来填充一个矩形区域 SolidBrush sb = new SolidBrush(Color.Pink); g.FillRectangle(sb,50,50,150,150); } public static void Main() { Application.Run(new Solidbru()); } } |
using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; public class Hatchbru:Form { public Hatchbru() { this.Text = "运用HatchBrush示例"; this.Paint += new PaintEventHandler(Fill_Graph); } public void Fill_Graph(object sender,PaintEventArgs e) { Graphics g = e.Graphics; //创建一把HatchBrush并用它来填充一个矩形区域 /*该画刷的HatchStyle有DiagonalCross、 ForwardDiagonal、Horizontal、 Vertical、 Solid等不同风格 */ HatchStyle hs = HatchStyle.Cross; HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red); g.FillRectangle(sb,50,50,150,150); } public static void Main() { Application.Run(new Hatchbru()); } } |
using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; public class LinearGradientbru:Form { public LinearGradientbru() { this.Text = "运用LinearGradientBrush示例"; this.Paint += new PaintEventHandler(Fill_Graph); } public void Fill_Graph(object sender,PaintEventArgs e) { Rectangle r = new Rectangle(500, 300, 100, 100); LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal); e.Graphics.FillRectangle(lb, r); } public static void Main() { Application.Run(new LinearGradientbru()); } } |
using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; public class PathGradientbru:Form { public PathGradientbru() { this.Text = "运用PathGradientBrush示例"; this.Paint += new PaintEventHandler(Fill_Graph); } public void Fill_Graph(object sender,PaintEventArgs e) { e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased; e.Graphics.FillRectangle(backgroundBrush, ClientRectangle); e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle); //先设置好一个路径 GraphicsPath path = new GraphicsPath(new Point[] { new Point(40, 140), new Point(275, 200), new Point(105, 225), new Point(190, 300), new Point(50, 350), new Point(20, 180), }, new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Bezier, (byte)PathPointType.Bezier, (byte)PathPointType.Bezier, (byte)PathPointType.Line, (byte)PathPointType.Line, }); //创建一把PathGradientBrush PathGradientBrush pgb = new PathGradientBrush(path); //设置画刷的周围颜色 pgb.SurroundColors = new Color[] { Color.Green, Color.Yellow, Color.Red, Color.Blue, Color.Orange, Color.White, }; //用画刷进行填充 e.Graphics.FillPath(pgb, path); } public static void Main() { Application.Run(new PathGradientbru()); } } |
using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; public class Texturedbru:Form { Brush bgbrush; public Texturedbru() { //创建一幅图像以供填充椭圆的背景用 Image bgimage = new Bitmap("dotnet.gif"); bgbrush = new TextureBrush(bgimage); this.Paint+=new PaintEventHandler(Text_bru); } public void Text_bru(object sender,PaintEventArgs e) { Graphics g = e.Graphics; g.FillEllipse(bgbrush,50,50,500,300); } public static void Main() { Application.Run(new Texturedbru()); } } |