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

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