C#经典实例----画图程序

上传人:枕*** 文档编号:139743275 上传时间:2022-08-22 格式:DOC 页数:30 大小:190KB
收藏 版权申诉 举报 下载
C#经典实例----画图程序_第1页
第1页 / 共30页
C#经典实例----画图程序_第2页
第2页 / 共30页
C#经典实例----画图程序_第3页
第3页 / 共30页
资源描述:

《C#经典实例----画图程序》由会员分享,可在线阅读,更多相关《C#经典实例----画图程序(30页珍藏版)》请在装配图网上搜索。

1、这是一种简朴旳画图程序其源码是:using System.Drawing;using System;using System.Windows.Forms;using System.Drawing.Imaging;namespace WindowsApplication1 partial class Form1 / / 必需旳设计器变量。 / private System.ComponentModel.IContainer components = null; / / 清理所有正在使用旳资源。 / / 假如应释放托管资源,为 true;否则为 false。 protected override

2、void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); public Form1() InitializeComponent(); #region Windows 窗体设计器生成旳代码 / / 设计器支持所需旳措施 - 不要 / 使用代码编辑器修改此措施旳内容。 / private void InitializeComponent() ponents = new System.ComponentModel.Container(

3、); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.文献ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.Open = new System.Windows.Forms.ToolStripMenuItem(); this.NEW = new System.Windows.Forms.ToolStripMenuItem(); this.saveFile = new System.Windows.Forms.ToolStripMen

4、uItem(); this.Exist = new System.Windows.Forms.ToolStripMenuItem(); this.编辑颜色ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.有关ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolTip1 = new System.Windows.Forms.ToolTip(ponents); this.menuStrip2 = new Sys

5、tem.Windows.Forms.MenuStrip(); this.画笔工具ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.画直线ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.画椭圆ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.画矩形ToolStripMenuItem = new System.Windows

6、.Forms.ToolStripMenuItem(); this.橡皮擦ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.鼠标ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveFiles = new System.Windows.Forms.SaveFileDialog(); this.openFiles = new System.Windows.Forms.OpenFileDialog(); this.

7、panel1 = new System.Windows.Forms.Panel(); this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); this.menuStrip2.SuspendLayout(); this.panel1.SuspendLayout(); this.SuspendLayout(); / / menuStrip1 / this.menuStrip1.Items.Add

8、Range(new System.Windows.Forms.ToolStripItem this.文献ToolStripMenuItem, this.编辑颜色ToolStripMenuItem, this.有关ToolStripMenuItem); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = menuStrip1; this.menuStrip1.Size = new System.Drawing.Size(305, 24); this.menuStrip1.TabInde

9、x = 0; this.menuStrip1.Text = menuStrip1; / / 文献ToolStripMenuItem / this.文献ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem this.Open, this.NEW, this.saveFile, this.Exist); this.文献ToolStripMenuItem.Name = 文献ToolStripMenuItem; this.文献ToolStripMenuItem.Size = new System.

10、Drawing.Size(43, 20); this.文献ToolStripMenuItem.Text = 文献; / / Open / this.Open.Name = Open; this.Open.Size = new System.Drawing.Size(98, 22); this.Open.Text = 打开; this.Open.Click += new System.EventHandler(this.Open_Click); / / NEW / this.NEW.Name = NEW; this.NEW.Size = new System.Drawing.Size(98, 2

11、2); this.NEW.Text = 新建; this.NEW.Click += new System.EventHandler(this.NEW_Click); / / saveFile / this.saveFile.Name = saveFile; this.saveFile.Size = new System.Drawing.Size(98, 22); this.saveFile.Text = 保留; this.saveFile.Click += new System.EventHandler(this.saveFile_Click); / / Exist / this.Exist.

12、Name = Exist; this.Exist.Size = new System.Drawing.Size(98, 22); this.Exist.Text = 退出; this.Exist.Click+= new System.EventHandler(this. Exist_Click); / / 编辑颜色ToolStripMenuItem / this.编辑颜色ToolStripMenuItem.Name = 编辑颜色ToolStripMenuItem; this.编辑颜色ToolStripMenuItem.Size = new System.Drawing.Size(67, 20)

13、; this.编辑颜色ToolStripMenuItem.Text = 编辑颜色; this.编辑颜色ToolStripMenuItem.Click += new System.EventHandler(this.编辑颜色ToolStripMenuItem_Click); / / 有关ToolStripMenuItem / this.有关ToolStripMenuItem.Name = 有关ToolStripMenuItem; this.有关ToolStripMenuItem.Size = new System.Drawing.Size(43, 20); this.有关ToolStripMen

14、uItem.Text = 有关; this.有关ToolStripMenuItem.Click += new System.EventHandler(this.有关ToolStripMenuItem_Click); / / toolTip1 / this.toolTip1.ShowAlways = true; / / menuStrip2 / this.menuStrip2.Dock = System.Windows.Forms.DockStyle.Bottom; this.menuStrip2.Enabled = false; this.menuStrip2.GripStyle = Syst

