Figure 1
Figure 1 Running MyFormusing System;
using System.WinForms;
using System.Drawing;
public class MyForm : Form
{
public MyForm ()
{
Text = "Windows Forms Demo";
}
protected override void OnPaint (PaintEventArgs e)
{
e.Graphics.DrawString ("Hello, world", Font,
new SolidBrush (Color.Black), ClientRectangle);
}
public static void Main (string[] args)
{
Application.Run (new MyForm ());
}
}
Figure 3 Main.cs (Version 1) using System;
using System.WinForms;
using System.Drawing;
public class MyForm : Form
{
public MyForm ()
{
// Set the form's title
Text = "Image Viewer";
// Set the form's size
ClientSize = new Size (640, 480);
}
public static void Main (string[] args)
{
Application.Run (new MyForm ());
}
}
Figure 4 Main.cs (Version 2) using System;
using System.WinForms;
using System.Drawing;
public class MyForm : Form
{
public MyForm ()
{
// Set the form's title
Text = "Image Viewer";
// Set the form's size
ClientSize = new Size (640, 480);
// Create a menu
MainMenu menu = new MainMenu ();
MenuItem item = menu.MenuItems.Add ("&Options");
item.MenuItems.Add (new MenuItem ("E&xit",
new EventHandler (OnExit)));
// Attach the menu to the form
Menu = menu;
}
// Handler for the Exit command
private void OnExit (object sender, EventArgs e)
{
Close ();
}
public static void Main (string[] args)
{
Application.Run (new MyForm ());
}
}
Figure 5 Main.cs (Version 3) using System;
using System.WinForms;
using System.Drawing;
public class MyForm : Form
{
protected int _FilterIndex = -1;
protected Bitmap _MyBitmap;
public MyForm ()
{
// Set the form's title
Text = "Image Viewer";
// Set the form's size
ClientSize = new Size (640, 480);
// Create a menu
MainMenu menu = new MainMenu ();
MenuItem item = menu.MenuItems.Add ("&Options");
item.MenuItems.Add (new MenuItem ("&Open...",
new EventHandler (OnOpenImage), Shortcut.CtrlO));
item.MenuItems.Add ("-");
item.MenuItems.Add (new MenuItem ("E&xit",
new EventHandler (OnExit)));
// Attach the menu to the form
Menu = menu;
}
// Handler for the Open command
private void OnOpenImage (object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog ();
ofd.Filter = "Image Files (JPEG, GIF, BMP, etc.)|" +
"*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png|" +
"JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"GIF Files (*.gif)|*.gif|" +
"BMP Files (*.bmp)|*.bmp|" +
"TIFF Files (*.tif;*.tiff)|*.tif;*.tiff|" +
"PNG Files (*.png)|*.png|" +
"All files (*.*)|*.*";
if (_FilterIndex != -1)
ofd.FilterIndex = _FilterIndex;
if (ofd.ShowDialog () == DialogResult.OK) {
String fileName = ofd.FileName;
if (fileName.Length != 0) {
_FilterIndex = ofd.FilterIndex;
try {
_MyBitmap = new Bitmap (fileName);
Text = "Image Viewer - " + fileName;
Invalidate ();
}
catch {
MessageBox.Show (String.Format ("{0} is not " +
"a valid image file", fileName), "Error",
MessageBox.OK | MessageBox.IconError);
}
}
}
}
// Handler for the Exit command
private void OnExit (object sender, EventArgs e)
{
Close ();
}
public static void Main (string[] args)
{
Application.Run (new MyForm ());
}
}
Figure 6 Main.cs (Version 4) using System;
using System.WinForms;
using System.Drawing;
public class MyForm : Form
{
•••
// Handler for the Exit command
private void OnExit (object sender, EventArgs e)
{
Close ();
}
// OnPaint handler
protected override void OnPaint (PaintEventArgs e)
{
if (_MyBitmap != null) {
Graphics g = e.Graphics;
g.DrawImage (_MyBitmap, 0, 0,
_MyBitmap.Width, _MyBitmap.Height);
}
}
public static void Main (string[] args)
{
Application.Run (new MyForm ());
}
}
Figure 8 Main.cs (Version 5) using System;
using System.WinForms;
using System.Drawing;
public class MyForm : Form
{
•••
// Handler for the Open command
private void OnOpenImage (object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog ();
ofd.Filter = "Image Files (JPEG, GIF, BMP, etc.)|" +
"*.jpg;*.jpeg;*.gif;*.bmp;*.tif;*.tiff;*.png|" +
"JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"GIF Files (*.gif)|*.gif|" +
"BMP Files (*.bmp)|*.bmp|" +
"TIFF Files (*.tif;*.tiff)|*.tif;*.tiff|" +
"PNG Files (*.png)|*.png|" +
"All files (*.*)|*.*";
if (_FilterIndex != -1)
ofd.FilterIndex = _FilterIndex;
if (ofd.ShowDialog () == DialogResult.OK) {
String fileName = ofd.FileName;
if (fileName.Length != 0) {
_FilterIndex = ofd.FilterIndex;
try {
_MyBitmap = new Bitmap (fileName);
Text = "Image Viewer - " + fileName;
AutoScroll = true;
AutoScrollMinSize = _MyBitmap.Size;
Invalidate ();
}
catch {
MessageBox.Show (String.Format ("{0} is not " +
"a valid image file", fileName), "Error",
MessageBox.OK | MessageBox.IconError);
}
}
}
}
// Handler for the Exit command
private void OnExit (object sender, EventArgs e)
{
Close ();
}
// OnPaint handler
protected override void OnPaint (PaintEventArgs e)
{
if (_MyBitmap != null) {
Graphics g = e.Graphics;
g.DrawImage (_MyBitmap,
AutoScrollPosition.X, AutoScrollPosition.Y,
_MyBitmap.Width, _MyBitmap.Height);
}
}
public static void Main (string[] args)
{
Application.Run (new MyForm ());
}
}
Figure 9 Main.cs (Version 6) using System;
using System.WinForms;
using System.Drawing;
public class MyForm : Form
{
protected bool _NativeSize = true;
protected int _FilterIndex = -1;
protected Bitmap _MyBitmap;
public MyForm ()
{
// Set the form's title
Text = "Image Viewer";
// Set the form's size
ClientSize = new Size (640, 480);
// Create a menu
MainMenu menu = new MainMenu ();
MenuItem item = menu.MenuItems.Add ("&Options");
item.MenuItems.Add (new MenuItem ("&Open...",
new EventHandler (OnOpenImage), Shortcut.CtrlO));
item.MenuItems.Add ("-");
item.MenuItems.Add (new MenuItem ("Size Image to &Fit Window",
new EventHandler (OnFitToWindow)));
item.MenuItems.Add (new MenuItem ("Show Image in &Native Size",
new EventHandler (OnNativeSize)));
item.MenuItems.Add ("-");
item.MenuItems.Add (new MenuItem ("E&xit",
new EventHandler (OnExit)));
// Attach the menu to the form
Menu = menu;
}
// Handler for the Open command
private void OnOpenImage (object sender, EventArgs e)
{
•••
}
// Handler for the Size Image to Fit Window command
private void OnFitToWindow (object sender, EventArgs e)
{
_NativeSize = false;
SetStyle (ControlStyles.ResizeRedraw, true);
if (_MyBitmap != null) {
AutoScroll = false;
Invalidate ();
}
}
// Handler for the Show Image in Native Size command
private void OnNativeSize (object sender, EventArgs e)
{
_NativeSize = true;
SetStyle (ControlStyles.ResizeRedraw, false);
if (_MyBitmap != null) {
AutoScroll = true;
AutoScrollMinSize = _MyBitmap.Size;
Invalidate ();
}
}
// Handler for the Exit command
private void OnExit (object sender, EventArgs e)
{
Close ();
}
// OnPaint handler
protected override void OnPaint (PaintEventArgs e)
{
if (_MyBitmap != null) {
Graphics g = e.Graphics;
if (_NativeSize)
g.DrawImage (_MyBitmap,
AutoScrollPosition.X, AutoScrollPosition.Y,
_MyBitmap.Width, _MyBitmap.Height);
else
g.DrawImage (_MyBitmap, ClientRectangle);
}
}
public static void Main (string[] args)
{
Application.Run (new MyForm ());
}
}
Figure 10 Main.cs (Final Version) using System;
using System.WinForms;
using System.Drawing;
public class MyForm : Form
{
protected MenuItem _itemNativeSize;
protected MenuItem _itemFitToWindow;
protected bool _NativeSize = true;
protected int _FilterIndex = -1;
protected Bitmap _MyBitmap;
public MyForm ()
{
// Set the form's title
Text = "Image Viewer";
// Set the form's size
ClientSize = new Size (640, 480);
// Create a menu
MainMenu menu = new MainMenu ();
MenuItem item = menu.MenuItems.Add ("&Options");
item.AddOnPopup += new EventHandler (OnPopupOptionsMenu);
item.MenuItems.Add (new MenuItem ("&Open...",
new EventHandler (OnOpenImage), Shortcut.CtrlO));
item.MenuItems.Add ("-");
item.MenuItems.Add (_itemFitToWindow =
new MenuItem ("Size Image to &Fit Window",
new EventHandler (OnFitToWindow))
);
item.MenuItems.Add (_itemNativeSize =
new MenuItem ("Show Image in &Native Size",
new EventHandler (OnNativeSize))
);
item.MenuItems.Add ("-");
item.MenuItems.Add (new MenuItem ("E&xit",
new EventHandler (OnExit)));
// Attach the menu to the form
Menu = menu;
}
// Handler for Options menu popups
private void OnPopupOptionsMenu (object sender, EventArgs e)
{
_itemNativeSize.Checked = _NativeSize ? true : false;
_itemFitToWindow.Checked = _NativeSize ? false : true;
}
•••
public static void Main (string[] args)
{
Application.Run (new MyForm ());
}
}
Figure 12 .NET Framework Classes Used by ImageView | Class | Namespace | Description |
| Application | System.WinForms | Represents a Windows Forms-based application |
| Bitmap | System.Drawing | Represents bitmapped images |
| Form | System.WinForms | Represents the windows (forms) used in Windows Forms-based applications |
| Graphics | System.Drawing | The conduit for a Windows Forms-based application's graphical output; analagous to a Windows device context |
| MainMenu | System.WinForms | Represents main (top-level) menus |
| MenuItem | System.WinForms | Represents the items in a menu |
| MessageBox | System.WinForms | Represents message boxes |
| OpenFileDialog | System.WinForms | Represents the Windows Open File dialog |
namespace TuneTown
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.WinForms;
/// <summary>
/// Summary description for AddEditForm.
/// </summary>
public class AddEditForm : System.WinForms.Form
{
public string Title
{
get { return TitleBox.Text; }
set { TitleBox.Text = value; }
}
public string Artist
{
get { return ArtistBox.Text; }
set { ArtistBox.Text = value; }
}
public string Comment
{
get { return CommentBox.Text; }
set { CommentBox.Text = value; }
}
/// <summary>
/// Required designer variable
/// </summary>
private System.ComponentModel.Container components;
private System.WinForms.Button NotOKButton;
private System.WinForms.Button OKButton;
private System.WinForms.TextBox CommentBox;
private System.WinForms.TextBox ArtistBox;
private System.WinForms.TextBox TitleBox;
private System.WinForms.Label CommentLabel;
private System.WinForms.Label ArtistLabel;
private System.WinForms.Label TitleLabel;
public AddEditForm()
{
//
// Required for Win Form Designer support
//
InitializeComponent();
//
// TODO: Add constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used
/// </summary>
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.ArtistBox = new System.WinForms.TextBox();
this.ArtistLabel = new System.WinForms.Label();
this.TitleLabel = new System.WinForms.Label();
this.NotOKButton = new System.WinForms.Button();
this.OKButton = new System.WinForms.Button();
this.CommentLabel = new System.WinForms.Label();
this.TitleBox = new System.WinForms.TextBox();
this.CommentBox = new System.WinForms.TextBox();
ArtistBox.Location = new System.Drawing.Point(88, 64);
ArtistBox.Text = "";
ArtistBox.TabIndex = 3;
ArtistBox.Size = new System.Drawing.Size(224, 20);
ArtistLabel.Location = new System.Drawing.Point(23, 73);
ArtistLabel.Text = "&Artist";
ArtistLabel.Size = new System.Drawing.Size(64, 16);
ArtistLabel.TabIndex = 2;
TitleLabel.Location = new System.Drawing.Point(24, 32);
TitleLabel.Text = "&Title";
TitleLabel.Size = new System.Drawing.Size(64, 16);
TitleLabel.TabIndex = 0;
NotOKButton.Location = new System.Drawing.Point(216, 200);
NotOKButton.DialogResult = System.WinForms.DialogResult.Cancel;
NotOKButton.Size = new System.Drawing.Size(96, 32);
NotOKButton.TabIndex = 7;
NotOKButton.Text = "Cancel";
OKButton.Location = new System.Drawing.Point(88, 200);
OKButton.DialogResult = System.WinForms.DialogResult.OK;
OKButton.Size = new System.Drawing.Size(96, 32);
OKButton.TabIndex = 6;
OKButton.Text = "OK";
CommentLabel.Location = new System.Drawing.Point(24, 112);
CommentLabel.Text = "&Comment";
CommentLabel.Size = new System.Drawing.Size(64, 16);
CommentLabel.TabIndex = 4;
TitleBox.Location = new System.Drawing.Point(88, 24);
TitleBox.Text = "";
TitleBox.TabIndex = 1;
TitleBox.Size = new System.Drawing.Size(224, 20);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.Text = "AddEditForm";
this.MaximizeBox = false;
this.CancelButton = NotOKButton;
//@design this.TrayLargeIcon = true;
this.BorderStyle = System.WinForms.FormBorderStyle.FixedDialog;
this.ShowInTaskbar = false;
this.AcceptButton = OKButton;
//@design this.TrayHeight = 0;
this.MinimizeBox = false;
this.ClientSize = new System.Drawing.Size(330, 247);
CommentBox.Location = new System.Drawing.Point(88, 104);
CommentBox.Text = "";
CommentBox.Multiline = true;
CommentBox.TabIndex = 5;
CommentBox.Size = new System.Drawing.Size(224, 80);
this.Controls.Add(OKButton);
this.Controls.Add(TitleBox);
this.Controls.Add(TitleLabel);
this.Controls.Add(ArtistLabel);
this.Controls.Add(CommentLabel);
this.Controls.Add(ArtistBox);
this.Controls.Add(CommentBox);
this.Controls.Add(NotOKButton);
}
}
}
Figure 19 MainForm.cs (Version 1) namespace TuneTown
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.WinForms;
using System.Data;
/// <summary>
/// Summary description for MainForm.
/// </summary>
public class MainForm : System.WinForms.Form
{
/// <summary>
/// Required designer variable
/// </summary>
private System.ComponentModel.Container components;
private System.WinForms.ColumnHeader CommentHeader;
private System.WinForms.ColumnHeader ArtistHeader;
private System.WinForms.ColumnHeader TitleHeader;
private System.WinForms.Button RemoveButton;
private System.WinForms.Button EditButton;
private System.WinForms.Button AddButton;
private System.WinForms.ListView TuneView;
public MainForm()
{
//
// Required for Win Form Designer support
//
InitializeComponent();
//
// TODO: Add constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used
/// </summary>
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AddButton = new System.WinForms.Button();
this.CommentHeader = new System.WinForms.ColumnHeader();
this.RemoveButton = new System.WinForms.Button();
this.EditButton = new System.WinForms.Button();
this.ArtistHeader = new System.WinForms.ColumnHeader();
this.TuneView = new System.WinForms.ListView();
this.TitleHeader = new System.WinForms.ColumnHeader();
AddButton.Location = new System.Drawing.Point(408, 16);
AddButton.Size = new System.Drawing.Size(96, 40);
AddButton.TabIndex = 1;
AddButton.Text = "&Add";
AddButton.Click += new EventHandler (OnAddButtonClicked);
CommentHeader.Text = "Comment";
CommentHeader.Width = 200;
CommentHeader.TextAlign =
System.WinForms.HorizontalAlignment.Left;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.Text = "TuneTown";
//@design this.TrayLargeIcon = true;
//@design this.TrayHeight = 0;
this.ClientSize = new System.Drawing.Size(520, 277);
RemoveButton.Location = new System.Drawing.Point(408, 134);
RemoveButton.Size = new System.Drawing.Size(96, 40);
RemoveButton.TabIndex = 3;
RemoveButton.Text = "&Remove";
RemoveButton.Click += new EventHandler (OnRemoveButtonClicked);
EditButton.Location = new System.Drawing.Point(408, 75);
EditButton.Size = new System.Drawing.Size(96, 40);
EditButton.TabIndex = 2;
EditButton.Text = "&Edit";
EditButton.Click += new EventHandler (OnEditButtonClicked);
ArtistHeader.Text = "Artist";
ArtistHeader.Width = 100;
ArtistHeader.TextAlign =
System.WinForms.HorizontalAlignment.Left;
TuneView.Text = "listView1";
TuneView.MultiSelect = false;
TuneView.HideSelection = false;
TuneView.FullRowSelect = true;
TuneView.TabIndex = 0;
TuneView.ForeColor = System.Drawing.SystemColors.WindowText;
TuneView.View = System.WinForms.View.Report;
TuneView.GridLines = true;
TuneView.Sorting = System.WinForms.SortOrder.Ascending;
TuneView.Size = new System.Drawing.Size(376, 248);
TuneView.Location = new System.Drawing.Point(16, 16);
TuneView.Columns.All =
new System.WinForms.ColumnHeader[] {TitleHeader,
ArtistHeader, CommentHeader};
TuneView.AddOnDoubleClick +=
new EventHandler (OnItemDoubleClicked);
TitleHeader.Text = "Title";
TitleHeader.Width = 100;
TitleHeader.TextAlign =
System.WinForms.HorizontalAlignment.Left;
this.Controls.Add(AddButton);
this.Controls.Add(TuneView);
this.Controls.Add(EditButton);
this.Controls.Add(RemoveButton);
}
private void OnAddButtonClicked (object sender, EventArgs e)
{
AddEditForm dlg = new AddEditForm ();
if (dlg.ShowDialog () == DialogResult.OK) {
ListItem item = TuneView.InsertItem (
TuneView.ListItems.Count, new ListItem (dlg.Title,
new string[] { dlg.Artist, dlg.Comment } ));
item.Focused = true;
}
}
private void OnEditButtonClicked (object sender, EventArgs e)
{
if (TuneView.ListItems.Count != 0) {
ListItem item = TuneView.FocusedItem;
if (item != null) {
AddEditForm dlg = new AddEditForm ();
dlg.Title = item.Text;
dlg.Artist = item.SubItems[0];
dlg.Comment = item.SubItems[1];
if (dlg.ShowDialog () == DialogResult.OK) {
TuneView.FocusedItem.Text = dlg.Title;
TuneView.FocusedItem.SetSubItem (0, dlg.Artist);
TuneView.FocusedItem.SetSubItem (1, dlg.Comment);
}
}
}
}
private void OnRemoveButtonClicked (object sender, EventArgs e)
{
if (TuneView.ListItems.Count != 0) {
ListItem item = TuneView.FocusedItem;
if (item != null)
item.Remove ();
}
}
private void OnItemDoubleClicked (object sender, EventArgs e)
{
OnEditButtonClicked (sender, e);
}
/*
* The main entry point for the application.
*
*/
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
}
}
Figure 20 MainForm.cs (Final Version) namespace TuneTown
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.WinForms;
using System.Data;
using System.IO;
/// <summary>
/// Summary description for MainForm.
/// </summary>
public class MainForm : System.WinForms.Form
{
•••
private void InitializeComponent()
{
•••
this.Controls.Add(AddButton);
this.Controls.Add(TuneView);
this.Controls.Add(EditButton);
this.Controls.Add(RemoveButton);
InitializeListView ();
}
•••
private void OnItemDoubleClicked (object sender, EventArgs e)
{
OnEditButtonClicked (sender, e);
}
protected override void OnClosing (CancelEventArgs e)
{
try {
StreamWriter writer =
File.CreateText (
SystemInformation.GetFolderPath (
SpecialFolder.LocalApplicationData) +
"\\TuneTownData.ttd");
try {
for (int i=0; i<TuneView.ListItems.Count; i++) {
string s1 = TuneView.ListItems[i].Text;
string s2 = TuneView.ListItems[i].SubItems[0];
string s3 = TuneView.ListItems[i].SubItems[1];
writer.WriteLine (s1);
writer.WriteLine (s2);
writer.WriteLine (s3);
}
}
catch (Exception ex2) {
MessageBox.Show (ex2.Message);
}
finally {
writer.Close ();
}
}
catch (Exception ex1) {
MessageBox.Show (ex1.Message);
}
}
protected void InitializeListView ()
{
try {
StreamReader reader =
File.OpenText (
SystemInformation.GetFolderPath (
SpecialFolder.LocalApplicationData) +
"\\TuneTownData.ttd");
try {
string s1;
do {
s1 = reader.ReadLine ();
if (s1 != null) {
string s2 = reader.ReadLine ();
string s3 = reader.ReadLine ();
ListItem item = TuneView.InsertItem (
TuneView.ListItems.Count,
new ListItem (s1,
new string[] { s2, s3 } ));
item.Focused = true;
}
} while (s1 != null);
}
catch (Exception ex2) {
MessageBox.Show (ex2.Message);
}
finally {
reader.Close ();
}
}
catch (Exception) {
// Ignore exception (file doesn't yet exist)
}
}
/*
* The main entry point for the application.
*
*/
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
}
}
Show: