< C++ .NET Windows Form 1 | Main | C++ .NET Windows Form 3 >


 

 

More on Windows Forms 2

 

 

  1. What is a Message Loop?

  2. Using Form Properties

  3. Form Relationships

 

 

What's a Message Loop?

 

If you don't know how a Windows application works, here is a quick introduction. A message loop is at the heart of every Windows application, providing the 'pump' that drives the execution of the program. The parts of a Windows application, such as the forms, buttons, and scroll bars, communicate with each other, with other applications, and with the system by passing messages. In the world of Windows programming, a message is a small packet of data that is sent to a component to tell it something has happened. These somethings, called events, could include a timer going off, a key being pressed on the keyboard, or the user clicking a button. A message is a structure that describes an event, and Windows delivers the message to the appropriate application, placing it in the application's message queue. A tremendous number of events are delivered to a Windows application all the time, even when nothing much appears to be happening. At the heart of the application sits the message loop, a tight loop of code that removes one message at a time from the message queue and sends each off to the correct part of the application for processing.

In prehistoric times, when the only tools programmers had for writing Windows programs were a C compiler and a copy of the Windows SDK (Software Development Kit), you had to code the message loop manually and understand the architecture behind message processing. Now we have development frameworks such as MFC and the .NET Framework that do all the housekeeping for you, so you don't need to get into the details of how message processing happens. You can if you want to do advanced and clever things, but you don't have to for the vast majority of applications. So when you want to run a form as the GUI for your application, you need to start a message loop so that the form can process messages. You do so using the Application::Run function, which has the effect of both starting a message loop and displaying the form. The message loop keeps running until it receives a quit message, which can be sent from application code or by the operating system as a result of the user physically closing the window. In either case, the message loop terminates, the window closes, and the application exits.

 

Using Form Properties

 

Now that you've mastered the basics of displaying a form, let's move on to see how you can affect the way the form looks and behaves. When you examine the code in Form1.h, you'll see that the form is represented by a class that inherits from System::Windows::Forms::Form as shown below.

public ref class Form1 : public System::Windows::Forms::Form

