




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
浅析C#中图形编程(一) 作者: 像Java一样,C提供了一整套相当丰富的类库、方法以及事件以供开发者使用。C还引入了GDI+,它是由GDI演变而来的,具有比GDI更强大的功能而且简化了程序员的编程工作。所以开发者运用这些,就可以很方便的开发出具有强大图形图像功能的应用程序了。本文,笔者就通过一些实例像读者介绍一下C中的图形编程的基本知识。 简单实例: 首先,让我们从例子开始,以下是一个最简单的实例: 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(); 在上面的实例中,我们用到了一个方法:DrawString(),它带有5个参数。同时,我们发现在运用DrawString()方法以前,我们先创建了一个Graphics类型的对象g=e.Graphics,这就说明了在运用任何图形类的方法以前我们必须先创建该类的一个实例化对象。在DrawString()方法后,我们用到了DrawRectangle()方法,其实我们还可以运用其他的方法来画椭圆或是多边形等等。第一个实例还是相当简单易懂的,不是吗?变换图形的度量单位: 在图形编程中,默认的图形度量单位是象素。不过,你可以通过修改PageUnit属性来修改图形的度量单位,可以是英寸或是毫米等。实现方法如下: Graphics g = e.Graphics; g.PageUnit = GraphicsUnit.Inch 操作颜色选择对话框: 在实际运用特别是图形图像编程过程中,我们可能会经常碰到颜色选择对话框(以及下面要提到的字体选择对话框)。使用颜色选择对话框,我们可以让用户来选择系统预定的颜色以及用户自定义的颜色。在使用颜色选择对话框之前,我们必须先创建一个ColorDialog类型的对象: ColorDialog cd = new ColorDialog(); 然后,我们就可以用ShowDialog()方法来显示颜色选择对话框了。之后,就可以通过调用用户的颜色选择进行相关的图形操作了。 以下,我给大家一个实例。该实例中有一个按钮和一个文本框,通过点击按钮可以调出颜色选择对话框,根据用户的颜色选择就可以设置文本框的背景颜色了。 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(); 使用System.Drawing.Drawing2D名字空间: 如果你有一些图形图像编程的经验,那么你一定知道画笔和画刷的概念。它们在图形编程有非常广泛的运用。System.Drawing.Drawing2D名字空间提供了相当强大的功能,能让开发者很容易地操作画笔以及画刷对象。比如,你可以通过设置画笔的DashStyle属性(有Dash、DashDot、Solid等风格)来确定直线的风格。同样,通过运用SolidBrush、HatchBrush、GradientBrush等画笔你可以很轻易地修改被填充区域的外观。比如,你可以用SolidBrush将一个矩形区域用许许多多不同粗细的直线来填充。那么,我们在什么时候运用画笔和画刷呢?就像上面的例子中那样,通常一个图形轮廓(运用DrawXXX()方法)是用画笔对象来实现的,而一个填充区域(运用FillXXX()方法)则是用画刷对象来实现的。 使用画笔对象: 在下面的实例中,我们用到了System.Drawing.Drawing2D名字空间。实例中我们用画笔以不同的风格画了直线、椭圆、馅饼图形、多边形等图形。 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(); 使用画刷对象: 画刷对象是用特定的颜色、模式或是图像来填充一块区域的。总共有四种类型的画刷:SolidBrush(默认的画刷)、HatchBrush、GradientBrush以及TexturedBrush。下面,我们分别给出实例来进行介绍。 1、运用SolidBrush: 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(); 2、运用HatchBrush: 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(); 3、运用GradientBrush: GradientBrush又可分为LinearGradientBrush和PathGradientBrush两种,从它们的名称我们可以知道前者是线性渐变的,而后者则是路径渐变的,因而能创造出更复杂和完美的效果。下面我就给大家分别举例: 1)、运用LinearGradientBrush: 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(); 所得图形如下:2)、运用PathGradientBrush: 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(); 所得图形如下:4、运用TexturedBrush: using System; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; public class Texturedbru:Form Brush bgbru
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030专科医疗市场发展分析及前景趋势与投融资发展机会研究报告
- 企业资料文档管理标准化模板
- 纤维素基生物降解包装-洞察及研究
- 2025年学历类自考学前儿童游戏指导-国际贸易理论与实务参考题库含答案解析(5套试卷)
- 2025年学历类自考儿科护理学(一)-教师职业道德与专业发展参考题库含答案解析(5套试卷)
- 2025年学历类自考中外文学作品导读-企业管理咨询参考题库含答案解析(5套试卷)
- 2025年学历类自考中外教育简史-学前心理学参考题库含答案解析(5套试卷)
- 建材门店转租合同范本
- 样品采购制作合同范本
- 石料开采合同(标准版)
- 层次分析-环境管理法课件
- 三年级下册口算天天100题(A4打印版)
- 上海交通大学学生生存手册
- 《道德与法治》三年级上册教材分析解读课件
- 幼儿园绘本故事:《苏丹的犀角》 课件
- 03第三阶段04印章模型制作
- 英汉互译课件05
- GB∕T 17766-2020 固体矿产资源储量分类
- Q∕SY 06515.1-2016 炼油化工工程电气技术规范 第1部分:通则
- 2010现代领翔nfcg2.0dohc原厂维修手册车身和外部
- 电动汽车充电站建设项目可行性研究报告
评论
0/150
提交评论