Managed C++ and .NET Development: Visual Studio .NET 2003 Edition

A form by itself is not the most exciting thing, but before you move on and give it some functionality, let's look at what you'll be getting in the default form. Then let's see what else you can customize.

So what do you get for free with a form? Among many things, you get the following:

It displays an icon, provides a control box, and does a lot of stuff in the background such as change the cursor when appropriate and take Windows messages and convert them into .NET events.

The Form is also very customizable. By manipulating a few of the Form's properties you can get a completely different look from the default, along with some additional functionality that was disabled in the default form configuration. Some of the more common properties are as follows:

There's nothing special about working with Form class properties. You can either change them using the Properties view as shown in Figure 9-3 or directly in code as Listing 9-3 points out. The choice is yours. Frequently you'll start off by making general changes using the Properties window and then go into the code's InitializeComponent() method (which you can find in the Form1.h file for all the examples in the book) to fine-tune the changes. It doesn't really matter if you make the changes in the code or in the Properties window, as any changes you make in one will immediately be reflected in the other.

Figure 9-3: Customizing Form1 using the Properties view

Listing 9-3: Customizing Form1.h

#pragma once namespace CustomHello { 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->AutoScaleBaseSize = System::Drawing::Size(6, 15); this->BackColor = System::Drawing::Color::Black; this->ClientSize = System::Drawing::Size(692, 272); this->Cursor = System::Windows::Forms::Cursors::UpArrow; this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::SizableToolWindow; this->Name = S"Form1"; this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Show; this->Text = S"Custom Form"; this->TopMost = true; } }; }

Caution

Be careful when you make changes within the InitializeComponent() method. The changes have to be made in exactly the same manner as the code generator or you may cause Visual Studio .NET's GUI design tool to stop functioning.

To customize a form (or any other control, for that matter), you just assign the appropriate types and values you want to the properties and let the form handle the rest. The example in Figure 9-3 and Listing 9-3 shows a hodgepodge of different form customizations just to see what the form will look like when it's done. The biggest change happened when I changed FormBorderStyle.

Tip

Properties that differ from the default appear in boldface within the Properties view.

Running CustomHello.exe results in the display in Figure 9-4. Notice that this form is quite a bit different from the default form generated by the previous example Hello.exe. For example, this form has no control box and no minimize or maximize buttons, and in the bottom right there is a form-sizing grip and an up-arrow cursor.

Figure 9-4: A very customized form

Note

For the rest of the chapter I will not list the .cpp file or repeat the constructor or dispose methods (unless something changes within them), as they are the same for every example.

Категории