{

     public:

        Form1(void)

...

...

You use this class as the base class whenever you want to create a form. The Form class has a large number of properties and methods, the most important of which are summarized in the following table.

 

Name

Method or Property

Description

AcceptButton

P

Gets or sets a reference to the button control that corresponds to the user pressing Enter.

Activate

M

Activates the window, bringing it to the front of the application's collection of windows.

ActiveForm

P

Gets the currently active form for this application, meaning the one at the front of the application's collection of windows.

AutoScale

P

Gets or sets a Boolean value indicating whether the form adjusts its size to fit the height of the font used on the form and scales its controls accordingly. The default is true.

AutoScroll

P

Gets or sets a value indicating whether the form displays scroll bars when controls fall outside the displayable area. The default is true.

CancelButton

P

Gets or sets a reference to the button control that corresponds to the user pressing Esc.

ClientSize

P

Gets or sets the size of the client area of the form. The client area is the portion of the form that excludes the title bar and borders.

Close

M

Closes the form and frees any resources the form has used.

DesktopLocation

P

Gets or sets the location of the form on the Windows desktop.

FormBorderStyle

P

Gets or sets the border style of the form. The default is FormBorderStyle::Sizeable.

HelpButton

P

Gets or sets a Boolean value indicating if the form is to display a Help button on the title bar. The default is false.

Icon

P

Gets or sets the icon associated with the form.

MaximizeBox

P

Gets or sets a Boolean value that indicates if the form is displaying a Maximize box on the title bar. The default is true.

Menu

P

Gets or sets a reference to the menu that is displayed on this form.

MinimizeBox

P

Gets or sets a Boolean value that indicates if the form is displaying a Minimize box on the title bar. The default is true.

OwnedForms

P

Holds the collection of child forms owned by this form, if any.

SetDesktopLocation

M

Sets the location of the form on the desktop.

ShowDialog

M

Shows the form as a modal dialog box.

ShowInTaskBar

P

Gets or sets a Boolean value, which is true if the form is to be shown in the Windows taskbar. The default is true.

Size

P

Gets or sets the form's size.

SizeGripStyle

P

Determines how (or even whether) the sizing grip is shown at the lower right of the form. The default is SizeGripStyle::Hide.

TopLevel

P

Gets or sets a Boolean value, which is true if the form is a top-level window, meaning that it has no parent other than the Windows desktop. The default is true.

TopMost

P

Gets or sets a Boolean value, which is true if the form is a topmost window, meaning it is always displayed on top of other windows, even when it doesn't have the focus. The default is false.

WindowState

P

Gets or sets the form's window state, which determines how the form is displayed: minimized, maximized, or normal. The default is FormWindowState::Normal.

 

Table 2

 

The Form class also has a very large number of methods and properties that it inherits from its base classes, which are shown here:

System.Object

   System.MarshalByRefObject

     System.ComponentModel.Component

       System.Windows.Forms.Control

         System.Windows.Forms.ScrollableControl

           System.Windows.Forms.ContainerControl

            System.Windows.Forms.Form

              Derived Classes

Notice especially the Component class, which forms the basis for all components that can be used with forms, and Control, which provides the base class for all visual components. All the base classes between them provide Form with approximately 110 properties, 180 methods, and 70 events! There are far too many to list here, so we suggest that you consult the .NET Framework documentation for more details. When we use inherited properties in the exercises or examples in this module, we'll tell you which base class they come from. By the way, when new version of the class library introduced, there will be many more. Although you can interact with the form's properties in code, the designer provides a Properties editor that gives you graphical access to all the most frequently used properties. The following exercise will show you how to use the Properties editor to edit the properties of a form so that you can make it appear where and how you want.

 

7.  Continue with the previous application, making sure that the design window is displayed. If the design window is not open, click the Form1.h file in Solution Explorer. You'll see that the form header file has a different icon from the other header and source files because it is the file that contains the definition of the form, and it's the file that is used by the designer.

 

Windows form solution explorer

 

8.  Right-click the form, and choose Properties from the context menu. The Properties editor will be displayed. Select the Text property, and change its value to give a TestForm caption to the form, as shown in the following figure.

 

Selecting and setting the Windows form Text property  

 

The Text property is inherited from Control, and it's being used here to set the text associated with the control. Many controls have some notion of an associated piece of text. In the case of an edit control, it's the text in the control; in the case of a button, it's the legend on the button. In the case of a form, it's the title displayed on the title bar of the form.

 

9.        If you rebuild and run the code, you'll see that the form now displays a caption.

 

Windows form output with caption

 

10.  The form border is represented by the FormBorderStyle property, which takes its value from the FormBorderStyle enumeration. The default border style is FormBorderStyle::Sizeable, which provides a simple border whose outline you can drag to change the size of the form. Use the Properties editor to change the border style to Fixed3D.

 

Seleting and setting a Windows form FormBorderStyle property

 

11.     Compile and run the code, and you'll see that the border style has changed and you can no longer resize the window, as shown in the following figure.

 

Windows form output with Fixed3D border style

 

12.     Take your time to experiment with other Form properties to change the look and feel of the form.

 

Form Relationships

 

Any form can create other forms, think of displaying a dialog box and by default, forms will be independent of one another so that you can minimize them and close them separately. There will be a parent-child relationship between the forms, but apart from that relationship, they are independent. Top-level forms, which are usually used for an application's main window, either do not have a parent form or have the desktop as a parent. It's also possible for one top-level form to be the owner of another top-level form, in which case there is a relationship between the two forms such that:

  1. The owned form is minimized, maximized, and hidden along with its owner.

  2. The owned form is closed when the owner form is closed.

  3. The owned form never displays behind the owner form.

Think of the Find And Replace dialog box in Microsoft Word as shown below. You can access the parent form without needing to close the child form.

 

Word Find and replace Windows Form

 

This window appears when you want to find something in the document, and it hovers over the Word window until you close it. If you minimize Word, the dialog box is minimized, and it disappears when you close Word.

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6

 


 

< C++ .NET Windows Form 1 | Main | C++ .NET Windows Form 3 >