home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c500 / 4.ddi / GENERIC.WEX / GENERIC.C next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  5.3 KB  |  202 lines

  1. /*
  2.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3.  *%                                       %
  4.  *%    Copyright (C) 1991, by WATCOM Systems Inc. All rights reserved.    %
  5.  *%                                       %
  6.  *%     Permission is granted to anyone to use this example program for       %
  7.  *%     any purpose on any computer system, subject to the following       %
  8.  *%    restrictions:                               %
  9.  *%                                       %
  10.  *%     1. This example is provided on an "as is" basis, without warranty. %
  11.  *%       You indemnify, hold harmless and defend WATCOM from and against %
  12.  *%       any claims or lawsuits, including attorney's, that arise or       %
  13.  *%       result from the use or distribution of this example, or any     %
  14.  *%       modification thereof.                       %
  15.  *%                                       %
  16.  *%     2. You may not remove, alter or suppress this notice from this       %
  17.  *%        example program or any modification thereof.               %
  18.  *%                                       %
  19.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20.  *
  21.  * GENERIC.C
  22.  *
  23.  * Generic windows program, demonstrates basic message handling
  24.  *
  25.  */
  26. #include <windows.h>
  27. #include <stdio.h>
  28. #include <malloc.h>
  29. #include "generic.h"
  30.  
  31. static char GenericClass[32]="GenericClass";
  32.  
  33. static BOOL FirstInstance( HANDLE );
  34. static BOOL AnyInstance( HANDLE, int, LPSTR );
  35. long _EXPORT FAR PASCAL WindowProc( HWND, unsigned, WORD, LONG );
  36.  
  37. /*
  38.  * WinMain - initialization, message loop
  39.  */
  40. int PASCAL WinMain( HANDLE this_inst, HANDLE prev_inst, LPSTR cmdline,
  41.             int cmdshow )
  42. {
  43.     MSG        msg;
  44.  
  45.     prev_inst = prev_inst;
  46. #ifdef __WINDOWS_386__
  47.     sprintf( GenericClass,"GenericClass%d", this_inst );
  48. #else
  49.     if( !prev_inst )
  50. #endif
  51.     if( !FirstInstance( this_inst ) ) return( FALSE );
  52.  
  53.     if( !AnyInstance( this_inst, cmdshow, cmdline ) ) return( FALSE );
  54.  
  55.     while( GetMessage( &msg, NULL, NULL, NULL ) ) {
  56.  
  57.     TranslateMessage( &msg );
  58.     DispatchMessage( &msg );
  59.  
  60.     }
  61.  
  62.     return( msg.wParam );
  63.  
  64. } /* WinMain */
  65.  
  66. /*
  67.  * FirstInstance - register window class for the application,
  68.  *           and do any other application initialization
  69.  */
  70. static BOOL FirstInstance( HANDLE this_inst )
  71. {
  72.     WNDCLASS    wc;
  73.     BOOL    rc;
  74.  
  75.     /*
  76.      * set up and register window class
  77.      */
  78.     wc.style = CS_HREDRAW | CS_VREDRAW;
  79.     wc.lpfnWndProc = (LPVOID) WindowProc;
  80.     wc.cbClsExtra = 0;
  81.     wc.cbWndExtra = sizeof( DWORD );
  82.     wc.hInstance = this_inst;
  83.     wc.hIcon = LoadIcon( this_inst, "GenericIcon" );
  84.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  85.     wc.hbrBackground = GetStockObject( WHITE_BRUSH );
  86.     wc.lpszMenuName = "GenericMenu";
  87.     wc.lpszClassName = GenericClass;
  88.     rc = RegisterClass( &wc );
  89.     return( rc );
  90.  
  91. } /* FirstInstance */
  92.  
  93. /*
  94.  * AnyInstance - do work required for every instance of the application:
  95.  *          create the window, initialize data
  96.  */
  97. static BOOL AnyInstance( HANDLE this_inst, int cmdshow, LPSTR cmdline )
  98. {
  99.     HWND     hwnd;
  100.     extra_data    *edata_ptr;
  101.     
  102.     /*
  103.      * create main window
  104.      */
  105.     hwnd = CreateWindow(
  106.     GenericClass,        /* class */
  107.     "WATCOM Generic Kind Of Application",    /* caption */
  108.     WS_OVERLAPPEDWINDOW,    /* style */
  109.     CW_USEDEFAULT,        /* init. x pos */
  110.     CW_USEDEFAULT,        /* init. y pos */
  111.     CW_USEDEFAULT,        /* init. x size */
  112.     CW_USEDEFAULT,        /* init. y size */
  113.     NULL,            /* parent window */
  114.     NULL,            /* menu handle */
  115.     this_inst,        /* program handle */
  116.     NULL            /* create parms */
  117.     );
  118.             
  119.     if( !hwnd ) return( FALSE );
  120.  
  121.     /*
  122.      * set up data associated with this window
  123.      */
  124.     edata_ptr = malloc( sizeof( extra_data ) );
  125.     if( edata_ptr == NULL ) return( FALSE );
  126.     edata_ptr->cmdline = cmdline;
  127.     SetWindowLong( hwnd, EXTRA_DATA_OFFSET, (DWORD) edata_ptr );
  128.  
  129.     /*
  130.      * display window
  131.      */
  132.     ShowWindow( hwnd, cmdshow );
  133.     UpdateWindow( hwnd );
  134.     
  135.     return( TRUE );
  136.                         
  137. } /* AnyInstance */
  138.  
  139. /*
  140.  * AboutDlgProc - processes messages for the about dialog.
  141.  */
  142. BOOL _EXPORT FAR PASCAL AboutDlgProc( HWND hwnd, unsigned msg,
  143.                 WORD wparam, LONG lparam )
  144. {
  145.     lparam = lparam;            /* turn off warning */
  146.  
  147.     switch( msg ) {
  148.     case WM_INITDIALOG:
  149.     return( TRUE );
  150.  
  151.     case WM_COMMAND:
  152.         if( wparam == IDOK ) {
  153.         EndDialog( hwnd, TRUE );
  154.         return( TRUE );
  155.     }
  156.     break;
  157.     }
  158.     return( FALSE );
  159.  
  160. } /* AboutDlgProc */
  161.  
  162. /*
  163.  * WindowProc - handle messages for the main application window
  164.  */
  165. LONG _EXPORT FAR PASCAL WindowProc( HWND hwnd, unsigned msg,
  166.                      WORD wparam, LONG lparam )
  167. {
  168.     FARPROC     proc;
  169.     HANDLE    inst_handle;
  170.     extra_data    *edata_ptr;
  171.     char    buff[128];
  172.  
  173.     switch( msg ) {
  174.     case WM_COMMAND:
  175.         switch( wparam ) {
  176.     case MENU_ABOUT:
  177.         inst_handle = GetWindowWord( hwnd, GWW_HINSTANCE );
  178.         proc = MakeProcInstance( AboutDlgProc, inst_handle );
  179.         DialogBox( inst_handle,"AboutBox", hwnd, proc );
  180.         FreeProcInstance( proc );
  181.         break;
  182.  
  183.     case MENU_CMDSTR:
  184.         edata_ptr = (extra_data *) GetWindowLong( hwnd,
  185.                             EXTRA_DATA_OFFSET );
  186.         sprintf( buff, "Command string was %Fs", edata_ptr->cmdline );
  187.         MessageBox( NULL, buff, "Program Information", MB_OK );
  188.         break;
  189.     }
  190.     break;
  191.  
  192.     case WM_DESTROY:
  193.     PostQuitMessage( 0 );
  194.     break;
  195.  
  196.     default:
  197.     return( DefWindowProc( hwnd, msg, wparam, lparam ) );
  198.     }
  199.     return( 0L );
  200.  
  201. } /* WindowProc */
  202.