Amazon Honor System Click Here to Pay Learn More

by Forest J. Handford

This chapter we will cover our first windows program.  Windows programs are very intimidating at first glance but the more you look at them the easier they are.

In C++ there is a switch statement.  The switch statement is used by many other programmers.  I personally don't like it because it takes more typing than else if statements.  It also tends to be slower.  Here is an example of the switch statement:
switch ( Number )
{
   case > 45:
      cout<<"The number is bigger than fourty-five."<<endl;
      break;
   case < 2:
      cout<<"The number is less than 2."<<endl;
      break;
   case 4:
      cout<<"The number is equal to 4."<<endl;
      break;
   default:
      cout<<"The number is not a number I look for."<<endl;
      break;
}

The above is equivalent to:
if ( Number > 45) cout<<"The number is bigger than fourty-five."<<endl;
else if ( Number < 2 ) cout<<"The number is less than 2."<<endl;
else if ( Number == 4 ) cout<<"The number is equal to 4."<<endl;
else cout<<"The number is not a number I look for."<<endl;

In our first windows program I will use the switch statement because the compiler places it there and I'm too lazy to re-write it.  If you don't use Microsoft Visual C++ 6.0 compiler, the program for this chapter may not compile correctly.
 

C++ programs can be compiler specific.  This is a large drawback with C++.  Many C++ programmers own multiple compilers.  Java was created as a language that not only wasn't compiler specific but it wasn't operating system specific.  The same java compiled code will run on UNIX, Windows 3.11, Windows 95, Windows NT, Redhat Linux, and Macintosh.  Currently Microsoft is selling a version of Java that is for Windows 95 only!  Please don't support that product or you will be supporting the end of a great language. 


If you have the Microsoft Visual C++ 6.0 compiler you can create a very similar Win32 application by going to File -> New.  Then click the projects tab, select Win32 application and then title the project:


Next hit the OK button and then choose "a typical HELLO WORLD! application."  As is, this will create a compileable program that will say "HELLO WORLD!"

Now you can go to your resources tab and edit all of the resources that will be used in the program.  The resource editor is very simple to use so I won't go into too much detail with it.

Here is how to edit your dialog boxes:



How to edit your strings:


Instead of a main function this type of windows program has a winmain function.  I wrote detailed comments for you to review in the program so I won't discuss them here and avoid redundancy:

< Program and Code >

Copyright (c) 1998, Forest J. Handford

resource.h
    This is the standard header file, which defines new resource IDs.
    Microsoft Visual C++ reads and updates this file.

/////////////////////////////////////////////////////////////////////////////
Other notes:

AppWizard uses "TODO:" to indicate parts of the source code you
should add to or customize.

/////////////////////////////////////////////////////////////////////////////


// stdafx.cpp : source file that includes just the standard includes
// Test.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
// Copyright (c) 1998, Forest J. Handford

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file


