Home

Introduction to Applications

 

Windows Fundamentals

 

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 files, functions, objects, resources, data types, and constants that define how the Microsoft Windows family of 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 as much as possible about Win32 to be an effective Windows programmer.

 

In order to create a Win32 application in C++Builder, on the main menu, you can click File -> New -> Other... In the left list of the New Items dialog box, click, you can click C++Builder Projects. On the right list, click Console Application and click OK. From the New Console Application dialog box, click the C++ radio button and make sure the Console Application check box is unchecked.

C++ in a VCL Application

As you may know already, you will use your knowledge of C++ to develop C++Builder applications. This includes the ability to create objects, declare variables, create classes, use your own classes, use or use classes created by other people, etc. Most of the classes you will use have already been created. Still, in some cases, you will want to create your own classes. You can separately create a header file and a source file or create a unit that becomes a class. To create any of these, use the New Items dialog box. In the left list, click C++Builder Files. In the right list, make your choice and click OK.

To manage classes, you can use the Class Explorer. To get it, on the main menu, you can click View -> C++ Class Explorer.

The WinMain Function

As every C++ application specifies the main() function as its entry point, every Windows application must have a function called WinMain. If you want to support Unicode (namely long strings) in your application, use _tWinMain. If you start a Win32 application using the New Console Application, C++Builder would display syntax of the _tWinMain() function. This is:

WINAPI _tWinMain(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 _tWinMain() function.

Practical LearningPractical Learning: Creating an Application

  1. To start C++Builder, click Start -> (All) Programs -> Embarcadero RAD Studio 2010 -> C++Builder 2010
  2. On the main menu, click File -> New -> Other... 
  3. On the New Items dialog box, click Console Application
     
    New Items
  4. Click OK
  5. In the New Console Application dialog box, accept the C++ radio button and the Use VCL check box. Clear the check mark on the Console Application chez box
     
  6. Click OK
  7. To save the application, on the Standard toolbar, click the Save All button
  8. On the toolbar of the dialog box, click the Create New Folder button
  9. Type Exercise and press Enter to display it in the Save In combo box
  10. Change the name of the Unit1.cpp file to Example and click Save
  11. Change the name of the project to Exercise and press Enter
  12. In the Exerice.cpp file, notice that the skeleton _tWinMain function has been created for you.
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #include <windows.h>
    #pragma hdrstop
    
    #include <tchar.h>
    //---------------------------------------------------------------------------
    
    #pragma argsused
    WINAPI _tWinMain(HINSTANCE hInstance,
    		 HINSTANCE hPrevInstance,
    		 LPSTR lpCmdLine,
    		 int nCmdShow)
    {
    	return 0;
    }
    //---------------------------------------------------------------------------

Applications Fundamentals

 

Introduction to VCL Objects

To make application development easier, that is, to provide Rapid Application Development (RAD), the Visual Component Library (VCL) is equipped with various classes. The most fundamental class is named TObject.

VCL Inheritance

This is the ancestor to all VCL classes you will use in an application. The TObject class is defined in the System.hpp header file. As the parent of all VCL classes, it gives some common functionalities to its children, including:

  1. The ability to create an object, thereby allocating the necessary memory resources needed during the lifetime of the object
  2. The ability to maintain an object. That is, to be able to locate it, both by the application and the operating system. This also makes it possible for other objects of the same application or for the operating system to be aware of the presence of the object
  3. The ability to destroy an object when it is not needed anymore, thereby freeing the resources the objects was using and make these resources available to the operating system and consequently to other objects and application

You will hardly, if ever, need to declare a variable of type TObject. Instead, if necessary, you can create a class that derives from TObject. The TObject class is equipped with many methods and no properties.

Introduction to Object Persistence

Persistence is the ability to write or to read a value. The value can be written to an object. The object can be physical such as a hard drive or a DVD drive, or it an be virtual such as a variable. In the same way, the value can be read from the same type of object. These two operations make it possible to share values, such as to assign one object to another. To provide this functionality to objects, the VCL provides a class named TPersistent. The TPersistent class is defined in the Classes.hpp header class.

The TPersistent class is derived from TObject:

TPersistent Inheritance

TPersistent is the ancestor to all classes that use properties, so these properties can be written to or read from. The TPersistent class is equipped with a method named Assign. Its syntax is:

virtual void __fastcall Assign(Classes::TPersistent * Source);

This method makes it possible to assign an object to another another. You can either use this method or the assignment operator "=".

Introduction to Components

A component is an object that is used in a graphical application. Examples of components are buttons, pictures, check boxes, mouse cursor, or lists. The most fundamental aspect of a component is that it must be created. The most fundamental object of a computer is the operating system. It gives life to the computer and the objects that would appear on it. Most of these objects are seen on the screen. In fact, as far as a user is concerned, an application is a series of objects that can be seen and used. Some objects can carry other objects, which means they act as parents. An object A that holds or carries another object B is referred to as its owner.

To support these various scenarios, the ability for a component to appear in an application and the ability for one component to own another component, the VCL provides a class named TComponent. The TComponent class is defined in the Classes.hpp header file.

The TComponent class is derived from TPersistent:

VCL Inheritance

TComponent is the ancestor for objects that appear in an application. The TComponent class is equipped with many properties and methods.

As mentioned already, one of the characteristics of a component is that it can own other components. To give you access to the objects that a component owns, the TComponent class is equipped with a property named Components:

__property Classes::TComponent * Components = {read=GetComponent};

The TComponent::Components property is a collection or array where each element is an object on the component. One of the properties of the TComponent class is named ComponentCount:

__property int ComponentCount = {read=GetComponentCount};

TComponent::ComponentCount is a read-only property that produces the number of objects inside a component.

Introduction to Controls

In a computer application, a control is an object that appears, or has a physical presence, on the screen. To make this possible, a control presents some visual characteristics such as borders or colors. A control can also exhibit a behavior, such as the ability to move or change.

To support the concept of controls, the VCL provides a class named TControl. The TControl class is derived from TComponent:

TControl Inheritance

The TControl class is defined in the Controls.hpp header. The TControl class is equipped with many properties and methods that we cannot review at this time.

Introduction to Windows Controls

A Windows control is an object that can react to a user. For example, it can receive key strokes (from the keyboard) or can respond when a mouse acts on it. Examples of Windows controls are buttons, list views, check boxes, menus, radio buttons, dialog boxes, or scroll bars.

To support Windows controls, the VCL provides a class named TWinControl. The TWinControl class is derived from TControl:

TWinControl

The TWinControl class is defined in the Controls.hpp header. The TWinControl class is equipped with many properties and methods that we cannot review at this time.

  

Introduction to Application Initialization

A computer application, also called an application, is a series of instructions that tell the computer what to do, when an how. These instructions are group in one entity named a project. In Microsoft Windows, to create an application, you use one of two structures named WNDCLASS and WNDCLASSEX. These two structures are defined respectively as follows:

typedef struct _WNDCLASS {
     UINT       style;
     WNDPROC    lpfnWndProc;
     int        cbClsExtra;
     int        cbWndExtra;
     HINSTANCE  hInstance;
     HICON      hIcon;
     HCURSOR    hCursor;
     HBRUSH     hbrBackground;
     LPCTSTR    lpszMenuName;
     LPCTSTR    lpszClassName;
 } WNDCLASS, *PWNDCLASS;
typedef struct _WNDCLASSEX {
	UINT cbSize;
	UINT style;
 	WNDPROC lpfnWndProc;
 	int cbClsExtra;
 	int cbWndExtra;
 	HINSTANCE hInstance;
 	HICON hIcon;
 	HCURSOR hCursor;
 	HBRUSH hbrBackground;
 	LPCTSTR lpszMenuName;
 	LPCTSTR lpszClassName;
 	HICON hIconSm;
} WNDCLASSEX, *PWNDCLASSEX;

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.

To support applications, or to simplify the creation of an application, the VCL provides a class named TApplication. The TApplication class is derived from the TComponent class:

TApplication Inheritance

The TApplication class is defined in the Forms.hpp header file. TApplication is used to create, or get access to, an application. It is the main central point of an application as a Windows entity.

To give you ample access to the application and to the screen, every GUI application you develop using the VCL declares a TApplication variable. This means that you will hardly, if ever, need to declare a variable of this class since it is already available to your application.

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.

Applicationís Instance

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. Once the instance exists, it can be accessed either by any control of the same application or by the operating system.

When you create a VCL application, a variable of type TApplication is declared and the instance of the application is globally made available to anything that needs it. The instance of the application is called HInstance.

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.

Practical LearningPractical Learning: Creating an Application

  • In the body of the WinMain() function, call the TApplication::Initialize() method before the return line:
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #include <windows.h>
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    		LPSTR lpCmdLine, int nCmdShow)
    {
    	Application->Initialize();
    
    	return 0;
    }
    //---------------------------------------------------------------------------

