//***********************************************************************/

// Introduction to Windows Programming

// Example1.c

// created:   01/12/95   Rainer Döbele

// modified:

//***********************************************************************/

#include <windows.h>     // we always need to include the windows header file

#include "example1.h"    // this is our own header file used e.g. to define ressource Ids

// define the name of our Window Class

static const char szWndClassName[]={ "MYWNDCLASS" };

// Global variables

// Note:  use globals only for constants which are valid thoughout

//        the whole time of program execution

HINSTANCE hInstance;     // Global Handle to Program Instance

HWND      hAppWnd;       // Global Handle to Application Window

// Prototypes of exported functions called by Windows

LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);

// Internal Function Prototoyes

static BOOL RegisterWindowClasses(HINSTANCE hFirstInstance);

static void PaintAppWindow(HWND hWnd,HDC hdc,int iPaintCount);

// Local varables

// e.g. static char szFileName[120];

//***********************************************************************/

int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)

{ MSG  msg;

  // Store Instance Handle in the global variable hInstance

  hInstance=hInst;

  // If there is no Instance of our program in memory then register

  // the window class

  if (hPrevInst == NULL)

      if (!RegisterWindowClasses(hInstance))

           return FALSE;  // registration failed!

  // Create our Main Application Window

  hAppWnd=CreateWindow(szWndClassName,"My First Windows Program",  // Class Name and Window Title

                       WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,  // Style of Window

                       CW_USEDEFAULT,CW_USEDEFAULT,400,300,        // Position and Size of Window

                       NULL,NULL,hInstance,                        // Handle of Parent Window, Menu and Instance

                       NULL                                        // User data passed to WM_CRATE

                      );

  if (!hAppWnd) return FALSE; // Create Window failed

  // Show the Window as specified in the nCmdShow parameter

  ShowWindow(hAppWnd,nCmdShow);

  UpdateWindow(hAppWnd);

  // Enter the Message Loop

  while (GetMessage(&msg, NULL, 0, 0))

    {

        TranslateMessage(&msg); /* translates virtual key codes    */

        DispatchMessage(&msg);  /* dispatches message to window    */

    }

  // Program has been terminated

  return (int) msg.wParam;    /* return value of PostQuitMessage */

}

//***********************************************************************/

static BOOL RegisterWindowClasses(HINSTANCE hFirstInstance)

{ WNDCLASS wc;

  // Register the window class.

  wc.lpszClassName = szWndClassName;  // Name of the Window Class

  wc.hInstance     = hFirstInstance;  // Handle of program instance

  wc.style         = CS_HREDRAW|CS_VREDRAW;  // Combination of Class Styles

  wc.lpfnWndProc   = AppWndProc; // Adress of Window Procedure

  wc.cbClsExtra    = 0;   // Extra Bytes allocated for this Class

  wc.cbWndExtra    = 0;   // Extra Bytes allocated for each Window

  wc.hIcon         = LoadIcon(hFirstInstance, MAKEINTRESOURCE(ICON_APPWND));

  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

  wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);

  wc.lpszMenuName  = NULL;

  if (!RegisterClass(&wc)) return FALSE;  // Register Class failed

  // Register other Window Classes if necessary

  // ....

  // All Window Classes Registered successfully

  return TRUE;

}

//***********************************************************************/

LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)

{ // this is where we receive all message concerning this window

  // we can either process a message or pass it on to the default

  // message handler of windows

  static int iPaintCount;  // Count the number of Paint Messages received

  switch(msg)

   {

     case WM_CREATE:

          // Initialize Window

          // this message is received before the window is displayed

          iPaintCount=0;

          break;

     case WM_PAINT:

          { PAINTSTRUCT ps;

            HDC hdc;

            // Increas Paint Message Counter

            iPaintCount++;

            // Get the handle to the Windows's Display Context

            hdc=BeginPaint(hWnd,&ps);

             // Now Paint the Windows's contents

             PaintAppWindow(hWnd,hdc,iPaintCount);

            // Call EndPaint to release the DC and validate the client area

            EndPaint(hWnd,&ps);

          }

          break;

     case WM_DESTROY:

          PostQuitMessage(0);  // Terminate Application

          break;

     default:

          // We didn't process the message so let Windows do it

          return DefWindowProc(hWnd,msg,wParam,lParam);

   }

  // We processed the message and there

  // is no processing by Windows necessary

  return 0L;

}

//***********************************************************************/

static void PaintAppWindow(HWND hWnd,HDC hdc,int iPaintCount)

{ char szText[50]; // Array holding the string displayed

  RECT rcWnd;      // Dimensions of the Windows's client area

  // Get the size of the Client Area

  GetClientRect(hWnd,&rcWnd);

  // put the string together

  wsprintf(szText,"Hello Windows World - Paint Count: %d",iPaintCount);

  // Now draw the Text in the window centre

  DrawText(hdc,szText,lstrlen(szText),&rcWnd,DT_SINGLELINE|DT_CENTER|DT_VCENTER);

}

//***********************************************************************/

Back to tutorial