// Test.cpp : Defines the entry point for the application.
// Copyright (c) 1998, Forest J. Handford

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;      // current instance
TCHAR szTitle[MAX_LOADSTRING];   // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM    MyRegisterClass(HINSTANCE hInstance);
BOOL    InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  // TODO: Place code here.
 MSG msg;
 HACCEL hAccelTable;

 // Initialize global strings
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING);
 MyRegisterClass(hInstance);

 // Perform application initialization:
 if (!InitInstance (hInstance, nCmdShow))
 {
  return FALSE;
 }

 //This next function loads the keyboard accelerators.
 //Keyboard accelerators commands like Ctrl-C or Alt-V
 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TEST);

 // Main message loop:
 while (GetMessage(&msg, NULL, 0, 0))
 {
  //This takes any input and then interprets it.
  //All input is stored in messages.
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
   //If the message queue has at least one message translate it
   //This changes virtual key messages to charachters
   TranslateMessage(&msg);
   //This dispatches messages from getmessages to windows
   DispatchMessage(&msg);
  }
 }

 return msg.wParam;
}
 
 

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX);

 wcex.style   = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc = (WNDPROC)WndProc;
 wcex.cbClsExtra  = 0;
 wcex.cbWndExtra  = 0;
 wcex.hInstance  = hInstance;
 wcex.hIcon   = LoadIcon(hInstance, (LPCTSTR)IDI_TEST);
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = (LPCSTR)IDC_TEST;
 wcex.lpszClassName = szWindowClass;
 wcex.hIconSm  = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

 return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   // This will create a window 800 x 600 at starting at the top left.
   hWnd = CreateWindow(szWindowClass, szTitle, WS_SYSMENU | WS_CAPTION,
      0, 0, 800, 600, NULL, NULL, hInstance, NULL);

   // If the last call failed, IE no memory, exit.
   if (!hWnd)
   {
      return FALSE;
   }

   //This will display the created window and maximize it.
   ShowWindow(hWnd, SW_MAXIMIZE);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 HDC hdc;
 TCHAR szHuggable[MAX_LOADSTRING]; //This will hold our message
 //Load our message
 LoadString(hInst, IDS_HUGGABLE, szHuggable, MAX_LOADSTRING);

 switch (message)
 {
  case WM_COMMAND:
   //This translates the wparam to a low order word
   wmId    = LOWORD(wParam);
   //This translates the wparam to a high order word
   wmEvent = HIWORD(wParam);
   // Parse the menu selections:
   switch (wmId)
   {
    //If the user choose about display the about dialog box
    case IDM_ABOUT:
     DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
     break;
    //If the user chooses to exit end the program
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    //if anything else make sure the messages are read
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
   }
   break;
  //If the user wants the window created draw
  case WM_PAINT:
   //Create a device context through MFC.
   hdc = BeginPaint(hWnd, &ps);
   // TODO: Add any drawing code here...
   RECT rt; //Create a rectangle
   //This makes the rectangle the same size as the client's window,
   GetClientRect(hWnd, &rt);
   // Draw our text
   DrawText(hdc, szHuggable, strlen(szHuggable), &rt, DT_CENTER);
   //This must be called to allow for the next BeginPaint()
   EndPaint(hWnd, &ps);
   break;
  //If the user wants to quit
  case WM_DESTROY:
   //This tells the system our program will soon close
   PostQuitMessage(0);
   break;
  //if anything else make sure the messages are delivered
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch (message)
 {
  case WM_INITDIALOG:
    return TRUE;

  case WM_COMMAND:
   if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
   {
    //Close the dialog box
    EndDialog(hDlg, LOWORD(wParam));
    return TRUE;
   }
   break;
 }
    return FALSE;
}


//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by Test.rc
// Copyright (c) 1998, Forest J. Handford
//
#define IDC_MYICON                      2
#define IDD_TEST_DIALOG                 102
#define IDD_ABOUTBOX                    103
#define IDS_APP_TITLE                   103
#define IDM_ABOUT                       104
#define IDM_EXIT                        105
#define IDS_HUGGABLE                    106
#define IDI_TEST                        107
#define IDI_SMALL                       108
#define IDC_TEST                        109
#define IDR_MAINFRAME                   128
#define IDC_STATIC                      -1

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        129
#define _APS_NEXT_COMMAND_VALUE         32771
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           110
#endif
#endif


// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
// Copyright (c) 1998, Forest J. Handford
//

#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_

#if _MSC_VER > 1000
//This command tells the compiler to only compile the code once
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers

// Windows Header Files:
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

// Local Header Files

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)


// Copyright (c) 1998, Forest J. Handford

#if !defined(AFX_TEST_H__BC415408_78D9_11D2_96EE_444553540000__INCLUDED_)
#define AFX_TEST_H__BC415408_78D9_11D2_96EE_444553540000__INCLUDED_

#if _MSC_VER > 1000
//The following means only compile once
#pragma once
#endif // _MSC_VER > 1000

#include "resource.h"

#endif // !defined(AFX_TEST_H__BC415408_78D9_11D2_96EE_444553540000__INCLUDED_)

Windows Intro - I think that we have done enough for this chapter.  In the next chapter we'll create a slightly more complex Windows Program!

HOME

Amazon Honor System Click Here to Pay Learn More