Step 1: Creating a basic application

Back Home Up Next

To begin the tutorial, open the file STEP01.CPP, which shows an example of the most basic useful ObjectWindows application. Because of its brevity, the entire file is shown here: You can find the source for Step 1 in the file STEP01.CPP in the directory EXAMPLES\OWL\TUTORIAL.

//-----------------------------------------------------------------------
// ObjectWindows - (C) Copyright 1991, 1994 by Borland International
// Tutorial application -- step01.cpp
//-----------------------------------------------------------------------
#include <owl/applicat.h>
#include <owl/framewin.h>

class TDrawApp : public TApplication
{
  public:
    TDrawApp() : TApplication() {}

    void InitMainWindow()
    {
        SetMainWindow(new TFrameWindow(0, "Sample ObjectWindows Program"));
    }
};

int
OwlMain(int /* argc */, char* /* argv */ [])
{
  return TDrawApp().Run();
}

This simple application includes a number of important features:

This source file includes two header files, owl\applicat.h and owl\framewin.h. These files are included because the application uses the TApplication and TFrameWindow ObjectWindows classes. Whenever you use an ObjectWindows class you must include the proper header files so your code compiles properly.
The class TDrawApp is derived from the ObjectWindows TApplication class. Every ObjectWindows application has a TApplication object-or more usually, a TApplication-derived object-generically known as the application object. If you try to use a TApplication object directly, you'll find that it's difficult to direct the program flow. Overriding TApplication gives you access to the workings of the application object and lets you override the necessary functions to make the application work the way you want.
In addition to an application object, every ObjectWindows application has an OwlMain function. The application object is actually created in the OwlMain function with a simple declaration. OwlMain is the ObjectWindows equivalent of the WinMain function in a regular Windows application. You can use OwlMain to check command-line arguments, set up global data, and anything else you want taken care of before the application begins execution.
To start execution of the application, call the application object's Run function. The Run function first calls the InitApplication function, but only if this instance of the application is the first instance (the default TApplication::InitApplication function does nothing). After the InitApplication function returns, Run calls the InitInstance function, which initializes each instance of an application. The default TApplication::InitInstance calls the function InitMainWindow, which initializes the application's main window, then creates and displays the main window.
TDrawApp overrides the InitMainWindow function. You can use this function to design the main window however you want it. The SetMainWindow function sets the application's main window to a TFrameWindow or TFrameWindow-derived object passed to the function. In this case, simply create a new TFrameWindow with no parent (the first parameter of the TFrameWindow is a pointer to the window's parent) and the title "Sample ObjectWindows Program."

This basic application introduces two of the most important concepts in ObjectWindows programming. As simple as it seems, deriving a class from TApplication and overriding the InitMainWindow function gives you quite a bit of control over application execution. As you'll see in later steps, you can easily craft a large and complex application from this simple beginning.

Where to find more information

Here's a guide to where you can find more information on the topics introduced in this step:

Application objects, along with their Init* member functions, are discussed in the chapter "Application and module objects" of the ObjectWindows Programmer's Guide.
OwlMain is discussed in "Application and module objects" of the ObjectWindows Programmer's Guide.
TFrameWindow is discussed in the chapter "Window objects" of the ObjectWindows Programmer's Guide.
 


Copyright © 1998-2001 Yura Bidus. All rights reserved.