Hello World Version 1: Creating a Main Window

Version 1 of the Hello World sample application shows you how to create a main window and insert a text string into it using the static text control. A static text control is a text field, bitmap, icon, or box that you can use to label or box another control. In version 1, the "Hello World!!!" text string is inserted into a static text control.

Version 1 shows you how to do the following:

  1. Create the main window
  2. Create a static text control
  3. Set the focus and show the main window

The main window for version 1 of the application looks like this:


Listing the Hello World Version 1 Files

The ahellow1.cpp file contains the source code for the main procedure, which is described later in this chapter.

File Type of Code
ahellow1.cpp Source code for the main procedure
ahellow1.h Symbolic definitions file for the hello1 executable file
ahellow1.rc Resource file for the hello1 executable file

The Primary Source Code File

The ahellow1.cpp file contains the source code used for version 1. Here is a listing of the source code:

#include <iapp.hpp>
#include <istattxt.hpp>
#include <iframe.hpp>
#include <icoordsy.hpp>
#include "ahellow1.h"

/**************************************************************************
* main  - Application entry point for Hello World Version 1.              *
*                                                                         *
* Creates a new object mainWindow of class IFrameWindow                   *
* Creates a new object hello of class IStaticText                         *
* Sets the static text value and aligns it                                *
* Sets the static text as the client of the mainWindow                    *
* Sets the size of mainWindow                                             *
* Sets the window focus to mainWindow                                     *
* Displays the mainWindow                                                 *
* Starts the events processing for the application                        *
**************************************************************************/
int main()
{
  ICoordinateSystem::setApplicationOrientation(
          ICoordinateSystem::kOriginLowerLeft );
  IFrameWindow mainWindow (WND_MAIN);
  IStaticText hello(WND_HELLO, &mainWindow, &mainWindow);
  hello.setText(STR_HELLO);
  hello.setAlignment(IStaticText::centerCenter);
  mainWindow.setClient(&hello);

  mainWindow.sizeTo(ISize(400,300));
  mainWindow.setFocus();
  mainWindow.show();
  IApplication::current().run();
  return 0;
} /* end main() */

Using the Configuration File

In VisualAge C++ Version 4.0, the configuration file helps you manage your software development projects and the files associated with your project.

The following example shows the Hello World version 1 configuration file for the Windows operating system:


option
    link(debug, yes),
    link(subsystem,WINDOWS,4,0),
    link(linkwithmultithreadlib, yes),
    link(linkwithsharedlib, yes),
    link(extdictionary, no),
    link(map, no),
    gen(rtti, yes),
    link(padding, no)
{
    target "hello1.exe"
    {
        option
            lang(nokeyword, "bool"),
            lang(nokeyword, "true"),
            lang(nokeyword, "false"),
            lang(staticinlinelinkage, no),
            lang(staticconstlinkage, no),
            define("IC_LANG_BOOL", "0"),
            incl(searchpath,".")
        {
            option macros(global)
            {
                source type(hpp) "ahellow1.h"
            }
            source type(cpp) "ahellow1.cpp"
            option res_rc_options(" -d__WINDOWS__ -d_WIN32 -dIC_WIN")
            {
                source type(rc) "ahellow1.rc"
            }
        }
    }
}


Using a Makefile

In VisualAge C++ Version 3.5, the make utility helps you manage your software development projects and the files associated with your project. Make uses a makefile to convert your source code file into an object file. The makefile is a special file containing a list of tasks that you provide to convert your source file.

The following example shows the Hello World version 1 makefile for the OS/2 operating system:

# --- Flags --- GCPPFLAGS=/Gm+ /Gd+ /Gh+ /Ti+ /Fb+ /Q+ /Ft- /qrtti=all /DIC_LANG_BOOL=0 GCPPLFLAGS=/Tdp /B"/pmtype:pm /debug:none" GRCFLAGS=-D __OS2__ -D IC_PM # --- Steps --- all: hello1.exe ahellow1.obj: ahellow1.cpp icc.exe /c $(GCPPFLAGS) ahellow1.cpp ahellow1.res: ahellow1.rc ahellow1.h rc.exe -r $(GRCFLAGS) ahellow1.rc hello1.exe: ahellow1.obj ahellow1.res icc.exe $(GCPPFLAGS) $(GCPPLFLAGS) -Fehello1.exe ahellow1.obj rc.exe ahellow1.res hello1.exe