15、em.Windows.Forms.ToolStripGripStyle.Visible; this.menuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem this.画笔工具ToolStripMenuItem, this.画直线ToolStripMenuItem, this.画椭圆ToolStripMenuItem, this.画矩形ToolStripMenuItem, this.橡皮擦ToolStripMenuItem, this.鼠标ToolStripMenuItem); this.menuStrip2.Locati

16、on = new System.Drawing.Point(0, 332); this.menuStrip2.Name = menuStrip2; this.menuStrip2.Size = new System.Drawing.Size(305, 24); this.menuStrip2.TabIndex = 1; this.menuStrip2.Text = menuStrip2; this.menuStrip2.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.menuStrip2

17、_ItemClicked); / / 画笔工具ToolStripMenuItem / this.画笔工具ToolStripMenuItem.Name = 画笔工具ToolStripMenuItem; this.画笔工具ToolStripMenuItem.Size = new System.Drawing.Size(43, 20); this.画笔工具ToolStripMenuItem.Text = 画笔; / / 画直线ToolStripMenuItem / this.画直线ToolStripMenuItem.Name = 画直线ToolStripMenuItem; this.画直线ToolS

18、tripMenuItem.Size = new System.Drawing.Size(55, 20); this.画直线ToolStripMenuItem.Text = 画直线; / / 画椭圆ToolStripMenuItem / this.画椭圆ToolStripMenuItem.Name = 画椭圆ToolStripMenuItem; this.画椭圆ToolStripMenuItem.Size = new System.Drawing.Size(55, 20); this.画椭圆ToolStripMenuItem.Text = 画椭圆; / / 画矩形ToolStripMenuIte

19、m / this.画矩形ToolStripMenuItem.Name = 画矩形ToolStripMenuItem; this.画矩形ToolStripMenuItem.Size = new System.Drawing.Size(55, 20); this.画矩形ToolStripMenuItem.Text = 画矩形; / / 橡皮擦ToolStripMenuItem / this.橡皮擦ToolStripMenuItem.Name = 橡皮擦ToolStripMenuItem; this.橡皮擦ToolStripMenuItem.Size = new System.Drawing.Siz

20、e(55, 20); this.橡皮擦ToolStripMenuItem.Text = 橡皮擦; / / 鼠标ToolStripMenuItem / this.鼠标ToolStripMenuItem.Name = 鼠标ToolStripMenuItem; this.鼠标ToolStripMenuItem.Size = new System.Drawing.Size(43, 20); this.鼠标ToolStripMenuItem.Text = 鼠标; / / openFiles / this.openFiles.FileName = 图像文献; / / panel1 / this.panel

21、1.Controls.Add(this.label1); this.panel1.Controls.Add(this.button1); this.panel1.Location = new System.Drawing.Point(12, 59); this.panel1.Name = panel1; this.panel1.Size = new System.Drawing.Size(281, 131); this.panel1.TabIndex = 2; this.panel1.Visible = false; / / label1 / this.label1.AutoSize = tr

22、ue; this.label1.Location = new System.Drawing.Point(3, 29); this.label1.Name = label1; this.label1.Size = new System.Drawing.Size(275, 12); this.label1.TabIndex = 1; this.label1.Text = 更多资料到查看?/zhouxiaoming; / / button1 / this.button1.Location = new System.Drawing.Point(121, 105); this.button1.Name

23、= button1; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = 关闭; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); / / Form1 / this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

24、 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(305, 356); this.Controls.Add(this.panel1); this.Controls.Add(this.menuStrip1); this.Controls.Add(this.menuStrip2); this.Cursor = System.Windows.Forms.Cursors.Default; this.MainMenuStrip = this.me

25、nuStrip1; this.Name = Form1; this.Text = Form1; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged); this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp); this.MouseMove += new Syste

26、m.Windows.Forms.MouseEventHandler(this.Form1_MouseMove); this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); this.menuStrip2.ResumeLayout(false); this.menuStrip2.PerformLayout(); this.panel1.ResumeL

27、ayout(false); this.panel1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); #endregion private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem 文献ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem Open; private System.Windows.For

28、ms.ToolStripMenuItem NEW; private System.Windows.Forms.ToolStripMenuItem saveFile; private System.Windows.Forms.ToolStripMenuItem Exist; private System.Windows.Forms.ToolStripMenuItem 编辑颜色ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 有关ToolStripMenuItem; private System.Windows.Fo

29、rms.ToolTip toolTip1; private System.Windows.Forms.MenuStrip menuStrip2; private System.Windows.Forms.ToolStripMenuItem 画笔工具ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 画直线ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 画矩形ToolStripMenuItem; private System.Wind

