Hello, World Win Form Style


"Hello, World!" Win Form Style

Okay, you did the obligatory "Hello, World!" for a console application, so now you'll do it again for a Win Form application. The first thing you need to do is create a project using the Windows Forms Application (.NET) template, exactly like you did for the console application (see Figure 9-1).

click to expand
Figure 9-1: Creating a Win Form "Hello, World!" application project

Once the project template is finished being built, you have a complete Windows application. Okay, on to the next chapter ... Just kidding!

The process of building the "Hello, World!" application involves the following steps:

  1. Expand the GUI Toolbox view.

  2. Click the required GUI component in the Toolbox view.

  3. Drag the component to the design form.

  4. Change the component's properties in the Properties view.

  5. Double-click the component to create the event handler for the component. This will bring up the IDE editor.

  6. Enter the code in the IDE editor to handle the event for the component.

This is very easy and very straightforward. If this level of simplicity gives you the willies, as it did me, be comforted by the fact that you can go in and code everything by hand if you want. After a while, you will come to realize that you do not have to code much in the way of the GUI interface manually.

So what code is provided? Listing 9-1 shows Form1.cpp. It doesn't look like there's much going on, but looks can be deceiving.

Listing 9-1: The Default Form1.cpp

start example
 #include "stdafx.h" #include "Form1.h" #include <windows.h> using namespace Hello; int APIENTRY _tWinMain(HINSTANCE hInstance,                        HINSTANCE hPrevInstance,                        LPTSTR    lpCmdLine,                        int       nCmdShow) {     System::Threading::Thread::CurrentThread->ApartmentState =        System::Threading::ApartmentState::STA;     Application::Run(new Form1());     return 0; } 
end example

The first thing you notice is that the wizard includes the <windows. h> header file. The only reason I see for this is for using Windows typedef data types. Notice the header file is even included after the Form1.h file, which, as you'll see, contains the definition of the form. Being included after Form1.h means none of its defined types are even referenced within Form1.h.

Start up the Windows application using a standard WinMain. Do you notice the heavy use of Windows data types? This could also be coded as

 int __stdcall WinMain(long hInst, long hPrevInst, long lpCmdLine, int nCmdShow) { } 

Then #include <windows.h> could have been removed, but it doesn't hurt as it is.

The next thing the code does is initialize the current thread's apartment state. If you don't know what an apartment state is, don't worry about it—it's a process threading thing for COM, and this book avoids COM because it isn't managed code. In fact, the .NET Framework doesn't use apartment threads, but just to be safe, the apartment state is set to a single-threaded apartment (STA) in case a COM object is wrapped and used later in the application.

Finally, the program uses the Application class to start up Form1. The Application class is a fairly powerful class containing several static methods and properties to manage an application. The most common tasks you will use it for are starting and stopping your applications and processing Windows messages. You may also find it useful for getting information about an application via its properties.

You know what? There wasn't much there, was there? Okay, maybe all the magic is in the Form1.h file (see Listing 9-2). To access the source code of Form1.h, you need to right-click Form1.h within Solution Explorer and select the View Code menu item. You can also right-click within the form designer window.

Listing 9-2: The Default Form1.h

start example
 #pragma once namespace Hello {     using namespace System;     using namespace System::ComponentModel;     using namespace System::Collections;     using namespace System::Windows::Forms;     using namespace System::Data;     using namespace System::Drawing;     public __gc class Form1 : public System::Windows::Forms::Form     {     public:         Form1(void)         {             InitializeComponent();         }     protected:         void Dispose(Boolean disposing)         {             if (disposing && components)             {                 components->Dispose();             }             __super::Dispose(disposing);         }     private:         System::ComponentModel::Container * components;         void InitializeComponent(void)         {             this->Size = System::Drawing::Size(300,300);             this->Text = S'Form1";         }     }; } 
end example

Believe it or not, this is complete Win Forms code. You want to know something else? If you code this by hand, all you need is this:

 #pragma once namespace Hello {     using namespace System;     using namespace System::Windows::Forms;     public __gc class Form1 : public Form     {     public:         Form1(void)         {             this->Size = Drawing::Size(300,300);             this->Text = S'Form1";         }     }; } 

All the rest of the code is for the design tool. Now this is simple! All the code does is specify the form's size and title. The rest is handled within the .NET Framework.

Okay, now for grins and giggles, change the title of the form to Hello World. To do this, just change the form's Text property. You can do this in a couple of ways. First, you can just type Hello World in the source code replacing the string Text property value Form1. Second, you can change the Text text box within the Properties view. Notice that if you change the property in one place, the other automatically gets updated as well.

As a thought, I guess the developers of the .NET Framework could have made things easier by calling this the Title property, but as you will soon see, the Text property is found in all Win Forms controls and is used for the default text-based property of the control.

When you finally finish staring in disbelief, go ahead and try compiling and running hello.exe. (Pressing Ctrl-F5 is the fastest way of doing this.) Rather unexciting, as you can see in Figure 9-2, but hey, what do you expect from two lines of relevant code?

click to expand
Figure 9-2: The "Hello World" form




Managed C++ and. NET Development
Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
ISBN: 1590590333
EAN: 2147483647
Year: 2005
Pages: 169

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