C#经典实例----画图程序_第1页
C#经典实例----画图程序_第2页
C#经典实例----画图程序_第3页
C#经典实例----画图程序_第4页
C#经典实例----画图程序_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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(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; oldPoi

温馨提示

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

评论

0/150

提交评论