2.2.10 Initialization Functions

Most applications use two locally defined initialization functions:

The main initialization function carries out work that must be done only once for all instances of the application (for example, registering window classes).

The instance initialization function performs tasks that must be done for every instance of the application.

Using initialization functions keeps the WinMain function simple and readable; it also organizes initialization tasks so that they can be placed in a separate code segment and discarded after use. The Generic application does not discard its initialization functions.

2.2.10.1 Main Initialization Function

The Generic application's main initialization function looks like the following:

BOOL InitApplication(hinstCurrent)
HINSTANCE hinstCurrent;  /* current instance                       */
{
    WNDCLASS  wc;

    /*
     *  Fill in window-class structure with parameters that
     *  describe the main window.
     */

    wc.style = NULL;                /* class style(s)              */
    wc.lpfnWndProc = MainWndProc;   /* window procedure            */
                                    /* for windows of this class   */

    wc.cbClsExtra = 0;              /* no per-class extra data     */
    wc.cbWndExtra = 0;              /* no per-window extra data    */

    wc.hInstance = hinstCurrent;    /* application that owns class */
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName =  "GenericMenu";   /* menu name in .RC file   */
    wc.lpszClassName = "GenericWClass"; /* name in CreateWindow    */

    /* Register the window class and return success/failure code.  */

    return (RegisterClass(&wc));
}

2.2.10.2 Instance Initialization Function

Generic's instance initialization function looks like the following:

BOOL InitInstance(hinstCurrent, nCmdShow)
HINSTANCE hinstCurrent; /* handle of current instance              */
int nCmdShow;           /* param for first ShowWindow call         */
{
    HWND hWnd;          /* handle of main window                   */

    /*
     * Save the instance handle in a static variable, which will
     * be used in subsequent calls from this application to
     * Windows.
     */


    hInst = hinstCurrent;

    /* Create a main window for this application instance.        */

    hWnd = CreateWindow(
        "GenericWClass",                /* see RegisterClass call */
        "Generic Sample Application",   /* text for title bar     */
        WS_OVERLAPPEDWINDOW,            /* window style           */
        CW_USEDEFAULT,                  /* default horz position  */
        CW_USEDEFAULT,                  /* default vert position  */
        CW_USEDEFAULT,                  /* default width          */
        CW_USEDEFAULT,                  /* default height         */
        NULL,                /* overlapped windows have no parent */
        NULL,                /* use window class menu             */
        hinstCurrent,        /* this instance owns this window    */
        NULL                 /* pointer not needed                */
    );

    /* If the window could not be created, return "failure."      */

    if (hWnd == NULL)
        return FALSE;

    /*
     * Make the window visible, update its client area, and
     * return "success."
     */

    ShowWindow(hWnd, nCmdShow);  /* shows window                  */
    UpdateWindow(hWnd);          /* sends WM_PAINT message        */
    return TRUE;
}