There are two ways to invoke make, depending on what you name your make file.

  1. If you have a file named makefile, type the following:
    nmake
    
  2. If you have a file by a different name, for example, makefile.win, type the following:
    nmake -f makefile.win
    


    Exploring Hello World Version 1

    The following sections describe each of the tasks performed by the Hello World version 1 application.

    Creating the Main Window

    The first statement creates the main window, an instance of the IFrameWindow class, for the application. To make this class available, the application must include the iframe.hpp library header file, as follows:

    #include <iframe.hpp>                   //IFrameWindow Class
    
    

    We need to define our application's orientation, as follows:

       ICoordinateSystem::setApplicationOrientation(
               ICoordinateSystem::kOriginLowerLeft );
    

    In this case, we are using the lower-left corner as the origin (similar tothe OS/2 operating system). Notice that we had to include the following header file, as follows:

    #include <icoordsy.hpp>
    
    

    Now that the IFrameWindow class is available, a variable, in this case mainWindow, is defined as a new object of this class. This object represents the main window of the application. For example:

      IFrameWindow mainWindow (WND_MAIN);                                         
    
    

    WND_MAIN is then defined in the ahellow1.rc file. The object represents the main window of the application.

    Creating a Static Text Control

    Next, create a static text control for the "Hello, World!!!" text string. Because this control is an object of the IStaticText class, it includes another library header file, istattxt.hpp, as follows:

     #include <istattxt.hpp>                 //IStaticText class                   
    
    

    Now, define another variable, hello, as a new object of the IStaticText class, which represents a static text control:

      IStaticText hello(WND_HELLO, &mainWindow, &mainWindow);
    
    

    WND_HELLO is the control ID.

    The argument that follows identifies the mainWindow as the parent of the static text control. This positions the static text control in relation to the main window and displays it on top of the main window.

    The last argument identifies the main window as the owner of the static text control. Controls notify their owner windows when events take place by using command, help, or control events. In this case, if an action is performed on the static text control, such as modifying its text string, that action is reported to the main window, which is specified as the owner. In version 1, no actions are performed on the static text control, but they are in versions 2 through 6.

    Setting a Text String for the Static Text Control

    After creating the static text control, give it a static text string. The IStaticText class is derived from the ITextControl class and, thus, inherits its member functions. One of those member functions, setText, defines the text string for the static text control. For example:

      hello.setText(STR_HELLO);                                         
    
    
    Then in the .rc file, you would define the following: 
    #include "ahellow1.h"
     
    STRINGTABLE
      BEGIN
        WND_MAIN,     "Hello World Sample - Version 1"
        STR_HELLO,    "Hello, World!!!"
      END
    

    Aligning the Static Text Control

    Next, the setAlignment member function of the IStaticText class aligns the text string in the static text control. In this sample, it is centered both horizontally and vertically.

    50   hello.setAlignment(IStaticText::centerCenter);                            
    
    

    If you do not align the text string, the default placement is in the upper-left corner of the static text area.

    Setting Static Text Control as the Client Window

    Next, designate the static text control as the frame's client window so that the "Hello, World!!!" text string displays in the main window's client area. Use the setClient member function of the IFrameWindow class, as follows:

      mainWindow.setClient(&hello);                                             
    
    

    The frame's client window is the window corresponding to the client area, which is the rectangular portion of the frame window not occupied by the other frame controls (for example, title bar, window border, and minimize and maximize buttons). Setting the static text control as the client window causes it to occupy the entire client area and to be aligned within the boundaries of that area. When the user resizes the main window, the client area (static text control in this sample) grows or shrinks.

    Setting the Size of the Main Window

    The following code shows you how to change the size of the main window:

      mainWindow.sizeTo(ISize(400,300));                                        
    
    

    This sets the size of the main window to 400 pixels wide by 300 pixels high.

    Setting the Focus and Showing the Main Window

    The two statements in the following code do the following:

    These statements use the IFrameWindow::setFocus and IWindow::show member functions:

       mainWindow.setFocus();                                                    
       mainWindow.show();                                                        
    
    

    Because IFrameWindow is derived from IWindow, the setFocus and IWindow::show member functions are inherited from the IWindow class. Classes inherit functions from the base classes from which they are derived. An application does not have to include those base classes. Therefore, the IWindow class does not need to be included in this application for its functions to be available.

    Starting Event Processing

    The last statement displays the main window and starts the user interface window event processing for the application. This is accomplished by using member functions belonging to the IApplication and ICurrentApplication classes. Therefore, include another library header file, iapp.hpp, as follows:

    26 #include <iapp.hpp>                     //IApplication class                  
    
    

    The IApplication::current member function of the IApplication class returns the current application as an instance of the ICurrentApplication class. Next, the ICurrentApplication::run member function displays the main window and starts event processing for this application, using the following code:

       IApplication::current().run();                                            
    
    



    Windows
    Events and Event Handlers
    Controls


    Creating a Frame Window
    Adding Events and Event Handlers
    Changing the Title Bar
    Creating an Information Area


    IApplication
    ICommandHandler
    IFrameExtensions
    IFrameWindow
    IInfoArea
    ITitle