C#經典實例----畫圖程序_第1頁
C#經典實例----畫圖程序_第2頁
C#經典實例----畫圖程序_第3頁
C#經典實例----畫圖程序_第4頁
C#經典實例----畫圖程序_第5頁
已閱讀5頁,還剩25頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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; /創(chuàng)建一個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. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論