画图软件设计
2009-3-29
namespace WindowsApplication15
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public bool isLine;
public bool isLines;
public bool isTEllipse;
public bool isYEllipse;
public bool isText;
public bool isRect;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
FormChild form = new FormChild(this);
form.MdiParent = this;
form.Text = "画图" + this.MdiChildren.Length.ToString();
form.Show();
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch(e.Button.Text)
{
case "直线":
this.isLine = true;
this.isLines = false;
this.isYEllipse = false;
this.isTEllipse = false;
this.isRect = false;
this.isText = false;
break;
case "折线":
this.isLine = false;
this.isLines = true;
this.isYEllipse = false;
this.isTEllipse = false;
this.isRect = false;
this.isText = false;
break;
case "画圆":
this.isLine = false;
this.isLines = false;
this.isYEllipse = true;
this.isTEllipse = false;
this.isRect = false;
this.isText = false;
break;
case "椭圆":
this.isLine = false;
this.isLines = false;
this.isYEllipse = false;
this.isTEllipse = true;
this.isRect = false;
this.isText = false;
break;
case "矩形":
this.isLine = false;
this.isLines = false;
this.isYEllipse = false;
this.isTEllipse = false;
this.isRect = true;
this.isText = false;
break;
case "文字":
this.isLine = false;
this.isLines = false;
this.isYEllipse = false;
this.isTEllipse = false;
this.isRect = false;
this.isText = true;
break;
}
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
if(this.ActiveMdiChild != null)
{
SaveFileDialog savedialog = new SaveFileDialog();
savedialog.Filter = "(*.bmp)|*.bmp|(*.jpg)|*.jpg";
if(savedialog.ShowDialog() == DialogResult.OK)
{
FormChild form = this.ActiveMdiChild as FormChild;
form.SaveImage(savedialog.FileName);
}
}
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
OpenFileDialog opendialog = new OpenFileDialog();
opendialog.Filter = "(*.bmp)|*.bmp|(*.jpg)|*.jpg";
if(opendialog.ShowDialog() == DialogResult.OK)
{
FormChild form = new FormChild(this);
form.MdiParent = this;
form.Text = "画图" + this.MdiChildren.Length.ToString();
form.filepath = opendialog.FileName;
form.Show();
}
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
FormChild.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication15
{
/// <summary>
/// FormChild 的摘要说明。
/// </summary>
public class FormChild : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
//定义主窗体
public Form1 mainForm;
//定义图像
public Image image;
public string filepath;
//定义画直线的参数
private ArrayList arrLines;
private Line line;
private bool isLineMouseMove;
//定义画折线的参数
private ArrayList arrLineses;
private Lines lines;
private bool isLinesMouseMove;
private bool isDoubleClick;
//定义圆的参数
private ArrayList arrYEllipse;
private Ellipse ellipse;
private bool isEllipseMouseMove;
//定义椭圆的参数
private ArrayList arrTEllipse;
private Ellipse tellipse;
private bool isTEllipseMouseMove;
//定义矩形的参数
private ArrayList arrRect;
private Rectangle rect;
private bool isRectMouseMove;
//定义文字参数
private ArrayList arrText;
private Text text;
public FormChild(Form1 m_mainform)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.DoubleBuffer,true);
this.mainForm = m_mainform;
this.arrLines = new ArrayList();
this.arrLineses = new ArrayList();
this.arrYEllipse = new ArrayList();
this.arrTEllipse = new ArrayList();
this.arrRect = new ArrayList();
this.arrText = new ArrayList();
//
// TODO: 在InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法- 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// FormChild
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(424, 379);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "FormChild";
this.Text = "FormChild";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.FormChild_MouseDown);
this.DoubleClick += new System.EventHandler(this.FormChild_DoubleClick);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.FormChild_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.FormChild_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.FormChild_MouseMove);
}
#endregion
private void FormChild_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//画直线
if(this.mainForm.isLine == true)
{
this.isLineMouseMove = true;
this.line = new Line();
line.point1.X = e.X;
line.point1.Y = e.Y;
}
//画折线
if(this.mainForm.isLines == true)
{
this.isLinesMouseMove = true;
if(this.isDoubleClick == false)
{
this.lines = new Lines();
this.isDoubleClick = true;
}
lines.Points.Add(new Point(e.X,e.Y));
}
//画圆
if(this.mainForm.isYEllipse == true)
{
this.isEllipseMouseMove = true;
this.ellipse = new Ellipse();
this.ellipse.x = e.X;
this.ellipse.y = e.Y;
}
//画椭圆
if(this.mainForm.isTEllipse == true)
{
this.isTEllipseMouseMove = true;
this.tellipse = new Ellipse();
this.tellipse.x = e.X;
this.tellipse.y = e.Y;
}
//画矩形
if(this.mainForm.isRect == true)
{
this.isRectMouseMove = true;
this.rect = new Rectangle();
this.rect.X = e.X;
this.rect.Y = e.Y;
}
//画文字
if(this.mainForm.isText == true)
{
FormText form = new FormText();
if(form.ShowDialog() == DialogResult.OK)
{
this.text = new Text();
this.text.x = e.X;
this.text.y = e.Y;
this.text.text = form.text;
this.arrText.Add(this.text);
}
}
}
private void FormChild_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
//画直线
if(this.isLineMouseMove == true)
{
line.point2.X = e.X;
line.point2.Y = e.Y;
}
//画折线
if(this.isLinesMouseMove == true)
{
lines.MovePoint.X = e.X;
lines.MovePoint.Y = e.Y;
}
//画圆
if(this.isEllipseMouseMove == true)
{
this.ellipse.width = (int)Math.Sqrt((e.X-this.ellipse.x)*(e.X-this.ellipse.x) + (e.Y-this.ellipse.y)*(e.Y-this.ellipse.y));
}
//画椭圆
if(this.isTEllipseMouseMove == true)
{
this.tellipse.width = e.X - this.tellipse.x;
this.tellipse.height = e.Y - this.tellipse.y;
}
//画矩形
if(this.isRectMouseMove == true)
{
this.rect.Width = e.X - this.rect.X;
this.rect.Height = e.Y - this.rect.Y;
}
//把鼠标的坐标显示在状态栏中
this.mainForm.statusBarPanel1.Text = "X: " + e.X.ToString();
this.mainForm.statusBarPanel2.Text = "Y: " + e.Y.ToString();
this.Invalidate();
}
private void FormChild_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
//画直线
if(this.isLineMouseMove == true)
{
this.isLineMouseMove = false;
line.point2.X = e.X;
line.point2.Y = e.Y;
this.arrLines.Add(line);
}
//画圆
if(this.isEllipseMouseMove == true)
{
this.isEllipseMouseMove = false;
this.arrYEllipse.Add(this.ellipse);
}
//画椭圆
if(this.isTEllipseMouseMove == true)
{
this.isTEllipseMouseMove = false;
this.arrTEllipse.Add(this.tellipse);
}
//画矩形
if(this.isRectMouseMove == true)
{
this.isRectMouseMove = false;
this.arrRect.Add(this.rect);
}
}
private void FormChild_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if(this.filepath == null)
this.image = new Bitmap(this.Width,this.Height);
else
this.image = new Bitmap(this.filepath);
Graphics g = Graphics.FromImage(image);
Pen blackPen = new Pen(Color.Black,2);
//画直线
if(this.isLineMouseMove == true)
g.DrawLine(blackPen,line.point1,line.point2);
foreach(Line m_line in arrLines)
{
g.DrawLine(blackPen,m_line.point1,m_line.point2);
}
//画折线
if(this.isLinesMouseMove == true)
g.DrawLine(blackPen,(Point)lines.Points[lines.Points.Count-1],lines.MovePoint);
if(this.lines != null)
{
if(lines.Points.Count > 1 )
g.DrawLines(blackPen,lines.GetPoints());
}
foreach(Lines m_lines in this.arrLineses)
{
if(m_lines.Points.Count > 1 )
g.DrawLines(blackPen,m_lines.GetPoints());
}
//画圆
if(ellipse != null)
g.DrawEllipse(blackPen,this.ellipse.x,this.ellipse.y,this.ellipse.width,this.ellipse.width);
foreach(Ellipse m_ellipse in this.arrYEllipse)
{
g.DrawEllipse(blackPen,m_ellipse.x,m_ellipse.y,m_ellipse.width,m_ellipse.width);
}
//画椭圆
if(tellipse != null)
g.DrawEllipse(blackPen,this.tellipse.x,this.tellipse.y,this.tellipse.width,this.tellipse.height);
foreach(Ellipse m_tellipse in this.arrTEllipse)
{
g.DrawEllipse(blackPen,m_tellipse.x,m_tellipse.y,m_tellipse.width,m_tellipse.height);
}
//画矩形
if(rect.Width != 0)
g.DrawRectangle(blackPen,this.rect);
foreach(Rectangle m_rect in this.arrRect)
{
g.DrawRectangle(blackPen,m_rect);
}
//显示文字
foreach(Text m_text in this.arrText)
{
g.DrawString(m_text.text,this.Font,Brushes.Black,m_text.x,m_text.y);
}
//显示图像
e.Graphics.DrawImage(image,0,0,this.Width,this.Height);
}
private void FormChild_DoubleClick(object sender, System.EventArgs e)
{
//画折线
this.isLinesMouseMove = false;
this.isDoubleClick = false;
this.arrLineses.Add(this.lines);
}
public void SaveImage(string path)
{
this.image.Save(path);
}
}
}
/// <summary>
/// Line 的摘要说明。
/// </summary>
public class Line
{
public Point point1;
public Point point2;
public Line()
{
//
// TODO: 在此处添加构造函数逻辑
//
this.point1 = new Point(0);
this.point2 = new Point(0);
}
}
/// <summary>
/// Lines 的摘要说明。
/// </summary>
public class Lines
{
public ArrayList Points;
public Point MovePoint;
public Lines()
{
//
// TODO: 在此处添加构造函数逻辑
//
this.Points = new ArrayList();
this.MovePoint = new Point(0);
}
public Point[] GetPoints()
{
Point[] points = new Point[this.Points.Count];
for(int i=0; i < this.Points.Count; i++)
{
points[i] = (Point)this.Points[i];
}
return points;
}
}
/// <summary>
/// Text 的摘要说明。
/// </summary>
public class Text
{
public int x;
public int y;
public string text;
public Font font;
public SolidBrush color;
public Text()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
/// <summary>
/// Ellipse 的摘要说明。
/// </summary>
public class Ellipse
{
public int x;
public int y;
public int width;
public int height;
public Ellipse()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}