Creating the Windows Form


The first step in writing a Windows Form application is to include the appropriate .NET Framework components that support the Windows Forms. Add the following lines to the SimpleWindowsForm.cpp file after the #using <mscrolib.dll> statement:

 #using <System.DLL> #using <System.Drawing.DLL> #using <System.Windows.Forms.DLL> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; 

As was mentioned earlier, these statements import the portions of the .NET Framework needed and also indicate which namespaces you will use when declaring the Windows Form class. System.DLL is a component that includes all the system-level .NET Framework classes. System.Drawing.DLL is a component that deals with coordinates and other drawing capabilities in .NET. The coordinates' classes are what you need for setting up a Windows Form. The final component, System.Windows.Forms.DLL, includes the support for the Windows Forms' classes.

Creating the Windows Form Class

The next step in creating a Windows Form is to actually declare the managed Windows Form class. As you learned earlier, you need to declare the class with the __gc specifier to mark the class as managed. Declare a class named SimpleWindowsForm, as shown in Listing 7.2, with the typical constructor/destructor pairs. In order for this class to be used within your main function however, you must place the class declaration above the main function.

Listing 7.2 SimpleWindowsForm Class Declaration
 1: __gc class SimpleWindowsForm : public Form  2: {  3: public:  4:    SimpleWindowsForm();  5:    virtual ~SimpleWindowsForm() {}  6: };  7:  8: SimpleWindowsForm::SimpleWindowsForm()  9: { 10: 11: } 

Initializing the Windows Form

Because there's not a resource file to define a dialog template to initialize the Windows Form and its associated controls, you must create and initialize them using C++ source code. The initialization of the Windows Form occurs when the class is first constructed; otherwise, the form would display with nothing. It is generally good practice to create an InitForm() method that is called from within the constructor of the class, as Listing 7.3 shows. This allows you to easily find the form properties because they reside within a single function. The constructor may contain code for initializing member variables or calling other functions, so its best to not have to search for your form's properties through all the other code that's present.

Listing 7.3 SimpleWindowsForm Class Declaration with Basic Methods and Members
 1: __gc class SimpleWindowsForm : public Form  2: {  3: protected:  4:    void InitForm();  5:  6: public:  7:    SimpleWindowsForm();  8:    virtual ~SimpleWindowsForm() {}  9: }; 10: 11: 12: SimpleWindowsForm::SimpleWindowsForm() 13: { 14:    // Initialize the Windows Form 15:    InitForm(); 16: } 17: 18: 19: void SimpleWindowsForm::InitForm() 20: { 21: } 

The code shown in Listing 7.3 is the basic class for any Windows Form you create throughout the rest of this hour and in the coming hours. From this point, depending on how the form is initialized, the Windows Form can be anything and contain anything of which it is capable.

This form is going to be a simple Windows Form with a "Hello World" message and an OK push button. First, you need to declare the class members to store pointers to the Windows Form controls. Therefore, add the following declarations to the SimpleWindowsForm class:

 Label*  m_pHelloWorld_L; Button* m_pOK_B; 

In order to immediately know what type of control a member variable represents, you can use the naming scheme for member variables, as shown in the previous code snippet. The L for the m_pHelloWorld_L member variable means that the variable is a label, and likewise, the B after the m_pOK_B variable means that it is a Button.

Creating and initializing the controls within the Windows Form is straightforward enough; however, the screen layout is a matter of trial and error. All the coordinates and sizes are in pixels; therefore, you need to determine the right location and size for each control and initialize its properties. Listing 7.4 shows the modified InitForm() method, which creates and initializes the controls. Figure 7.1 shows the resulting Windows Form.

Figure 7.1. The displayed Windows Form defined by the SimpleWindowsForm class.


Listing 7.4 The SimpleWindowsForm::InitForm() Method Definition with Windows Form Control Initialization
 1: void SimpleWindowsForm::InitForm()  2: {  3:    // Allocate controls  4:    m_pHelloWorld_L = new Label;  5:    m_pOK_B         = new Button;  6:  7:    // m_pHelloWorld_L Initialization  8:    m_pHelloWorld_L->Location = Point(8, 20);  9:    m_pHelloWorld_L->TabIndex = 1; 10:    m_pHelloWorld_L->TabStop = false; 11:    m_pHelloWorld_L->Text = S"Hello World!"; 12:    m_pHelloWorld_L->Size = System::Drawing::Size(100, 16); 13: 14:    // m_pOK_B Initialization 15:    m_pOK_B->Location = Point(100, 50); 16:    m_pOK_B->TabIndex = 2; 17:    m_pOK_B->TabStop = true; 18:    m_pOK_B->Text = "OK"; 19:    m_pOK_B->Size = System::Drawing::Size(70, 20); 20: 21:    // Add controls to the Form 22:    Controls->Add( m_pHelloWorld_L ); 23:    Controls->Add( m_pOK_B ); 24: 25:    // Initialize Form attributes 26:    Size = System::Drawing::Size(200, 110); 27:    Text = "Simple Form"; 28: } 

As you can see from the code in Listing 7.4, every aspect of each control and the form itself is controlled through the class properties of the control and form. Therefore, you can easily generate forms dynamically at runtime. Also, in case you are wondering where the objects are deleted, note that the class is managed by the common language runtime (CLR). Therefore, deleting the objects you create is not necessary.

Completing the Application

At this point, you have created a class that represents a Windows Form, but the application doesn't know how to display the form yet, so you cannot run the application and see the form. This is accomplished through a simple change to the main() function. Instead of displaying a string to the console window, you need to tell the .NET Framework to run the Windows Form you just created. When you change the main() function to resemble the one shown in Listing 7.5, the .NET Framework will start a message loop for the SimpleWindowsForm object passed in as the parameter to the static Application::Run() method.

Listing 7.5 Modifications to the main() Function to Run the Application with a SimpleWindowsForm Object
int _tmain(void) {     Application::Run( new SimpleWindowsForm() );     return 0; } 

That's it. There's no message loop to code and no registering of window classes. Compile the application and run it to see the dialog application shown in Figure 7.1.

Making the Push Button Work

After running the application, you are probably wondering why the OK push button doesn't work. It is because there is no code to handle the push button in the application. Unlike MFC applications, where the OK and Cancel push buttons are handled by the CDialog class, the .NET Framework doesn't recognize these buttons from others.

Push buttons and other controls in the .NET Framework provide events when they are clicked or some other event occurs. Instead of capturing those events with a message map, as you do in MFC, you need to handle the events by binding them to a class method by passing a function pointer to the button control that it calls when the event you wish to handle occurs. This function pointer is also known as a delegate in the .NET Framework.

Start by adding the following protected method declaration to the SimpleWindowsForm class:

 void OnOK(Object* pSender, EventArgs* pEventArgs); 

Then add the following statement to the InitForm() method after the other m_pOK_B-> statements:

 m_pOK_B->add_Click( new EventHandler( this, OnOK ) ); 

This statement ties the Click event of the push button to the event handler OnOK(). Other events for controls are added in the exact same way, except there are other methods to represent each event. For example, add_DoubleClick() adds an event handler for the double-clicking of a control. You can also tie more than one event to a single event handler.

The OnOK() method is defined with the following statements, which call the Close() method. This method closes the Windows Form, and control is passed back to the main() function. Here's the code:

 void SimpleWindowsForm::OnOK(Object* pSender, EventArgs* pEventArgs) {    // Close the Windows Form    Close(); } 

Now that all the necessary code is in, you can build your project and run the application. If everything works correctly, you should be able to click the OK button, which closes the form.


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: