[ Home | Syllabus | Course Notes | Assignments | Search]
In .Net, a window is called a "form". Chapter 4 introduces Graphic User Interface (GUI) programming.
Graphics container: window, menu, button, listbox
Graphic drawing object: brush, pen, image, lines, text
Thread of execution: .NET uses a message/event driven architecture
Event handlers
With so many classes to explore, we will examine several of the examples in chapter 4 starting with the obligatory "Hello" program
using System; using System.Windows.Forms;using System.Drawing;
class MyForm : Form // inherits from the "Form" class { MyForm () { Text = "Windows Forms Demo";
} protected override void OnPaint (PaintEventArgs e)
{ e.Graphics.DrawString ("Hello, world", Font,
new SolidBrush (Color.Black), ClientRectangle);
} static void Main () { Application.Run (new MyForm ());
} }
F1: includes windows, menus, clipboard, controls like button, textbox, monthcalendar
F2: wraps the "GDI+" (Graphics Device Interface) portion of Windows
F3: "Text" is inherited from "Form" - it is the window's title
F4: Override this inherited virtual method to "Paint" the window. This is an event handler that responds to a WM_PAINT (on windows platforms) message. (more detail can be found in spring 2002 course offering). check out the PaintEventArgs info
F5: Font is member of Form - default font - can be changed
F6: Brush is color (try changing), ClientRectangle is data member - size of window
F7: Application provides a thread of execution and "Runs" the new Window object "Form"
Compile it with "csc /target:winexe hello.cs"
Menu handling
Menu state updating
Event/Message Handlers
Scrolling
Bitmaps
using System;
using System.Windows.Forms;
using System.Drawing;
class MyForm : Form
{
MenuItem NativeSize;
MenuItem FitToWindow;
bool ShowNativeSize = true;
int FilterIndex = -1;
Bitmap MyBitmap = null;
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 ();// "new" common for reference types
MenuItem item = menu.MenuItems.Add ("&Options");
item.Popup += new EventHandler (OnPopupOptionsMenu);
item.MenuItems.Add (new MenuItem ("&Open...",
new EventHandler (OnOpenImage), Shortcut.CtrlO)); 
item.MenuItems.Add ("-");
item.MenuItems.Add (FitToWindow =
new MenuItem ("Size Image to &Fit Window",
new EventHandler (OnFitToWindow))
);
item.MenuItems.Add (NativeSize =
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
void OnPopupOptionsMenu (object sender, EventArgs e)
{
NativeSize.Checked = ShowNativeSize;
FitToWindow.Checked = !ShowNativeSize;
}
// Handler for the Open command
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 {
Bitmap bitmap = new Bitmap (FileName);
if (MyBitmap != null)
MyBitmap.Dispose (); // Important!
MyBitmap = bitmap;
string[] parts = FileName.Split ('\\');
Text = "Image Viewer - " + parts[parts.Length - 1];
if (ShowNativeSize) {
AutoScroll = true;
AutoScrollMinSize = MyBitmap.Size;
AutoScrollPosition = new Point (0, 0);
}
Invalidate ();
}
catch (ArgumentException) {
MessageBox.Show (String.Format ("{0} is not " +
"a valid image file", FileName), "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
ofd.Dispose ();
}
// Handler for the Size Image to Fit Window command
void OnFitToWindow (object sender, EventArgs e)
{
ShowNativeSize = false;
SetStyle (ControlStyles.ResizeRedraw, true);
if (MyBitmap != null) {
AutoScroll = false;
Invalidate ();
}
}
// Handler for the Show Image in Native Size command
void OnNativeSize (object sender, EventArgs e)
{
ShowNativeSize = true;
SetStyle (ControlStyles.ResizeRedraw, false);
if (MyBitmap != null) {
AutoScroll = true;
AutoScrollMinSize = MyBitmap.Size;
AutoScrollPosition = new Point (0, 0);
Invalidate ();
}
}
// Handler for the Exit command
void OnExit (object sender, EventArgs e)
{
Close ();
}
// OnPaint handler
protected override void OnPaint (PaintEventArgs e)
{
if (MyBitmap != null) {
Graphics g = e.Graphics;
if (ShowNativeSize)
g.DrawImage (MyBitmap,
AutoScrollPosition.X, AutoScrollPosition.Y,
MyBitmap.Width, MyBitmap.Height);
else
g.DrawImage (MyBitmap, ClientRectangle);
}
}
static void Main ()
{
Application.Run (new MyForm ());
}
}
F8: Adds a new item to the menu AND remembers a REFERENCE to it in the reference variable "item" Therefore changes to "item" will change the "menu"
F9: "+=" is overloaded to add the event handler (delegate) to the POPUP event - thus the function "OnPopupOptionsMenu" will be called when the user wishes to "Popup" the main menu.
F10 A "Menutiem object can itself have a set of "MenuItems" for a submenu "&" is for keyboard access
F11: also can add an accelerator (shortcut) key
F12: Horizontal line in menu
F13: Adds an event handler during construction
F14: makes newly constructed "menu" the window's "Menu"
F15: this event handler is valled before the menu is painted so that "attributes" of the menu itmes can be checked - NOTE - we set the reference variables earlier