30、ows.Forms.ToolStripMenuItem 画椭圆ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 橡皮擦ToolStripMenuItem; private System.Windows.Forms.SaveFileDialog saveFiles; private System.Windows.Forms.OpenFileDialog openFiles; private System.Windows.Forms.ToolStripMenuItem 鼠标ToolStripMenuItem; pri

31、vate System.Windows.Forms.Panel panel1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; / / 应用程序旳主入口点。 / STAThread static void Main() Application.Run(new Form1(); /布尔型变量,与否正在绘图 private bool isDrawing = false; /绘图时记录鼠标旳位置 private Point startPoint, oldPoint; /枚举

32、类型,多种绘图工具 private enum drawTools Pen = 0, Line, Ellipse, Rectangle, String, Rubber, None ; /目前使用旳工具 private drawTools drawTool = drawTools.None; private string editFileName; private Image theImage; private Graphics ig; private Color foreColor = Color.Black; private Color backColor = Color.White; pri

33、vate int i = 0;/在保留文献是用来标识文献 private void Open_Click(object sender, EventArgs e) openFiles.Filter = Image Files(*.bmp;*.wmf;*.ico;*.cur;*.jgp)|*.bmp;*.wmf;*.ico;*.cur;*.jpg; openFiles.Multiselect = false; if (openFiles.ShowDialog() = DialogResult.OK) /修改窗口标题 this.Text = openFiles.FileName; editFileN

34、ame = openFiles.FileName; theImage = Image.FromFile(openFiles.FileName); Graphics g = this.CreateGraphics(); g.DrawImage(theImage, this.ClientRectangle); ig = Graphics.FromImage(theImage); ig.DrawImage(theImage, this.ClientRectangle); menuStrip2.Enabled = true; private void NEW_Click(object sender,

35、EventArgs e) Graphics g = this.CreateGraphics(); g.Clear(backColor); menuStrip2.Enabled = true; /创立一种Bitmap theImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); editFileName = 新建文献; /修改窗口标题 this.Text = editFileName; ig = Graphics.FromImage(theImage); ig.Clear(backColor); p

36、rivate void saveFile_Click(object sender, EventArgs e) saveFiles.Filter = 图像(*.bmp)|*.bmp; saveFiles.FileName = editFileName; if (saveFiles.ShowDialog() = DialogResult.OK) theImage.Save(saveFiles.FileName, ImageFormat.Bmp); this.Text = saveFiles.FileName; editFileName = saveFiles.FileName; i+; priva

37、te void 编辑颜色ToolStripMenuItem_Click(object sender, EventArgs e) ColorDialog colorDg = new ColorDialog(); if (colorDg.ShowDialog() = DialogResult.OK) foreColor = colorDg.Color; private void menuStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e) switch (menuStrip2.Items.IndexOf(e.Click

38、edItem) case 0: drawTool = drawTools.Pen; this.Cursor = Cursors.Hand; break; case 1: drawTool = drawTools.Line; this.Cursor = Cursors.Cross; break; case 2: drawTool = drawTools.Ellipse; this.Cursor = Cursors.Cross; break; case 3: drawTool = drawTools.Rectangle; this.Cursor = Cursors.Cross; break; ca

39、se 4: drawTool = drawTools.Rubber; this.Cursor = Cursors.No; break; case 5: drawTool = drawTools.None; this.Cursor = Cursors.Default; break; /鼠标按下 private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) if (e.Button = MouseButtons.Left) if (isDrawing = !isDrawing) = true)

40、startPoint = new Point(e.X, e.Y); oldPoint = new Point(e.X, e.Y); /鼠标移动 private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) Graphics g; g = this.CreateGraphics(); if (isDrawing) switch (drawTool) case drawTools.None: break; case drawTools.Pen: /从上一种点到目前点绘制线段 g.DrawLine

41、(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y); ig.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y); oldPoint.X = e.X; oldPoint.Y = e.Y; break; case drawTools.Line: /首先恢复本次操作之前旳图像,然后再添加Line this.Form1_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle); g.

42、DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y); break; case drawTools.Ellipse: /首先恢复本次操作之前旳图像,然后再添加Ellipse this.Form1_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle); g.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y

43、 - startPoint.Y); break; case drawTools.Rectangle: /首先恢复本次操作之前旳图像,然后再添加Rectangle this.Form1_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle); g.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y); break; case drawTools.R

44、ubber: /用背景色绘制宽线段 g.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y); ig.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y); oldPoint.X = e.X; oldPoint.Y = e.Y; break; /鼠标松开 private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) isDrawing = false;

45、switch (drawTool) case drawTools.Line: ig.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y); break; case drawTools.Ellipse: ig.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y); break; case drawTools.Rectangle: ig.DrawRectangle(new Pen(foreColo

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!