home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol12n07.zip / WINAPP.ZIP / WINAP.ZIP / WINAPP.C < prev    next >
Text File  |  1993-03-25  |  8KB  |  205 lines

  1. // WINAPP - WIN16 and WIN32 Portable Application Skeleton
  2. // Copyright (C) 1992 Ray Duncan
  3. // PC Magazine * Ziff Davis Publishing
  4.  
  5. #define dim(x) (sizeof(x) / sizeof(x[0]))   // returns no. of elements
  6.  
  7. #include "windows.h"
  8. #include "winapp.h"
  9.  
  10. HANDLE hInst;                               // module instance handle
  11. HWND hFrame;                                // handle for frame window
  12.  
  13. TCHAR szShortAppName[] = TEXT("WinApp");      // short application name
  14. TCHAR szAppName[] = TEXT("Windows Demo App"); // long application name
  15. TCHAR szMenuName[] = TEXT("WinAppMenu");      // name of menu resource
  16. TCHAR szIconName[] = TEXT("WinAppIcon");      // name of icon resource
  17.  
  18. //
  19. // Table of window messages supported by FrameWndProc()
  20. // and the functions which correspond to each message.
  21. //
  22. struct decodeUINT messages[] = {
  23.     WM_PAINT, DoPaint,
  24.     WM_COMMAND, DoCommand,
  25.     WM_DESTROY, DoDestroy, } ;
  26.  
  27. //
  28. // Table of menubar item IDs and their corresponding functions.
  29. //
  30. struct decodeUINT menuitems[] = {
  31.     IDM_EXIT, DoMenuExit,
  32.     IDM_ABOUT, DoMenuAbout, } ;
  33.  
  34. //
  35. // WinMain -- entry point for this application from Windows.
  36. //
  37. INT APIENTRY WinMain(HANDLE hInstance,
  38.     HANDLE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
  39. {
  40.     MSG msg;
  41.  
  42.     hInst = hInstance;                      // save this instance handle
  43.  
  44.     if(!hPrevInstance)                      // if first instance,
  45.         if(!InitApp(hInstance))             // register window class
  46.             return(FALSE);                  // exit if couldn't register
  47.  
  48.     if(!InitInstance(hInstance, nCmdShow))  // create this instance's window
  49.         return(FALSE);                      // exit if create failed
  50.  
  51.     while(GetMessage(&msg, NULL, 0, 0))     // while message != WM_QUIT
  52.     {
  53.         TranslateMessage(&msg);             // translate virtual key codes
  54.         DispatchMessage(&msg);              // dispatch message to window
  55.     }
  56.  
  57.     TermInstance(hInstance);                // clean up for this instance
  58.     return(msg.wParam);                     // return code = WM_QUIT value
  59. }
  60.  
  61. //
  62. // InitApp --- global initialization code for this application.
  63. //
  64. BOOL InitApp(HANDLE hInstance)
  65. {
  66.     WNDCLASS  wc;
  67.  
  68.     // set parameters for frame window class
  69.     wc.style = CS_HREDRAW|CS_VREDRAW;       // class style
  70.     wc.lpfnWndProc = FrameWndProc;          // class callback function
  71.     wc.cbClsExtra = 0;                      // extra per-class data
  72.     wc.cbWndExtra = 0;                      // extra per-window data
  73.     wc.hInstance = hInstance;               // handle of class owner
  74.     wc.hIcon = LoadIcon(hInst, szIconName); // application icon
  75.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);       // default cursor
  76.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); // background color 
  77.     wc.lpszMenuName =  szMenuName;          // name of menu resource
  78.     wc.lpszClassName = szShortAppName;      // name of window class
  79.  
  80.     return(RegisterClass(&wc));             // register class, return status
  81. }
  82.                     // text for title bar
  83.         WS_OVERLAPPEDWINDOW,                // window style
  84.         CW_USEDEFAULT, CW_USEDEFAULT,       // default position
  85.         CW_USEDEFAULT, CW_USEDEFAULT,       // default size
  86.         NULL,                               // no parent window
  87.         NULL,                               // use class default menu
  88.         hInstance,                          // window owner
  89.         NULL);                              // unused pointer
  90.  
  91.     if(!hFrame) return(FALSE);              // error, can't create window
  92.  
  93.     ShowWindow(hFrame, nCmdShow);           // make frame window visible
  94.     UpdateWindow(hFrame);                   // force WM_PAINT message
  95.     return(TRUE);                           // return success flag
  96. }
  97.  
  98. //
  99. // TermInstance -- instance termination code for this application.
  100. //
  101. BOOL TermInstance(HANDLE hinstance)
  102. {
  103.     return(TRUE);                           // return success flag
  104. }
  105.  
  106. //
  107. // FrameWndProc --- callback function for application frame window.
  108. //
  109. LONG CALLBACK FrameWndProc(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  110. {
  111.     INT i;                                  // scratch variable
  112.  
  113.     for(i = 0; i < dim(messages); i++)      // decode window message and
  114.     {                                       // run corresponding function
  115.         if(wMsg == messages[i].Code)
  116.             return((*messages[i].Fxn)(hWnd, wMsg, wParam, lParam));
  117.     }
  118.                                             // or hand off to Windows
  119.     return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  120. }
  121.  
  122. //
  123. // DoCommand -- process WM_COMMAND message for frame window by
  124. // decoding the menubar item with the menuitems[] array, then
  125. // running the corresponding function to process the command.
  126. // 
  127. LONG DoCommand(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  128. {
  129.     INT i;                                  // scratch variable
  130.  
  131.     for(i = 0; i < dim(menuitems); i++)     // decode menu command and
  132.     {                                       // run corresponding function
  133.         if(wParam == menuitems[i].Code)
  134.             return((*menuitems[i].Fxn)(hWnd, wMsg, wParam, lParam));
  135.     }
  136.  
  137.     return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  138. }
  139.  
  140. //
  141. // DoDestroy -- process WM_DESTROY message for frame window.
  142. // 
  143. LONG DoDestroy(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  144. {
  145.     PostQuitMessage(0);                     // force WM_QUIT message to
  146.     return(FALSE);                          // terminate the event loop
  147. }
  148.  
  149. //
  150. // DoPaint -- process WM_PAINT message for frame window.
  151. // 
  152. LONG DoPaint(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  153. {
  154.     HDC hdc;
  155.     PAINTSTRUCT ps;
  156.     RECT rect;
  157.     HFONT hfont;
  158.  
  159.     hdc = BeginPaint(hWnd, &ps);            // get device context
  160.     GetClientRect(hWnd, &rect);             // get client area dimensions
  161.     hfont = CreateFont(-36, 0, 0, 0, 700,   // get handle for pretty font
  162.         TRUE, 0, 0, ANSI_CHARSET,
  163.         OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  164.         DEFAULT_QUALITY, (FF_MODERN << 4) + DEFAULT_PITCH,
  165.         TEXT("Ariel"));
  166.     SelectObject(hdc, hfont);               // realize font in DC
  167.     DrawText(hdc, TEXT("Hey, Dude!"), -1,   // paint text in window
  168.         &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); 
  169.     EndPaint(hWnd, &ps);                    // release device context
  170.     return(FALSE);                          // terminate the event loop
  171. }
  172.  
  173. //
  174. // DoMenuExit -- process File-Exit command from menu bar.
  175. // 
  176. LONG DoMenuExit(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  177. {
  178.     SendMessage (hWnd, WM_CLOSE, 0, 0L);    // send window close message    
  179.     return(FALSE);                          // to shut down the app
  180. }
  181.  
  182. //
  183. // DoMenuAbout -- process File-About command from menu bar.
  184. // 
  185. LONG DoMenuAbout(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  186. {
  187.     WNDPROC lpProcAbout;                    // scratch pointer
  188.  
  189.     lpProcAbout = MakeProcInstance((WNDPROC) AboutDlgProc, hInst);
  190.     DialogBox(hInst, TEXT("AboutBox"), hWnd, lpProcAbout);         
  191.     FreeProcInstance(lpProcAbout);
  192.     return(FALSE);                              
  193. }
  194.  
  195. //
  196. // AboutDlgProc -- callback routine for About... dialog
  197. // 
  198. BOOL CALLBACK AboutDlgProc (HWND hwnd, UINT msg, UINT wParam, LONG lParam)
  199. {
  200.     if((msg == WM_COMMAND) && (wParam == IDOK))
  201.         EndDialog(hwnd, 0);
  202.     else
  203.         return(FALSE);
  204. }
  205.