home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 17 / winapp / winapp.c < prev    next >
Text File  |  1992-08-28  |  8KB  |  216 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. char szShortAppName[] = "WinApp";           // short application name
  14. char szAppName[] = "Windows Demo App";      // long application name
  15. char szMenuName[] = "WinAppMenu";           // name of menu resource
  16. char szIconName[] = "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.  
  83. //
  84. // InitInstance --- instance initialization code for this application.
  85. //
  86. BOOL InitInstance(HANDLE hInstance, INT nCmdShow)
  87. {
  88.     hFrame = CreateWindow(                  // create frame window
  89.         szShortAppName,                     // window class name
  90.         szAppName,                          // text for title bar
  91.         WS_OVERLAPPEDWINDOW,                // window style
  92.         CW_USEDEFAULT, CW_USEDEFAULT,       // default position
  93.         CW_USEDEFAULT, CW_USEDEFAULT,       // default size
  94.         NULL,                               // no parent window
  95.         NULL,                               // use class default menu
  96.         hInstance,                          // window owner
  97.         NULL);                              // unused pointer
  98.  
  99.     if(!hFrame) return(FALSE);              // error, can't create window
  100.  
  101.     ShowWindow(hFrame, nCmdShow);           // make frame window visible
  102.     UpdateWindow(hFrame);                   // force WM_PAINT message
  103.     return(TRUE);                           // return success flag
  104. }
  105.  
  106. //
  107. // TermInstance -- instance termination code for this application.
  108. //
  109. BOOL TermInstance(HANDLE hinstance)
  110. {
  111.     return(TRUE);                           // return success flag
  112. }
  113.  
  114. //
  115. // FrameWndProc --- callback function for application frame window.
  116. //
  117. LONG CALLBACK FrameWndProc(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  118. {
  119.     INT i;                                  // scratch variable
  120.  
  121.     for(i = 0; i < dim(messages); i++)      // decode window message and
  122.     {                                       // run corresponding function
  123.         if(wMsg == messages[i].Code)
  124.             return((*messages[i].Fxn)(hWnd, wMsg, wParam, lParam));
  125.     }
  126.                                             // or hand off to Windows
  127.     return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  128. }
  129.  
  130. //
  131. // DoCommand -- process WM_COMMAND message for frame window by
  132. // decoding the menubar item with the menuitems[] array, then
  133. // running the corresponding function to process the command.
  134. // 
  135. LONG DoCommand(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  136. {
  137.     INT i;                                  // scratch variable
  138.  
  139.     for(i = 0; i < dim(menuitems); i++)     // decode menu command and
  140.     {                                       // run corresponding function
  141.         if(wParam == menuitems[i].Code)
  142.             return((*menuitems[i].Fxn)(hWnd, wMsg, wParam, lParam));
  143.     }
  144.  
  145.     return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  146. }
  147.  
  148. //
  149. // DoDestroy -- process WM_DESTROY message for frame window.
  150. // 
  151. LONG DoDestroy(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  152. {
  153.     PostQuitMessage(0);                     // force WM_QUIT message to
  154.     return(0);                              // terminate the event loop
  155. }
  156.  
  157. //
  158. // DoPaint -- process WM_PAINT message for frame window.
  159. // 
  160. LONG DoPaint(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  161. {
  162.     HDC hdc;
  163.     PAINTSTRUCT ps;
  164.     RECT rect;
  165.     HFONT hfont;
  166.  
  167.     hdc = BeginPaint(hWnd, &ps);            // get device context
  168.     GetClientRect(hWnd, &rect);             // get client area dimensions
  169.     hfont = CreateFont(-36, 0, 0, 0, 700,   // get handle for pretty font
  170.         TRUE, 0, 0, ANSI_CHARSET,
  171.         OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  172.         DEFAULT_QUALITY, (FF_MODERN << 4) + DEFAULT_PITCH,
  173.         "Ariel");
  174.     SelectObject(hdc, hfont);               // realize font in DC
  175.     DrawText(hdc, "Hey, Dude!", -1,         // paint text in window
  176.         &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); 
  177.     EndPaint(hWnd, &ps);                    // release device context
  178.     return(0);                              // terminate the event loop
  179. }
  180.  
  181. //
  182. // DoMenuExit -- process File-Exit command from menu bar.
  183. // 
  184. LONG DoMenuExit(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  185. {
  186.     SendMessage (hWnd, WM_CLOSE, 0, 0L);    // send window close message    
  187.     return(0);                              // to shut down the app
  188. }
  189.  
  190. //
  191. // DoMenuAbout -- process File-About command from menu bar.
  192. // 
  193. LONG DoMenuAbout(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  194. {
  195.     WNDPROC lpProcAbout;                    // scratch far pointer
  196.  
  197.     lpProcAbout = MakeProcInstance((WNDPROC)AboutDlgProc, hInst);
  198.     DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);         
  199.     FreeProcInstance(lpProcAbout);
  200.     return(0);                              
  201. }
  202.  
  203. //
  204. // AboutDlgProc -- callback routine for About... dialog
  205. // 
  206. BOOL CALLBACK AboutDlgProc (HWND hwnd, UINT msg, UINT wParam, LONG lParam)
  207. {
  208.     if((msg == WM_COMMAND) && (wParam == IDOK))
  209.         EndDialog(hwnd, 0);
  210.     else
  211.         return(FALSE);
  212. }
  213.  
  214.  
  215. 
  216.