C# Object Serialization
Pages: 1, 2, 3, 4
Example 2. The form class for the application.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
namespace MyCSharp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem fileMenuItem;
private System.Windows.Forms.MenuItem fileNewMenuItem;
private System.Windows.Forms.MenuItem fileOpenMenuItem;
private System.Windows.Forms.MenuItem fileSaveMenuItem;
private System.Windows.Forms.MenuItem fileSaveAsMenuItem;
private System.Windows.Forms.MenuItem fileExitMenuItem;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton lineToolBarButton;
private System.Windows.Forms.ToolBarButton rectangleToolBarButton;
private System.Windows.Forms.ToolBarButton ellipseToolBarButton;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private ArrayList shapes = new ArrayList();
private enum shapeType
{
line, rectangle, ellipse,
}
private shapeType drawnShapeType = shapeType.line;
private Point startPoint;
private String filename;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 =
new System.Windows.Forms.MainMenu();
this.fileMenuItem =
new System.Windows.Forms.MenuItem();
this.fileNewMenuItem =
new System.Windows.Forms.MenuItem();
this.fileOpenMenuItem =
new System.Windows.Forms.MenuItem();
this.fileSaveMenuItem =
new System.Windows.Forms.MenuItem();
this.fileSaveAsMenuItem =
new System.Windows.Forms.MenuItem();
this.fileExitMenuItem =
new System.Windows.Forms.MenuItem();
this.toolBar1 =
new System.Windows.Forms.ToolBar();
this.lineToolBarButton =
new System.Windows.Forms.ToolBarButton();
this.rectangleToolBarButton =
new System.Windows.Forms.ToolBarButton();
this.ellipseToolBarButton =
new System.Windows.Forms.ToolBarButton();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {
this.fileMenuItem});
//
// fileMenuItem
//
this.fileMenuItem.Index = 0;
this.fileMenuItem.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {
this.fileNewMenuItem,
this.fileOpenMenuItem,
this.fileSaveMenuItem,
this.fileSaveAsMenuItem,
this.fileExitMenuItem});
this.fileMenuItem.Text = "File";
//
// fileNewMenuItem
//
this.fileNewMenuItem.Index = 0;
this.fileNewMenuItem.Shortcut =
System.Windows.Forms.Shortcut.CtrlN;
this.fileNewMenuItem.Text =
"&New";
this.fileNewMenuItem.Click +=
new System.EventHandler(this.fileNewMenuItem_Click);
//
// fileOpenMenuItem
//
this.fileOpenMenuItem.Index = 1;
this.fileOpenMenuItem.Shortcut =
System.Windows.Forms.Shortcut.CtrlO;
this.fileOpenMenuItem.Text =
"&Open";
this.fileOpenMenuItem.Click +=
new System.EventHandler(this.fileOpenMenuItem_Click);
//
// fileSaveMenuItem
//
this.fileSaveMenuItem.Index = 2;
this.fileSaveMenuItem.Shortcut =
System.Windows.Forms.Shortcut.CtrlS;
this.fileSaveMenuItem.Text = "&Save";
this.fileSaveMenuItem.Click +=
new System.EventHandler(this.fileSaveMenuItem_Click);
//
// fileSaveAsMenuItem
//
this.fileSaveAsMenuItem.Index = 3;
this.fileSaveAsMenuItem.Text = "Save &As";
this.fileSaveAsMenuItem.Click +=
new System.EventHandler(this.fileSaveAsMenuItem_Click);
//
// fileExitMenuItem
//
this.fileExitMenuItem.Index = 4;
this.fileExitMenuItem.Text = "E&xit";
this.fileExitMenuItem.Click +=
new System.EventHandler(this.fileExitMenuItem_Click);
//
// toolBar1
//
this.toolBar1.Buttons.AddRange(
new System.Windows.Forms.ToolBarButton[] {
this.lineToolBarButton,
this.rectangleToolBarButton,
this.ellipseToolBarButton});
this.toolBar1.DropDownArrows = true;
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(292, 39);
this.toolBar1.TabIndex = 0;
this.toolBar1.ButtonClick +=
new System.Windows.Forms.ToolBarButtonClickEventHandler(
this.toolBar1_ButtonClick);
//
// lineToolBarButton
//
this.lineToolBarButton.Text = "Line";
//
// rectangleToolBarButton
//
this.rectangleToolBarButton.Text = "Rectangle";
//
// ellipseToolBarButton
//
this.ellipseToolBarButton.Text = "Ellipse";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(
new System.Windows.Forms.Control[] {
this.toolBar1});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown +=
new System.Windows.Forms.MouseEventHandler(this_MouseDown);
this.MouseUp +=
new System.Windows.Forms.MouseEventHandler(this_MouseUp);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void fileExitMenuItem_Click(object sender,
System.EventArgs e)
{
this.Close();
}
private void toolBar1_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button==lineToolBarButton)
drawnShapeType = shapeType.line;
else if (e.Button==rectangleToolBarButton)
drawnShapeType = shapeType.rectangle;
else
drawnShapeType = shapeType.ellipse;
}
private void fileNewMenuItem_Click(object sender,
System.EventArgs e)
{
shapes.Clear();
this.CreateGraphics().Clear(Color.White);
}
private void fileOpenMenuItem_Click(object sender,
System.EventArgs e)
{
OpenDocument();
}
private void fileSaveMenuItem_Click(object sender,
System.EventArgs e)
{
Save();
}
private void fileSaveAsMenuItem_Click(object sender,
System.EventArgs e)
{
SaveAs();
}
private void OpenDocument()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog.FileName;
Stream myStream = openFileDialog.OpenFile();
if (myStream != null)
{
IFormatter formatter = new BinaryFormatter();
shapes = (ArrayList) formatter.de-serialize(myStream);
myStream.Close();
this.Refresh();
}
}
}
private bool Save()
{
if (filename==null)
{
return SaveAs();
}
else {
Stream myStream ;
myStream = File.OpenWrite(filename);
if (myStream != null)
{
IFormatter formatter =
/*(IFormatter) */ new BinaryFormatter();
// serialize shapes
formatter.Serialize(myStream, shapes);
myStream.Close();
return true;
}
}
return false;
}
private bool SaveAs()
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
filename = saveFileDialog.FileName;
return Save();
}
return false;
}
private void this_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==System.Windows.Forms.MouseButtons.Left)
{
startPoint = new Point(e.X, e.Y);
}
}
override protected void OnPaint(
System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
IEnumerator shapeEnum = shapes.GetEnumerator();
while (shapeEnum.MoveNext())
{
IShape shape = (IShape) shapeEnum.Current;
shape.Draw(g);
}
}
private void this_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==System.Windows.Forms.MouseButtons.Left)
{
Point endPoint = new Point(e.X, e.Y);
if (drawnShapeType==shapeType.line)
{
shapes.Add(new Line(startPoint, endPoint));
}
else if (drawnShapeType==shapeType.rectangle)
{
shapes.Add(new Rect(GetRectangleFromPoints(startPoint,
endPoint)));
}
else
{
shapes.Add(new Ellipse(GetRectangleFromPoints(startPoint,
endPoint)));
}
}
this.Refresh();
}
private Rectangle GetRectangleFromPoints(Point p1, Point p2)
{
int x1, x2, y1, y2;
if (p1.X < p2.X)
{
x1 = p1.X;
x2 = p2.X;
}
else
{
x1 = p2.X;
x2 = p1.X;
}
if (p1.Y < p2.Y)
{
y1 = p1.Y;
y2 = p2.Y;
}
else
{
y1 = p2.Y;
y2 = p1.Y;
}
// x2 > x1 and y2 > y1
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
}
}

