home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / SHOWGDI / MAIN.C_ / MAIN.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  5.1 KB  |  184 lines

  1. /****************************************************************************
  2.  Main.c
  3.  
  4.  The Main module handles the ShowGDI main program instance and window.
  5.  
  6.  The ShowGDI program allows interactive rendering of GDI graphics with 
  7.  an adjustable enlarged view of the results.
  8.  
  9.  Written by Dave Parker
  10.  (c)Copyright 1991 Microsoft Corporation
  11.  
  12. ****************************************************************************/
  13.  
  14. #include "windows.h"
  15.  
  16. #include "util.h"
  17. #include "dialogs.h"
  18. #include "view.h"
  19. #include "dc.h"
  20. #include "draw.h"
  21. #include "dib.h"
  22.  
  23. #include "main.h"
  24.  
  25.  
  26. /****************************************************************************
  27.    Globals
  28. ****************************************************************************/
  29.  
  30. HANDLE   hInst;             /* current instance */
  31. HWND     hwndMain;          /* main program window */
  32. HANDLE   hAccTable;         /* handle to accelerator table */
  33.  
  34.  
  35. /****************************************************************************
  36.    Functions
  37. ****************************************************************************/
  38.  
  39. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, 
  40.                     int nCmdShow )
  41. /* Main program entry point. */
  42. {
  43.    MSG msg;
  44.  
  45.    /* Initialize */
  46.    if (!hPrevInstance) {
  47.       if (!InitApplication( hInstance ))
  48.          return FALSE;
  49.    }
  50.    if (!InitInstance( hInstance, nCmdShow ))
  51.       return (FALSE);
  52.  
  53.    /* Event loop */
  54.    while (GetMessage( &msg, NULL, NULL, NULL )) {
  55.       if (!TranslateAccelerator( hwndMain, hAccTable, &msg )) {
  56.          TranslateMessage( &msg );
  57.          DispatchMessage( &msg );
  58.       }
  59.    }
  60.  
  61.    /* Cleanup and exit */
  62.    Fini();
  63.    return msg.wParam;
  64. }
  65.  
  66.  
  67. BOOL InitApplication( HANDLE hInstance )
  68. /* Initialize the application. */
  69. {
  70.    WNDCLASS  wc;
  71.  
  72.    /* Register the main window class */
  73.    wc.style = CS_HREDRAW | CS_VREDRAW;
  74.    wc.lpfnWndProc = MainWndProc;
  75.    wc.cbClsExtra = 0;
  76.    wc.cbWndExtra = 0;
  77.    wc.hInstance = hInstance;
  78.    wc.hIcon = LoadIcon(hInstance, "ShowGDIIcon");
  79.    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  80.    wc.hbrBackground = COLOR_WINDOW+1;
  81.    wc.lpszMenuName =  "ShowGDIMenu";
  82.    wc.lpszClassName = "ShowGDIClass";
  83.    if (!RegisterClass(&wc))
  84.        return FALSE;
  85.  
  86.    return TRUE;
  87. }
  88.  
  89.  
  90. BOOL InitInstance( HANDLE hInstance, int nCmdShow )
  91. /* Initialize this instance of the application. */
  92. {
  93.    /* Record instance and accelerator table in globals */
  94.    hInst = hInstance;
  95.    hAccTable = LoadAccelerators(hInst, "ShowGDIAcc");
  96.  
  97.    /* Create the main window and record in global */
  98.    hwndMain = CreateWindow(
  99.        "ShowGDIClass",        /* See RegisterClass() call.          */
  100.        "ShowGDI",             /* Text for window title bar.         */
  101.        WS_OVERLAPPEDWINDOW,   /* Window style.                      */
  102.        CW_USEDEFAULT,         /* Default horizontal position.       */
  103.        CW_USEDEFAULT,         /* Default vertical position.         */
  104.        CW_USEDEFAULT,         /* Default width.                     */
  105.        CW_USEDEFAULT,         /* Default height.                    */
  106.        NULL,                  /* Overlapped windows have no parent. */
  107.        NULL,                  /* Use the window class menu.         */
  108.        hInstance,             /* This instance owns this window.    */
  109.        NULL                   /* Pointer not needed.                */
  110.    );
  111.    if (!hwndMain)
  112.       return (FALSE);
  113.  
  114.    /* Initialize the view and the DC settings */
  115.    if (!NewView())
  116.       return FALSE;
  117.    ReadDC( drawDC );
  118.  
  119.    /* Show the window and update it */
  120.    ShowWindow( hwndMain, nCmdShow );
  121.    UpdateWindow( hwndMain );
  122.  
  123.    return (TRUE);             
  124. }
  125.  
  126.  
  127. void Fini( void )
  128. /* Cleanup and prepare to quit */
  129. {
  130.    if (curDIB != NULL)
  131.       GlobalFree( curDIB );
  132. }
  133.  
  134.  
  135. long FAR PASCAL MainWndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  136. /* Window handler for main application window */
  137. {
  138.    HDC         hdc;
  139.    PAINTSTRUCT ps;
  140.  
  141.    switch (message) {
  142.       case WM_COMMAND:
  143.          switch( MenuGroupFromID( wParam ) ) {
  144.             case VIEW_MENU_GROUP:  ViewCmd( wParam );  return 0L;
  145.             case DC_MENU_GROUP:    DCCmd( wParam );    return 0L;
  146.             case DRAW_MENU_GROUP:  DrawCmd( wParam );  return 0L;
  147.             case HELP_MENU_GROUP:  HelpCmd( wParam );  return 0L;
  148.             default:
  149.                return (DefWindowProc( hWnd, message, wParam, lParam ));
  150.          }
  151.          break;
  152.  
  153.       case WM_INITMENU:
  154.          CheckViewMenuItems( (HMENU) wParam );
  155.          CheckDCMenuItems( (HMENU) wParam );
  156.          break;
  157.  
  158.       case WM_PAINT:
  159.          hdc = BeginPaint( hWnd, &ps );
  160.          PaintView( hdc, ps.rcPaint );
  161.          EndPaint( hWnd, &ps );
  162.          break;
  163.  
  164.       case WM_DESTROY:
  165.          PostQuitMessage( 0 );
  166.          break;
  167.  
  168.       default:
  169.          return (DefWindowProc( hWnd, message, wParam, lParam ));
  170.    }
  171.    return 0L;
  172. }
  173.  
  174.  
  175. void HelpCmd( int item )
  176. /* Process the command 'item' from the Help menu. */
  177. {
  178.    switch (item) {
  179.       case IDM_ABOUT:
  180.          Dlg( AboutDlg );
  181.          break;
  182.    }
  183. }
  184.