A Skeletal Form-Based Windows Program


We will begin by creating a minimal form-based Windows application. This application simply creates and displays a window. It contains no other features. However, this skeleton does show the steps necessary to construct a fully functional window. This framework is the starting point upon which most types of Windows applications will be built. The skeletal form-based Windows program is shown here:

 // A form-based Windows Skeleton. using System; using System.Windows.Forms; // WinSkel is derived from Form. class WinSkel : Form {   public WinSkel() {     // Give the window a name.     Text = "A Windows Skeleton";   }   // Main is used only to start the application.   [STAThread]   public static void Main() {     WinSkel skel = new WinSkel(); // create a form     // Start the window running.     Application.Run(skel);   } }

The window created by this program is shown in Figure 26-1.

image from book
Figure 26-1: The skeletal form-based window

Let’s examine this program line by line. First, notice that both System and System.Windows.Forms are included. System is needed because of the STAThread attribute that precedes Main( ). System.Windows.Forms supports the Windows forms subsystem, as just explained.

Next, a class called WinSkel is created. It inherits Form. Thus, WinSkel defines a specific type of form. In this case, it is a minimal form.

Inside the WinSkel constructor is the following line of code:

 Text = "A Windows Skeleton";

Text is the property that sets the title of the window. Thus, this assignment causes the title bar in the window to contain “A Windows Skeleton”. Text is defined like this:

 public override string Text { get; set; }

Text is inherited from Control.

Next is the Main( ) method, which is declared much like the Main( ) methods found throughout the rest of this book. It is the method at which program execution begins. Notice, however, that it is preceded by the STAThread property. As a general rule, the Main( ) method of a Windows program should have this property. It sets the threading model for the program to a single-threaded apartment (STA). (A discussion of threading models and apartments is beyond the scope of this chapter, but briefly, a Windows application can use one of two different threading models: single-threaded apartment or multithreaded apartment.)

Inside Main( ), a WinSkel object is created. This object is then passed to the Run( ) method defined by the Application class, as shown here:

 Application.Run(skel);

This starts the window running. The Application class is defined within System.Windows.Forms, and it encapsulates aspects common to all Windows applications. The Run( ) method used by the skeleton is shown here:

 public static void Run(Form ob)

It takes a reference to a form as a parameter. Since WinSkel inherits Form, an object of type WinSkel can be passed to Run( ).

When the program is run, it creates the window shown in Figure 26-1. The window has the default size and is fully functional. It can be resized, moved, minimized, maximized, and closed. Thus, the basic features needed by nearly all windows were achieved by writing only a few lines of form-based code. In contrast, the same program written using the C language and directly calling the Windows API would have required approximately five times as many lines of code!

The preceding skeleton defines the outline that most form-based Windows applications will take. In general, to create a form, you create a class that inherits Form. Initialize the form to meet your needs, create an object of your derived class, and then call Application.Run( ) on that object.

Compiling the Windows Skeleton

You can compile a Windows program using either the command-line compiler or Visual Studio. For the very short programs shown in this chapter, the command-line compiler is the easiest way; but for real applications, you will probably want to use the IDE. (Also, as explained at the start of this chapter, you will probably want to use the design tools provided by Visual Studio.) Each way is shown here.

Compiling from the Command Line

Assuming that you call the skeleton WinSkel.cs, then to compile the skeleton from the command line, use this command:

 csc /t:winexe WinSkel.cs

The /t:winexe switch tells the compiler to create a Windows application rather than a console program. To run the program, simply enter WinSkel at the command line.

Compiling from the IDE

To compile the program using the Visual Studio IDE, first create a new Windows Application project. Do this by selecting File | New | Project. Then select Windows Application in the New Project dialog box. Call the project WinSkel. Delete all C# source code files that were automatically created. Next, right-click on the WinSkel project name and select Add, and then select Add New Item. From the Add New Item dialog box, select C# Code File, and name the file WinSkel.cs. Now, enter the skeleton code exactly as shown, and then build the solution. To run the project, select Debug | Start Without Debugging.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net