Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


System.Windows.Forms

In .Net, a window is called a "form". Chapter 4 introduces Graphic User Interface (GUI) programming.

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 ()); 
    }
}

Compile it with "csc /target:winexe hello.cs"


ImageView Example

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 ());
    }
}
 

 

 


Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: January 26, 2004.