home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sb_bc.ddi / BC / CH5 / WINAPP-3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  4.6 KB  |  186 lines

  1. /***              Windows Application 3                ***/
  2. /***                                                   ***/
  3. /***              WINAPP-3 includes                    ***/
  4. /***                       (1) WINAPP-3.CPP            ***/
  5. /***                       (2) WINAPP-3.DEF            ***/
  6.  
  7. #include <windows.h>
  8.  
  9. // Declaration
  10.  
  11. long FAR PASCAL _export AppWndProc (HWND, WORD, WORD, LONG) ;
  12.  
  13.  
  14. class Main
  15. { public:
  16.     static HANDLE hInstance ;
  17.     static HANDLE hPrevInstance ;
  18.     static int    nCmdShow ;
  19.  
  20.     static int MessageLoop(void) ;
  21. } ;
  22.  
  23. HANDLE Main::hInstance = 0 ;
  24. HANDLE Main::hPrevInstance = 0 ;
  25. int    Main::nCmdShow = 0 ;
  26.  
  27. int Main::MessageLoop(void)
  28. { MSG msg ;
  29.  
  30.   while ( GetMessage(&msg, NULL, 0, 0) )
  31.     { TranslateMessage(&msg) ;
  32.       DispatchMessage(&msg) ;
  33.     }
  34.   return msg.wParam ;
  35. }
  36.  
  37.  
  38. ////////// Base Class -> Window
  39. //////////
  40. class Window
  41. { protected:
  42.     HWND hwnd ;
  43.  
  44.   public:
  45.     HWND GetHandle(void)     // get the window handle in the class
  46.     { return hwnd ; }
  47.  
  48.     BOOL Show(int nCmdShow)
  49.     { return ShowWindow(hwnd,nCmdShow) ; }
  50.  
  51.     void Update(void)
  52.     { UpdateWindow(hwnd) ; }
  53.  
  54.     virtual long AppWndProc(WORD message, WORD wParam, LONG lParam) = 0 ;
  55. } ;
  56.  
  57.  
  58. ////////// Derived Class -> MainWindow
  59. //////////
  60. class MainWindow : public Window
  61. { private:
  62.     static char szAppName[8] ;
  63.  
  64.   public :
  65.     static void Register(void) ;
  66.     MainWindow(void) ;
  67.     long AppWndProc(WORD message, WORD wParam, LONG lParam) ;
  68. } ;
  69.  
  70. char MainWindow::szAppName[] = "WinApp3" ;
  71.  
  72. void MainWindow::Register(void)
  73. { WNDCLASS wndclass ;
  74.  
  75.   wndclass.style      = CS_HREDRAW | CS_VREDRAW ;
  76.   wndclass.lpfnWndProc      = ::AppWndProc ;
  77.   wndclass.cbClsExtra       = 0 ;
  78.   wndclass.cbWndExtra      = sizeof(MainWindow *) ;
  79.   wndclass.hInstance       = Main::hInstance ;
  80.   wndclass.hIcon      = LoadIcon(NULL,IDI_APPLICATION) ;
  81.   wndclass.hCursor      = LoadCursor(NULL,IDC_ARROW) ;
  82.   wndclass.hbrBackground  = GetStockObject(WHITE_BRUSH) ;
  83.   wndclass.lpszMenuName      = NULL ;
  84.   wndclass.lpszClassName  = szAppName ;
  85.  
  86.   RegisterClass(&wndclass) ;
  87. }
  88.  
  89. MainWindow::MainWindow(void)
  90. {
  91.   hwnd = CreateWindow(szAppName,              // window class name
  92.               "Windows Application",  // window caption
  93.               WS_OVERLAPPEDWINDOW,    // window style
  94.               CW_USEDEFAULT,      // initial x position
  95.               0,                  // initial y position
  96.               CW_USEDEFAULT,      // initial x length
  97.               0,                  // initial y length
  98.               NULL,               // parent window handle
  99.               NULL,               // window menu handle
  100.               Main::hInstance,    // program instance handle
  101.               (LPSTR) this) ;     // parameters , different
  102.                       //   from WINAPP-1
  103.   Show(Main::nCmdShow) ;
  104.   Update() ;
  105. }
  106.  
  107. long MainWindow::AppWndProc(WORD message, WORD wParam, LONG lParam)
  108. {
  109.   switch (message)
  110.     { case WM_DESTROY:
  111.     PostQuitMessage(0) ;
  112.     break ;
  113.  
  114.       default:
  115.     return DefWindowProc(hwnd, message, wParam, lParam) ;
  116.     }
  117.   return 0 ;
  118. }
  119.  
  120. #if defined(__SMALL__) || defined(__MEDIUM__)
  121. inline Window *GetPointer( HWND hWnd )
  122. {
  123.     return (Window *) GetWindowWord( hWnd, 0 );
  124. }
  125. inline void SetPointer( HWND hWnd, Window *pWindow )
  126. {
  127.     SetWindowWord( hWnd, 0, (WORD) pWindow );
  128. }
  129.  
  130. // else pointers are far
  131. #elif defined(__LARGE__) || defined(__COMPACT__)
  132. inline Window *GetPointer( HWND hWnd )
  133. {
  134.     return (Window *) GetWindowLong( hWnd, 0 );
  135. }
  136. inline void SetPointer( HWND hWnd, Window *pWindow )
  137. {
  138.     SetWindowLong( hWnd, 0, (LONG) pWindow );
  139. }
  140.  
  141. #else
  142.     #error Choose another memory model!
  143. #endif
  144.  
  145.  
  146. long FAR PASCAL _export AppWndProc (HWND hwnd,   WORD message,
  147.                     WORD wParam, LONG lParam)
  148. { Window *pWindow = GetPointer( hwnd );
  149.  
  150.   if ( pWindow == 0 )
  151.      { if ( message == WM_CREATE )
  152.       { LPCREATESTRUCT lpcs;
  153.  
  154.         lpcs = (LPCREATESTRUCT) lParam;
  155.         pWindow = (Window *) lpcs->lpCreateParams;
  156.  
  157.         SetPointer(hwnd,pWindow);
  158.  
  159.         return pWindow->AppWndProc(message, wParam, lParam);
  160.       }
  161.        else return DefWindowProc( hwnd, message, wParam, lParam );
  162.      }
  163.   else return pWindow->AppWndProc(message, wParam, lParam);
  164. }
  165.  
  166.  
  167. // Turn off warning: Parameter 'lpszCmdLine' is never used ...
  168. #pragma argsused
  169.  
  170. // Turn off warning: 'mainwindow' is assigned a value that is never ...
  171. #pragma option -w-aus
  172.  
  173. int PASCAL WinMain(HANDLE hInstance,
  174.            HANDLE hPrevInstance,
  175.            LPSTR  lpszCmdLine,
  176.            int    nCmdShow)
  177. { Main::hInstance = hInstance ;
  178.   Main::hPrevInstance = hPrevInstance ;
  179.   Main::nCmdShow = nCmdShow ;
  180.  
  181.   if (! Main::hPrevInstance) MainWindow::Register() ;
  182.  
  183.   MainWindow mainwindow ;
  184.  
  185.   return Main::MessageLoop() ;
  186. }