Introduction to Application's Screen

The screen is the monitor that allows a person to visually interact with the machine. The screen includes such information as the computerís desktop, its size, the display monitor, and other pieces of information related to a screen as an object or related to the objects of an application.

To give you information about the screen that is being used in your application, the VCL provides the TScreen class. The TScreen class is derived from TComponent:

TScreen Inheritance

As mentioned for the TApplication class, when you create a VCL project a global TScreen variable is automatically declared so you will never need to explicitly create a TScreen object.

Creating and Opening a VCL Forms Application

 

Creating a Project

Rapid application programming (RAP) and rapid application development (RAD) consists of visually creating a computer programm by adding objects to the projects and writing code only when necessary. This makes the development go fast and allows the programmer to regularly preview the result. Although you can manually create a fully functional application in Embarcadero RAD Studio, the goal of using it is to take advantage of its rich design interface and objects. For this reason, for the rest of our lessons, we will use RAD as much as possible.

There are many ways you can start a visual application in Embarcadero RAD Studio:

  1. At any time, on the main menu, you can click File -> New -> VCL Forms Application - C++Builder
  2. You can use the New Items dialog box. On the main manu, you can click File -> New -> Other, or click the New Items button New Items on the Standard toolbar. In the left section of the New Items dialog box, click C++Builder Projects. In the right list, double-click VCL Forms Application
     
    New Items
  3. If no project is currently being developped, you can use the Tool Palette. Click C++Builder Projects to expand. Then double-click the VCL Forms Application icon
     
    VCL Forms Application

Any of these actions would create a new project display a starting form.

Opening a Project

To open a project:

  • On the main menu, click File -> Open Project...
  • On the Standard toolbar, click the Open Project button Open Project
 
 
 
 

Previous Copyright © 2010-2011 FunctionX, Inc. Next