home *** CD-ROM | disk | FTP | other *** search
- /*** Windows Application 3 ***/
- /*** ***/
- /*** WINAPP-3 includes ***/
- /*** (1) WINAPP-3.CPP ***/
- /*** (2) WINAPP-3.DEF ***/
-
- #include <windows.h>
-
- // Declaration
-
- long FAR PASCAL _export AppWndProc (HWND, WORD, WORD, LONG) ;
-
-
- class Main
- { public:
- static HANDLE hInstance ;
- static HANDLE hPrevInstance ;
- static int nCmdShow ;
-
- static int MessageLoop(void) ;
- } ;
-
- HANDLE Main::hInstance = 0 ;
- HANDLE Main::hPrevInstance = 0 ;
- int Main::nCmdShow = 0 ;
-
- int Main::MessageLoop(void)
- { MSG msg ;
-
- while ( GetMessage(&msg, NULL, 0, 0) )
- { TranslateMessage(&msg) ;
- DispatchMessage(&msg) ;
- }
- return msg.wParam ;
- }
-
-
- ////////// Base Class -> Window
- //////////
- class Window
- { protected:
- HWND hwnd ;
-
- public:
- HWND GetHandle(void) // get the window handle in the class
- { return hwnd ; }
-
- BOOL Show(int nCmdShow)
- { return ShowWindow(hwnd,nCmdShow) ; }
-
- void Update(void)
- { UpdateWindow(hwnd) ; }
-
- virtual long AppWndProc(WORD message, WORD wParam, LONG lParam) = 0 ;
- } ;
-
-
- ////////// Derived Class -> MainWindow
- //////////
- class MainWindow : public Window
- { private:
- static char szAppName[8] ;
-
- public :
- static void Register(void) ;
- MainWindow(void) ;
- long AppWndProc(WORD message, WORD wParam, LONG lParam) ;
- } ;
-
- char MainWindow::szAppName[] = "WinApp3" ;
-
- void MainWindow::Register(void)
- { WNDCLASS wndclass ;
-
- wndclass.style = CS_HREDRAW | CS_VREDRAW ;
- wndclass.lpfnWndProc = ::AppWndProc ;
- wndclass.cbClsExtra = 0 ;
- wndclass.cbWndExtra = sizeof(MainWindow *) ;
- wndclass.hInstance = Main::hInstance ;
- wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION) ;
- wndclass.hCursor = LoadCursor(NULL,IDC_ARROW) ;
- wndclass.hbrBackground = GetStockObject(WHITE_BRUSH) ;
- wndclass.lpszMenuName = NULL ;
- wndclass.lpszClassName = szAppName ;
-
- RegisterClass(&wndclass) ;
- }
-
- MainWindow::MainWindow(void)
- {
- hwnd = CreateWindow(szAppName, // window class name
- "Windows Application", // window caption
- WS_OVERLAPPEDWINDOW, // window style
- CW_USEDEFAULT, // initial x position
- 0, // initial y position
- CW_USEDEFAULT, // initial x length
- 0, // initial y length
- NULL, // parent window handle
- NULL, // window menu handle
- Main::hInstance, // program instance handle
- (LPSTR) this) ; // parameters , different
- // from WINAPP-1
- Show(Main::nCmdShow) ;
- Update() ;
- }
-
- long MainWindow::AppWndProc(WORD message, WORD wParam, LONG lParam)
- {
- switch (message)
- { case WM_DESTROY:
- PostQuitMessage(0) ;
- break ;
-
- default:
- return DefWindowProc(hwnd, message, wParam, lParam) ;
- }
- return 0 ;
- }
-
- #if defined(__SMALL__) || defined(__MEDIUM__)
- inline Window *GetPointer( HWND hWnd )
- {
- return (Window *) GetWindowWord( hWnd, 0 );
- }
- inline void SetPointer( HWND hWnd, Window *pWindow )
- {
- SetWindowWord( hWnd, 0, (WORD) pWindow );
- }
-
- // else pointers are far
- #elif defined(__LARGE__) || defined(__COMPACT__)
- inline Window *GetPointer( HWND hWnd )
- {
- return (Window *) GetWindowLong( hWnd, 0 );
- }
- inline void SetPointer( HWND hWnd, Window *pWindow )
- {
- SetWindowLong( hWnd, 0, (LONG) pWindow );
- }
-
- #else
- #error Choose another memory model!
- #endif
-
-
- long FAR PASCAL _export AppWndProc (HWND hwnd, WORD message,
- WORD wParam, LONG lParam)
- { Window *pWindow = GetPointer( hwnd );
-
- if ( pWindow == 0 )
- { if ( message == WM_CREATE )
- { LPCREATESTRUCT lpcs;
-
- lpcs = (LPCREATESTRUCT) lParam;
- pWindow = (Window *) lpcs->lpCreateParams;
-
- SetPointer(hwnd,pWindow);
-
- return pWindow->AppWndProc(message, wParam, lParam);
- }
- else return DefWindowProc( hwnd, message, wParam, lParam );
- }
- else return pWindow->AppWndProc(message, wParam, lParam);
- }
-
-
- // Turn off warning: Parameter 'lpszCmdLine' is never used ...
- #pragma argsused
-
- // Turn off warning: 'mainwindow' is assigned a value that is never ...
- #pragma option -w-aus
-
- int PASCAL WinMain(HANDLE hInstance,
- HANDLE hPrevInstance,
- LPSTR lpszCmdLine,
- int nCmdShow)
- { Main::hInstance = hInstance ;
- Main::hPrevInstance = hPrevInstance ;
- Main::nCmdShow = nCmdShow ;
-
- if (! Main::hPrevInstance) MainWindow::Register() ;
-
- MainWindow mainwindow ;
-
- return Main::MessageLoop() ;
- }