home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / Chapter4 / 4-1 / FirstConsole.cpp next >
Encoding:
C/C++ Source or Header  |  1998-10-24  |  4.8 KB  |  192 lines

  1. // FirstConsole.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "resource.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8.  
  9. // Global Variables:
  10. HINSTANCE hInst;                                // current instance
  11. TCHAR szTitle[MAX_LOADSTRING];                                // The title bar text
  12. TCHAR szWindowClass[MAX_LOADSTRING];                                // The title bar text
  13.  
  14. // Foward declarations of functions included in this code module:
  15. ATOM                MyRegisterClass(HINSTANCE hInstance);
  16. BOOL                InitInstance(HINSTANCE, int);
  17. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  18. LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  19.  
  20. int APIENTRY WinMain(HINSTANCE hInstance,
  21.                      HINSTANCE hPrevInstance,
  22.                      LPSTR     lpCmdLine,
  23.                      int       nCmdShow)
  24. {
  25.      // TODO: Place code here.
  26.     MSG msg;
  27.     HACCEL hAccelTable;
  28.  
  29.     // Initialize global strings
  30.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  31.     LoadString(hInstance, IDC_FIRSTCONSOLE, szWindowClass, MAX_LOADSTRING);
  32.     MyRegisterClass(hInstance);
  33.  
  34.     // Perform application initialization:
  35.     if (!InitInstance (hInstance, nCmdShow)) 
  36.     {
  37.         return FALSE;
  38.     }
  39.  
  40.     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_FIRSTCONSOLE);
  41.  
  42.     // Main message loop:
  43.     while (GetMessage(&msg, NULL, 0, 0)) 
  44.     {
  45.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  46.         {
  47.             TranslateMessage(&msg);
  48.             DispatchMessage(&msg);
  49.         }
  50.     }
  51.  
  52.     return msg.wParam;
  53. }
  54.  
  55.  
  56.  
  57. //
  58. //  FUNCTION: MyRegisterClass()
  59. //
  60. //  PURPOSE: Registers the window class.
  61. //
  62. //  COMMENTS:
  63. //
  64. //    This function and its usage is only necessary if you want this code
  65. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  66. //    function that was added to Windows 95. It is important to call this function
  67. //    so that the application will get 'well formed' small icons associated
  68. //    with it.
  69. //
  70. ATOM MyRegisterClass(HINSTANCE hInstance)
  71. {
  72.     WNDCLASSEX wcex;
  73.  
  74.     wcex.cbSize = sizeof(WNDCLASSEX); 
  75.  
  76.     wcex.style            = CS_HREDRAW | CS_VREDRAW;
  77.     wcex.lpfnWndProc    = (WNDPROC)WndProc;
  78.     wcex.cbClsExtra        = 0;
  79.     wcex.cbWndExtra        = 0;
  80.     wcex.hInstance        = hInstance;
  81.     wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_FIRSTCONSOLE);
  82.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  83.     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
  84.     wcex.lpszMenuName    = (LPCSTR)IDC_FIRSTCONSOLE;
  85.     wcex.lpszClassName    = szWindowClass;
  86.     wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  87.  
  88.     return RegisterClassEx(&wcex);
  89. }
  90.  
  91. //
  92. //   FUNCTION: InitInstance(HANDLE, int)
  93. //
  94. //   PURPOSE: Saves instance handle and creates main window
  95. //
  96. //   COMMENTS:
  97. //
  98. //        In this function, we save the instance handle in a global variable and
  99. //        create and display the main program window.
  100. //
  101. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  102. {
  103.    HWND hWnd;
  104.  
  105.    hInst = hInstance; // Store instance handle in our global variable
  106.  
  107.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  108.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  109.  
  110.    if (!hWnd)
  111.    {
  112.       return FALSE;
  113.    }
  114.  
  115.    ShowWindow(hWnd, nCmdShow);
  116.    UpdateWindow(hWnd);
  117.  
  118.    return TRUE;
  119. }
  120.  
  121. //
  122. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  123. //
  124. //  PURPOSE:  Processes messages for the main window.
  125. //
  126. //  WM_COMMAND    - process the application menu
  127. //  WM_PAINT    - Paint the main window
  128. //  WM_DESTROY    - post a quit message and return
  129. //
  130. //
  131. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  132. {
  133.     int wmId, wmEvent;
  134.     PAINTSTRUCT ps;
  135.     HDC hdc;
  136.     TCHAR szHello[MAX_LOADSTRING];
  137.     LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
  138.  
  139.     switch (message) 
  140.     {
  141.         case WM_COMMAND:
  142.             wmId    = LOWORD(wParam); 
  143.             wmEvent = HIWORD(wParam); 
  144.             // Parse the menu selections:
  145.             switch (wmId)
  146.             {
  147.                 case IDM_ABOUT:
  148.                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  149.                    break;
  150.                 case IDM_EXIT:
  151.                    DestroyWindow(hWnd);
  152.                    break;
  153.                 default:
  154.                    return DefWindowProc(hWnd, message, wParam, lParam);
  155.             }
  156.             break;
  157.         case WM_PAINT:
  158.             hdc = BeginPaint(hWnd, &ps);
  159.             // TODO: Add any drawing code here...
  160.             RECT rt;
  161.             GetClientRect(hWnd, &rt);
  162.             DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
  163.             EndPaint(hWnd, &ps);
  164.             break;
  165.         case WM_DESTROY:
  166.             PostQuitMessage(0);
  167.             break;
  168.         default:
  169.             return DefWindowProc(hWnd, message, wParam, lParam);
  170.    }
  171.    return 0;
  172. }
  173.  
  174. // Mesage handler for about box.
  175. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  176. {
  177.     switch (message)
  178.     {
  179.         case WM_INITDIALOG:
  180.                 return TRUE;
  181.  
  182.         case WM_COMMAND:
  183.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  184.             {
  185.                 EndDialog(hDlg, LOWORD(wParam));
  186.                 return TRUE;
  187.             }
  188.             break;
  189.     }
  190.     return FALSE;
  191. }
  192.