Annotation of mstools/samples/generic/generic.c, revision 1.1.1.2

1.1       root        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: 
1.1.1.2 ! root       50: int WinMain(
1.1       root       51:     HANDLE hInstance,
                     52:     HANDLE hPrevInstance,
                     53:     LPSTR lpCmdLine,
                     54:     int nCmdShow
                     55:     )
                     56: {
                     57: 
                     58:     MSG msg;                                /* message                      */
                     59: 
                     60:     UNREFERENCED_PARAMETER( lpCmdLine );
                     61: 
                     62:     if (!hPrevInstance)                         /* Other instances of app running? */
                     63:        if (!InitApplication(hInstance)) /* Initialize shared things */
                     64:            return (FALSE);              /* Exits if unable to initialize     */
                     65: 
                     66:     /* Perform initializations that apply to a specific instance */
                     67: 
                     68:     if (!InitInstance(hInstance, nCmdShow))
                     69:         return (FALSE);
                     70: 
                     71:     /* Acquire and dispatch messages until a WM_QUIT message is received. */
                     72: 
                     73:     while (GetMessage(&msg,       /* message structure                      */
                     74:            NULL,                  /* handle of window receiving the message */
                     75:            NULL,                  /* lowest message to examine              */
                     76:            NULL))                 /* highest message to examine             */
                     77:        {
                     78:        TranslateMessage(&msg);    /* Translates virtual key codes           */
                     79:        DispatchMessage(&msg);     /* Dispatches message to window           */
                     80:     }
                     81:     return (msg.wParam);          /* Returns the value from PostQuitMessage */
                     82: }
                     83: 
                     84: 
                     85: /****************************************************************************
                     86: 
                     87:     FUNCTION: InitApplication(HANDLE)
                     88: 
                     89:     PURPOSE: Initializes window data and registers window class
                     90: 
                     91:     COMMENTS:
                     92: 
                     93:         This function is called at initialization time only if no other 
                     94:         instances of the application are running.  This function performs 
                     95:         initialization tasks that can be done once for any number of running 
                     96:         instances.  
                     97: 
                     98:         In this case, we initialize a window class by filling out a data 
                     99:         structure of type WNDCLASS and calling the Windows RegisterClass() 
                    100:         function.  Since all instances of this application use the same window 
                    101:         class, we only need to do this when the first instance is initialized.  
                    102: 
                    103: 
                    104: ****************************************************************************/
                    105: 
                    106: BOOL InitApplication(HANDLE hInstance)       /* current instance            */
                    107: {
                    108:     WNDCLASS  wc;
                    109: 
                    110:     /* Fill in window class structure with parameters that describe the       */
                    111:     /* main window.                                                           */
                    112: 
                    113:     wc.style = NULL;                    /* Class style(s).                    */
                    114:     wc.lpfnWndProc = (WNDPROC)MainWndProc;       /* Function to retrieve messages for  */
                    115:                                         /* windows of this class.             */
                    116:     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
                    117:     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
                    118:     wc.hInstance = hInstance;          /* Application that owns the class.   */
                    119:     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
                    120:     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
                    121:     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
                    122:     wc.lpszMenuName =  "GenericMenu";   /* Name of menu resource in .RC file. */
                    123:     wc.lpszClassName = "GenericWClass"; /* Name used in call to CreateWindow. */
                    124: 
                    125:     /* Register the window class and return success/failure code. */
                    126: 
                    127:     return (RegisterClass(&wc));
                    128: 
                    129: }
                    130: 
                    131: 
                    132: /****************************************************************************
                    133: 
                    134:     FUNCTION:  InitInstance(HANDLE, int)
                    135: 
                    136:     PURPOSE:  Saves instance handle and creates main window
                    137: 
                    138:     COMMENTS:
                    139: 
                    140:         This function is called at initialization time for every instance of 
                    141:         this application.  This function performs initialization tasks that 
                    142:         cannot be shared by multiple instances.  
                    143: 
                    144:         In this case, we save the instance handle in a static variable and 
                    145:         create and display the main program window.  
                    146:         
                    147: ****************************************************************************/
                    148: 
                    149: BOOL InitInstance(
                    150:     HANDLE          hInstance,          /* Current instance identifier.       */
                    151:     int             nCmdShow)           /* Param for first ShowWindow() call. */
                    152: {
                    153:     HWND            hWnd;               /* Main window handle.                */
                    154: 
                    155:     /* Save the instance handle in static variable, which will be used in  */
                    156:     /* many subsequence calls from this application to Windows.            */
                    157: 
                    158:     hInst = hInstance;
                    159: 
                    160:     /* Create a main window for this application instance.  */
                    161: 
                    162:     hWnd = CreateWindow(
                    163:         "GenericWClass",                /* See RegisterClass() call.          */
                    164:         "Generic Sample Application",   /* Text for window title bar.         */
                    165:         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
                    166:         CW_USEDEFAULT,                  /* Default horizontal position.       */
                    167:         CW_USEDEFAULT,                  /* Default vertical position.         */
                    168:         CW_USEDEFAULT,                  /* Default width.                     */
                    169:         CW_USEDEFAULT,                  /* Default height.                    */
                    170:         NULL,                           /* Overlapped windows have no parent. */
                    171:         NULL,                           /* Use the window class menu.         */
                    172:         hInstance,                      /* This instance owns this window.    */
                    173:         NULL                            /* Pointer not needed.                */
                    174:     );
                    175: 
                    176:     /* If window could not be created, return "failure" */
                    177: 
                    178:     if (!hWnd)
                    179:         return (FALSE);
                    180: 
                    181:     /* Make the window visible; update its client area; and return "success" */
                    182: 
                    183:     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
                    184:     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
                    185:     return (TRUE);               /* Returns the value from PostQuitMessage */
                    186: 
                    187: }
                    188: 
                    189: /****************************************************************************
                    190: 
                    191:     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
                    192: 
                    193:     PURPOSE:  Processes messages
                    194: 
                    195:     MESSAGES:
                    196: 
                    197:        WM_COMMAND    - application menu (About dialog box)
                    198:        WM_DESTROY    - destroy window
                    199: 
                    200:     COMMENTS:
                    201: 
                    202:        To process the IDM_ABOUT message, call MakeProcInstance() to get the
                    203:        current instance address of the About() function.  Then call Dialog
                    204:        box which will create the box according to the information in your
                    205:        generic.rc file and turn control over to the About() function.  When
                    206:        it returns, free the intance address.
                    207: 
                    208: ****************************************************************************/
                    209: 
                    210: LONG APIENTRY MainWndProc(
                    211:        HWND hWnd,                /* window handle                   */
                    212:        UINT message,             /* type of message                 */
                    213:        UINT wParam,              /* additional information          */
                    214:        LONG lParam)              /* additional information          */
                    215: {
                    216:     FARPROC lpProcAbout;         /* pointer to the "About" function */
                    217: 
                    218:     switch (message) {
                    219:        case WM_COMMAND:           /* message: command from application menu */
                    220:            if (LOWORD(wParam) == IDM_ABOUT) {
                    221:                    lpProcAbout = MakeProcInstance((FARPROC)About, hInst);
                    222: 
                    223:                DialogBox(hInst,                 /* current instance         */
                    224:                    "AboutBox",                  /* resource to use          */
                    225:                        hWnd,                    /* parent handle            */
1.1.1.2 ! root      226:                        lpProcAbout);            /* About() instance address */
1.1       root      227: 
                    228:                FreeProcInstance(lpProcAbout);
                    229:                break;
                    230:            }
                    231:            else                            /* Lets Windows process it       */
                    232:                    return (DefWindowProc(hWnd, message, wParam, lParam));
                    233: 
                    234:        case WM_DESTROY:                  /* message: window being destroyed */
                    235:            PostQuitMessage(0);
                    236:            break;
                    237: 
                    238:        default:                          /* Passes it on if unproccessed    */
                    239:            return (DefWindowProc(hWnd, message, wParam, lParam));
                    240:     }
                    241:     return (NULL);
                    242: }
                    243: 
                    244: 
                    245: /****************************************************************************
                    246: 
                    247:     FUNCTION: About(HWND, unsigned, WORD, LONG)
                    248: 
                    249:     PURPOSE:  Processes messages for "About" dialog box
                    250: 
                    251:     MESSAGES:
                    252: 
                    253:        WM_INITDIALOG - initialize dialog box
                    254:        WM_COMMAND    - Input received
                    255: 
                    256:     COMMENTS:
                    257: 
                    258:        No initialization is needed for this particular dialog box, but TRUE
                    259:        must be returned to Windows.
                    260: 
                    261:        Wait for user to click on "Ok" button, then close the dialog box.
                    262: 
                    263: ****************************************************************************/
                    264: 
                    265: BOOL APIENTRY About(
                    266:        HWND hDlg,                /* window handle of the dialog box */
                    267:        UINT message,             /* type of message                 */
                    268:        UINT wParam,            /* message-specific information    */
                    269:        LONG lParam)
                    270: {
                    271:     switch (message) {
                    272:        case WM_INITDIALOG:                /* message: initialize dialog box */
                    273:            return (TRUE);
                    274: 
                    275:        case WM_COMMAND:                      /* message: received a command */
                    276:            if (LOWORD(wParam) == IDOK          /* "OK" box selected?        */
                    277:                || LOWORD(wParam) == IDCANCEL) {        /* System menu close command? */
                    278:                EndDialog(hDlg, TRUE);        /* Exits the dialog box        */
                    279:                return (TRUE);
                    280:            }
                    281:            break;
                    282:     }
                    283:     return (FALSE);                          /* Didn't process a message    */
                    284:        UNREFERENCED_PARAMETER(lParam);
                    285: }
1.1.1.2 ! root      286: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.