Introduction to the Win32 Library
A library is a set of published documents. It could consist of a piece of paper, a book, or a group of books used as a (written) reference for a specific purpose. The programs that are on a Microsoft Windows operating system follow a set of rules and suggestions defined in a library called Win32 (Windows 32-bits). Win32 is a group of functions, objects, variables, and constants that define how the Microsoft Windows (95 and above) operating systems function. As a reference, it allows individuals and companies to know what is necessary in order to create or develop applications for this specific platform.
Over all, it is not strictly necessary to know Win32 thoroughly to write programs; but it is highly recommended to know most of the intricacies of its implementations. You should know as much as possible about Win32 to be an effective Windows programmer.
In order to create a Win32 application in Borland C++ Builder, you would call the New Items dialog box. From the General property page, you can click the Console Application icon and click OK. From the Console Wizard dialog box, click the C++ radio button and make sure the Console Application check box is unchecked.
As every C++ application specifies the main() function as its entry point, every Windows application must have a function called WinMain. If you start a Win32 application using the Console Wizard, Borland C++ Builder would display syntax of the WinMain() function. This is:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
If you start a default graphical application, a source file with the name of the project would be created and it would have this syntax of the WinMain() function:
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
To make application development easier, that is, to provide Rapid Application Development (RAD), the Visual Component Library (VCL) is equipped with various classes. To start a computer application in a Win32 environment, you must create an application. This is done using the WNDCLASS or the WNDCLASSEX structure. These two structures are defined as follows:
For a Win32 application, you must declare a variable of one of these structures. Then you should provide or at least control the value of each of the member variables. Fortunately, to simplify this process, the VCL provides two fundamental classes used to create an application. They are
TApplication and TScreen.
To create an application, the TApplication class provides the Initialize() method. Its syntax is simply:
void __fastcall Initialize(void);
This creates an empty application without much to do.
If you start an application such as Notepad, you are said to have created an instance of the application. In the same way, when you declare a variable of a class, an instance of the class is created and made available to the project. In fact, when you create an application, you must create an instance of that application so it can be made available to the operating system. Such an instance must be passed to the
WinMain() function. Once the instance exists, it can be accessed either by any control of the same application or by the operating system for any reason.
After creating an application, you can create an object that the user will look at when interacting with the computer. Such an object is called a window. Most of the things you see on the screen or on an application are window objects. As various as they are, there are different techniques used to create them.
void __fastcall CreateForm(System::TMetaClass* InstanceClass, void *Reference);
The name of the form is passed as pointer as the second argument. Because the form is being added to the application, giving the application access to it for further operations and processing, the CreateForm() method requires that the class that controls the form be specified. This is normally done by calling the __classid() operator. Once you have specified the form to the application, you must also specify the name of the source file that holds the implementation of the form. This is done by calling the USEFORM macro. Its syntax is:
This macro takes two arguments. The first is a string that specifies the source file. The second specifies the name of the form as it appears in the project.
void __fastcall Run(void);
Therefore, an application should (must) call this method to process its assignment. If Run() is not called, the application will compile and execute fine but because it cannot process messages, it will not display anything (displaying something on the screen is a message by itself and must be processed).
To create your applications, there are two settings you will be using. If a control is displaying on the screen and you are designing it, this is referred to as Design Time. This means that you have the ability to manipulate the control. You can visually set the control’s appearance, its location, its size, and other necessary or available characteristics. The design view is usually the most used and the easiest because you can glance at a control, have a realistic display of the control and configure its properties. The visual design is the technique that allows you to visually add a control and manipulate its display. This is the most common, the most regularly used, and the easiest technique.
The other system you will be using to control a window is with code, writing the program. This is done by typing commands or instructions using the keyboard. This is considered, or referred to, as Run Time. This is the only way you can control an object’s behavior while the user is interacting with the computer and your program.
A Windows control is a graphical object that allows the user to interact with the computer. The controls are as varied as the needs and goals are. Because there are so many controls for various purposes, their design and creation are left to the computer programmer.
When you start Borland C++ Builder, it creates and displays an empty form. It also provides its various controls on the Tool Palette so you can choose which ones are appropriate for your particular application. Otherwise, to create a graphical application, you can click File -> New -> Application from the main menu. You can also click the New button on the Standard toolbar. From the New Items dialog box, also called the Object Repository, you can click the type of application or file you would like to create.
Controls can be set by categories based on their function or role. A container is a control whose main purpose is to host other controls. To design it, you pick up objects from the Tool Palette and drop them where desired. The most common and the most widely used container is the form. In Borland C++ Builder, a form can be configured to display like a regular dialog box, to become a Single Document Interface (SDI) or to participate in a Multiple Document Interface (MDI) application.
All of the applications we will create in this book qualify as graphical user interface (GUI). A GUI application is one that displays Windows controls to the user who acts on them to interact with the computer.
When creating an application, you will be adding Windows controls, also called controls, to it. Most of the controls you will use reside on the Tool Palette:
There are various sections of objects on the Tool Palette. Each section has a header that displays either a - or a + button. This allows you display or to hide the items in a section. To display the items of a section, you must expand, which you can do by clicking its + button. To hide the items of a section, you must collapse it, which you can do by clicking its - button.
Some sections of the Tool Palette holds the Windows controls you will use to create or design an application. The controls on the Tool Palette are grouped into categories:
As mentioned already, you can expand or collapse a section at will.
The controls are represented each by a specific button. Some of the buttons display an appearance that easily suggests their role. Some others may not be obviously identified. In any case, to find out what control a button represents, you can position your mouse on it. A small yellow box called a tool tip or a hint would display. With experience, you will find out that the hint reflects the actual name of the control:
Rapid Application Development (RAD) consists of selecting the controls that are necessary for your application and use them as you see fit. There are various techniques you can use to add a control to your application.
Creating a good-looking application is a combination of art, intuition, and logic. Not all IDEs provide the same features but Borland C++ Builder is enhanced on this aspect. We will learn how to configure various controls as we move on. The first thing to do is to get familiar with what can ease the user’s experience with your application.
When designing an application, you will usually position the desired controls on a form. You will you decide what control you need, where it will be positioned on the form and with regard to other controls, what appearance it should have, and what behavior should be customized.
While you are designing a form and it is hosting controls, it is important to always know what particular control is selected. Whenever you make changes, they are applied to the control that has focus.
There are various techniques used to give focus to a control:
To select a form, click an unoccupied area on the form. If the form is hidden, you can press F12 to toggle between the form and the code. If your application has more than one form, to display one of them, on the main menu, you can click View -> Forms and select the desired form from the list.
Every object of an application must have a name. This also applies to the Windows controls you add to your application. When you create a form, Borland C++ Builder assigns it a default name, such as Form1, or Form2. In the same way, if you add a control to a form, the control receives a default name, such as Edit1 or Panel1. Most of the time, you should change that name to reflect the role of the control in your application.
To change the name of a control, first select it. Then, in the Object Inspector, click the Name field and type the desired name. Each control in the same application must have a unique name.
Any control on a form can be moved to a desired location either to accommodate other controls or based on new demands:
There are two valuable dialog boxes available to move controls. To solve alignment issues, access the Align dialog box from the main menu by clicking Edit -> Align…
To better control the relative alignment of controls, you can use the Alignment toolbar. To access it, on the main menu, you can click View -> Toolbars -> Align:
Because it may be equipped with buttons you are not familiar with, to know what a button is for, you can position the mouse on it and wait for the tool tip.
When dealing with relative dimensions, call the Size dialog by clicking Edit -> Size…
Most controls, including the form, can be resized using guiding mouse cursors. To resize a control, first select it. Except for the form, whenever a control is selected, there are eight handles around it. To resize the control, position your mouse on one of the handles. The mouse pointer will change, indicating in what direction you can move to resize the control
To resize the control by one grid unit, click one of the handles and drag in the desired location. Once satisfied, release the mouse.
To resize a control by the small possible unit, select the control. Press and hold Alt. Then click the desired handle and drag in the desired direction. Once satisfied, release the mouse and Alt.
Controls navigation has to deal with not only the alignment of controls on a form but also the logical process the user should follow when using your product. The Tab key on your keyboard is the primary means for users to navigate from one control to another. The tab order is the sequence of controls that the keyboard follows when the user presses the Tab key. Sometimes, after creating a form, this navigation sequence does not follow the right logic. You usually have to adjust that sequence. This is done from the Edit Tab Order dialog box.
As seen above, in a RAD environment, the primary means of adding a control to an application consists of visually adding it to a container such as a form. In the world of Win32 programming, a control can only be created programmatically. In a VCL application, you can either add a control at design time or programmatically create it.
To programmatically create a control in a VCL application, you must know its base class. This is fortunately very easy to find out. If the control is one of the types of objects of the Tool Palette, you can use the exact name that displays. Examples are TEdit, TPanel, or TMemo.
The VCL requires that a dynamic control be declared as a pointer.
A property is a piece of information that characterizes a control. It could be related to its location or size. It could be its color, its identification, or any visual aspect that gives it presence on the screen.
The properties of an object can be changed either at design time or at run time. You can also manipulate these characteristics both at design and at run times. This means that you can set some properties at design time and some others at run time.
To manipulate the properties of a control at design time, first click the desired property from the Tool Palette. Then add it to the form or to a container control. To change the properties of a control at design time, on the form, click the control to select it. Then use the Object Inspector:
Each field on the Object Inspector has two sections: the property’s name and the property's value:
The box on the right side of each name represents the value of the property that you can set for an object. There are various kinds of fields you will use to set the properties. To know what particular kind a field is, you can click its name. But to set or change a property, you use the box on the right side of the property’s name: the property's value, also referred to as the field's value.
Empty fields: By default, these fields have nothing in their properties. Most of these properties are dependent on other settings of your program. For example, you can set a menu property for a form only after you have created a menu.
To set the property on such a field, you can type in it or select from a list. If you type an invalid value and press Enter, you would receive an "Invalid Property Value" error:
Text Fields: There are fields that expect you to type a value. Most of these fields have a default value. To change the value of the property, click the name of the property, type the desired value, and press Enter. While some properties, such as the Caption, would allow anything, some other fields expect a specific type of text, such as a numeric value. If you type an invalid value, you would receive an error:
You can click OK to dismiss the error dialog and type a new valid value.
To collapse the field, click the – button. Some of the set properties are created from an enumerator. Since the field was created as a Pascal-style set, this allows the property to have more than one value. For example, the BorderIcons property can have the minimize and the maximum buttons. Some other fields that have a + button are actually complete classes or a combination of classes.
There are various types of selection fields. Some of them display just two items. To change their value, you can just double-click the field. Some other fields have more than two values in the field. To change them, you can click their arrow and select from the list. You can also double-click a few times until the desired value is selected. On some other properties, you can double-click the field and a dialog box would come up.
|Previous||Copyright © 2004-2007 Yevol||Next|