Customizing the Form


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:

  • Sized

  • Minimized

  • Maximized

  • Moved

  • Closed

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:

  • AutoScroll is a Boolean that specifies if the form should automatically display scroll bars if sizing the window obscures a displayable area. The default value is true.

  • ClientSize is a System::Drawing::Size that specifies the size of the client area. The client area is the size of the window within the border and caption bar. You use this control to adjust the size of the window to your liking or to get the dimensions of it for GDI+ drawing. You will examine GDI+ in Chapter 11.

  • Cursor is a Cursor control that you use to specify the cursor to display when over the Win Form. The default is conveniently named Cursors::Default.

  • FormBorder is a FormBorderStyle enum that specifies the style of the border. You use this control to change the look of the form. Common styles are FixedDialog, FixedToolWindow, and SizableToolWindow, but the style you will see most often is the default Sizable.

  • Icon is a System::Drawing::Icon that you use to specify the icon associated with the form.

  • MaximizeBox is a Boolean that specifies if the maximize button should be displayed on the caption bar. The default is true.

  • Menu is a MainMenu control you use as the menu displayed on the form. The default is null, which signifies that there is no menu.

  • MinimizeBox is a Boolean that specifies if the minimize button should be displayed on the caption bar. The default is true.

  • Size is a System::Drawing::Size that specifies the size of the form. The size of the window includes the borders and caption bar. You use this control to set the size of the Win Form.

  • WindowState is a FormWindowState enum that allows you to find out or specify if the Win Form is displayed as Normal, Minimized, or Maximized. The default window state is FormWindowState::Normal.

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.

click to expand
Figure 9-3: Customizing Form1 using the Properties view

Listing 9-3: Customizing Form1.h

start example
 #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;         }     }; } 
end example

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.

click to expand
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.




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