home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c045 / 5.ddi / GENERIC / GENERIC.C$ / GENERIC.bin
Encoding:
Text File  |  1992-01-01  |  8.7 KB  |  264 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Generic.c
  4.  
  5.     PURPOSE: Generic template for Windows applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.         WinMain() - calls initialization function, processes message loop
  10.         InitApplication() - initializes window data and registers window
  11.         InitInstance() - saves instance handle and creates main window
  12.         MainWndProc() - processes messages
  13.         About() - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17.         Windows can have several copies of your application running at the
  18.         same time. The variable hInst keeps track of which instance this
  19.         application is so that processing will be to the correct window.
  20.  
  21. ****************************************************************************/
  22.  
  23. #include <windows.h>        /* required for all Windows applications */
  24. #include "generic.h"        /* specific to this program */
  25.  
  26. HANDLE hInst;    /* current instance */
  27.  
  28. /****************************************************************************
  29.  
  30.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  31.  
  32.     PURPOSE: calls initialization function, processes message loop
  33.  
  34.     COMMENTS:
  35.  
  36.         Windows recognizes this function by name as the initial entry point
  37.         for the program.  This function calls the application initialization
  38.         routine, if no other instance of the program is running, and always
  39.         calls the instance initialization routine.    It then executes a message
  40.         retrieval and dispatch loop that is the top-level control structure
  41.         for the remainder of execution.  The loop is terminated when a WM_QUIT
  42.         message is received, at which time this function exits the application
  43.         instance by returning the value passed by PostQuitMessage().
  44.  
  45.         If this function must abort before entering the message loop, it
  46.         returns the conventional value NULL.
  47.  
  48. ****************************************************************************/
  49.  
  50. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  51. {
  52.     MSG msg;    /* message */
  53.  
  54.     if (!hPrevInstance)        /* Other instances of app running? */
  55.         if (!InitApplication(hInstance))    /* Initialize shared things */
  56.             return (FALSE);        /* Exits if unable to initialize */
  57.  
  58.     /* Perform initializations that apply to a specific instance */
  59.     if (!InitInstance(hInstance, nCmdShow))
  60.         return (FALSE);
  61.  
  62.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  63.     while (GetMessage(&msg,        /* message structure */
  64.         NULL,    /* handle of window receiving the message */
  65.         NULL,    /* lowest message to examine    */
  66.         NULL))    /* highest message to examine    */
  67.     {
  68.         TranslateMessage(&msg);        /* Translates virtual key codes          */
  69.         DispatchMessage(&msg);        /* Dispatches message to window          */
  70.     }
  71.     return (msg.wParam);        /* Returns the value from PostQuitMessage */
  72. }
  73.  
  74.  
  75. /****************************************************************************
  76.  
  77.     FUNCTION: InitApplication(HANDLE)
  78.  
  79.     PURPOSE: Initializes window data and registers window class
  80.  
  81.     COMMENTS:
  82.  
  83.         This function is called at initialization time only if no other
  84.         instances of the application are running.  This function performs
  85.         initialization tasks that can be done once for any number of running
  86.         instances.
  87.  
  88.         In this case, we initialize a window class by filling out a data
  89.         structure of type WNDCLASS and calling the Windows RegisterClass()
  90.         function. Since all instances of this application use the same window
  91.         class, we only need to do this when the first instance is initialized.
  92.  
  93.  
  94. ****************************************************************************/
  95.  
  96. BOOL InitApplication(HANDLE hInstance)
  97. {
  98.     WNDCLASS  wc;
  99.  
  100.     /* Fill in window class structure with parameters that describe the
  101.         main window. */
  102.  
  103.     wc.style = NULL;                /* Class style(s) */
  104.     wc.lpfnWndProc = MainWndProc;    /* Function to retrieve messages for  */
  105.                                     /* windows of this class */
  106.     wc.cbClsExtra = 0;                /* No per-class extra data */
  107.     wc.cbWndExtra = 0;                /* No per-window extra data */
  108.     wc.hInstance = hInstance;        /* Application that owns the class */
  109.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  110.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  111.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  112.     wc.lpszMenuName =  "GenericMenu";    /* Name of menu resource in .RC file. */
  113.     wc.lpszClassName = "GenericWClass"; /* Name used in call to CreateWindow. */
  114.  
  115.     /* Register the window class and return success/failure code. */
  116.     return (RegisterClass(&wc));
  117. }
  118.  
  119. /****************************************************************************
  120.  
  121.     FUNCTION:  InitInstance(HANDLE, int)
  122.  
  123.     PURPOSE:  Saves instance handle and creates main window
  124.  
  125.     COMMENTS:
  126.  
  127.         This function is called at initialization time for every instance of
  128.         this application.  This function performs initialization tasks that
  129.         cannot be shared by multiple instances.
  130.  
  131.         In this case, we save the instance handle in a static variable and
  132.         create and display the main program window.
  133.         
  134. ****************************************************************************/
  135.  
  136. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  137. {
  138.     HWND    hWnd;    /* Main window handle */
  139.  
  140.     /* Save the instance handle in static variable, which will be used in
  141.        many subsequent calls from this application to Windows.    */
  142.  
  143.     hInst = hInstance;
  144.  
  145.     /* Create a main window for this application instance.    */
  146.  
  147.     hWnd = CreateWindow(
  148.         "GenericWClass",    /* See RegisterClass() call */
  149.         "Generic Sample Application",    /* Text for window title bar */
  150.         WS_OVERLAPPEDWINDOW,    /* Window style */
  151.         CW_USEDEFAULT,    /* Default horizontal position */
  152.         CW_USEDEFAULT,    /* Default vertical position */
  153.         CW_USEDEFAULT,    /* Default width */
  154.         CW_USEDEFAULT,    /* Default height */
  155.         NULL,    /* Overlapped windows have no parent */
  156.         NULL,    /* Use the window class menu */
  157.         hInstance,    /* This instance owns this window */
  158.         NULL    /* Pointer not needed */
  159.     );
  160.  
  161.     /* If window could not be created, return "failure" */
  162.  
  163.     if (!hWnd)
  164.         return (FALSE);
  165.  
  166.     /* Make the window visible; update its client area; and return "success" */
  167.  
  168.     ShowWindow(hWnd, nCmdShow);        /* Show the window */
  169.     UpdateWindow(hWnd);     /* Sends WM_PAINT message */
  170.     return (TRUE);    /* Returns the value from PostQuitMessage */
  171. }
  172.  
  173. /****************************************************************************
  174.  
  175.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  176.  
  177.     PURPOSE:  Processes messages
  178.  
  179.     MESSAGES:
  180.  
  181.         WM_COMMAND      - application menu (About dialog box)
  182.         WM_DESTROY      - destroy window
  183.  
  184.     COMMENTS:
  185.  
  186.         To process the IDM_ABOUT message, call MakeProcInstance() to get the
  187.         current instance address of the About() function.  Then call Dialog
  188.         box which will create the box according to the information in your
  189.         generic.rc file and turn control over to the About() function.    When
  190.         it returns, free the intance address.
  191.  
  192. ****************************************************************************/
  193.  
  194. LONG FAR PASCAL MainWndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  195. {
  196.     FARPROC lpProcAbout;          /* pointer to the "About" function */
  197.  
  198.     switch (message)
  199.     {
  200.         case WM_COMMAND:    /* message: command from application menu */
  201.             if (wParam == IDM_ABOUT)
  202.             {
  203.                 lpProcAbout = MakeProcInstance(About, hInst);
  204.  
  205.                 DialogBox(hInst,         /* current instance         */
  206.                     "AboutBox",             /* resource to use         */
  207.                     hWnd,             /* parent handle         */
  208.                     lpProcAbout);         /* About() instance address */
  209.  
  210.                 FreeProcInstance(lpProcAbout);
  211.                 break;
  212.             }
  213.             else    /* Lets Windows process it         */
  214.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  215.  
  216.         case WM_DESTROY:          /* message: window being destroyed */
  217.             PostQuitMessage(0);
  218.             break;
  219.  
  220.         default:              /* Passes it on if unproccessed     */
  221.             return (DefWindowProc(hWnd, message, wParam, lParam));
  222.     }
  223.     return (NULL);
  224. }
  225.  
  226.  
  227. /****************************************************************************
  228.  
  229.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  230.  
  231.     PURPOSE:  Processes messages for "About" dialog box
  232.  
  233.     MESSAGES:
  234.  
  235.         WM_INITDIALOG - initialize dialog box
  236.         WM_COMMAND      - Input received
  237.  
  238.     COMMENTS:
  239.  
  240.         No initialization is needed for this particular dialog box, but TRUE
  241.         must be returned to Windows.
  242.  
  243.         Wait for user to click on "Ok" button, then close the dialog box.
  244.  
  245. ****************************************************************************/
  246.  
  247. BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  248. {
  249.     switch (message)
  250.     {
  251.         case WM_INITDIALOG:        /* message: initialize dialog box */
  252.             return (TRUE);
  253.  
  254.         case WM_COMMAND:    /* message: received a command */
  255.             if (wParam == IDOK || wParam == IDCANCEL)
  256.             {
  257.                 EndDialog(hDlg, TRUE);    /* Exits the dialog box */
  258.                 return (TRUE);
  259.             }
  260.             break;
  261.     }
  262.     return (FALSE);        /* Didn't process a message */
  263. }
  264.