home *** CD-ROM | disk | FTP | other *** search
- /*** Windows Application 5 ***/
- /*** ***/
- /*** WINAPP-5 includes ***/
- /*** (1) WINAPP-5.CPP ***/
- /*** (2) WINAPP-5.DEF ***/
-
- #pragma hdrfile "windows.sym"
- #include <windows.h>
- #pragma hdrstop
-
- #include"winapp-2.h"
-
- #ifndef min
- #define min(a,b) (((a) < (b)) ? (a) : (b))
- #endif
-
- #ifndef max
- #define max(a,b) (((a) > (b)) ? (a) : (b))
- #endif
-
- // 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 CreateProc(HWND hwnd,
- WORD wParam, LONG lParam) = 0 ;
-
- virtual long AppWndProc(WORD message,
- WORD wParam, LONG lParam) = 0 ;
- } ;
-
-
- class ScrollBar : public Window
- { private :
- int nBar ;
- short int nMax, nPos, nInc ;
-
- public :
-
- inline void Init(HWND hWnd, int bar)
- { hwnd = hWnd ;
- nBar = bar ;
- }
-
- inline short int Get_nMax(void)
- { return nMax ; }
-
- inline short int Get_nPos(void)
- { return nPos ; }
-
- inline short int Get_nInc(void)
- { return nInc ; }
-
- inline void Set_nMax(short int m)
- { nMax = m ; }
-
- inline void Set_nPos(short int p)
- { nPos = p ; }
-
- inline void Set_nInc(short int i)
- { nInc = i ; }
-
- inline void SetRange(BOOL bRedraw)
- { SetScrollRange(hwnd, nBar, 0, nMax, bRedraw) ; }
-
- inline void SetPos(BOOL bRedraw)
- { SetScrollPos(hwnd, nBar, nPos, bRedraw) ; }
-
- inline void ScrollWin(int XAmount, int YAmount)
- { ScrollWindow(hwnd, XAmount, YAmount, NULL, NULL) ; }
-
- virtual long CreateProc(HWND hwnd,
- WORD wParam, LONG lParam) = 0 ;
-
- virtual long AppWndProc(WORD message,
- WORD wParam, LONG lParam) = 0 ;
- } ;
-
-
- ////////// Derived Class -> MainWindow
- //////////
- class MainWindow : public Window
- { private:
- static char szAppName[8] ;
- struct { short int cxChar, cyChar, cxCaps, nMaxX ; } FontSize ;
-
- ScrollBar VSB, HSB ;
-
- public :
- static void Register(void) ;
- MainWindow(void) ; // constructor
-
- virtual long CreateProc(HWND hwnd,
- WORD wParam, LONG lParam) ;
-
- virtual long AppWndProc(WORD message,
- WORD wParam, LONG lParam) ;
- } ;
-
- char MainWindow::szAppName[] = "WinApp5" ;
-
- 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 2", // window caption
- WS_OVERLAPPEDWINDOW |
- WS_VSCROLL |
- WS_HSCROLL , // 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-2
- Show(Main::nCmdShow) ;
- Update() ;
- }
-
-
- // special case
-
- #pragma argsused
- long MainWindow::CreateProc(HWND hwnd,
- WORD wParam, LONG lParam)
- { HDC hdc ;
- TEXTMETRIC tm ;
-
- hdc = GetDC(hwnd) ;
-
- GetTextMetrics(hdc,&tm) ;
- FontSize.cxChar = tm.tmAveCharWidth ;
- FontSize.cyChar = tm.tmHeight + tm.tmExternalLeading ;
- FontSize.cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) *
- FontSize.cxChar / 2 ;
-
- ReleaseDC(hwnd,hdc) ;
-
- FontSize.nMaxX = 20 * FontSize.cxCaps + 45 * FontSize.cxChar ;
-
- HSB.Init(hwnd, SB_HORZ) ;
- VSB.Init(hwnd, SB_VERT) ;
-
- return 0L ;
- }
-
-
- long MainWindow::AppWndProc(WORD message,
- WORD wParam, LONG lParam)
- { HDC hdc ;
- PAINTSTRUCT ps ;
-
- static short int cxClient, cyClient ;
-
- short int nBeginLine, nEndLine ;
- short int i,x,y ;
-
- switch (message)
- { case WM_SIZE :
- cxClient = LOWORD(lParam) ;
- cyClient = HIWORD(lParam) ;
-
- VSB.Set_nMax(max(0, NUMLINE+2-cyClient/FontSize.cyChar));
- VSB.Set_nPos( min(VSB.Get_nMax(),VSB.Get_nPos()) ) ;
- VSB.SetRange(FALSE) ;
- VSB.SetPos(TRUE) ;
-
- HSB.Set_nMax( max(0, (FontSize.nMaxX-cxClient) /
- FontSize.cxChar+2) ) ;
- HSB.Set_nPos( min(HSB.Get_nMax(),HSB.Get_nPos()) ) ;
- HSB.SetRange(FALSE) ;
- HSB.SetPos(TRUE) ;
-
- break ;
-
-
- case WM_VSCROLL :
- switch (wParam)
- { case SB_TOP :
- VSB.Set_nInc(VSB.Get_nInc() - VSB.Get_nPos()) ;
- break ;
-
- case SB_BOTTOM :
- VSB.Set_nInc(VSB.Get_nMax() - VSB.Get_nPos()) ;
- break ;
-
- case SB_LINEUP :
- VSB.Set_nInc(-1) ;
- break ;
-
- case SB_LINEDOWN :
- VSB.Set_nInc(1) ;
- break ;
-
- case SB_PAGEUP :
- VSB.Set_nInc(min(-1,-cyClient/FontSize.cyChar) );
- break ;
-
- case SB_PAGEDOWN :
- VSB.Set_nInc(max(1,cyClient/FontSize.cyChar) );
- break ;
-
- case SB_THUMBTRACK :
- VSB.Set_nInc(LOWORD(lParam)-VSB.Get_nPos()) ;
- break ;
-
- default :
- VSB.Set_nInc(0) ;
- break ;
- }
-
- VSB.Set_nInc(min(VSB.Get_nInc() ,
- VSB.Get_nMax()-VSB.Get_nPos()) ) ;
- VSB.Set_nInc(max(VSB.Get_nInc() ,
- -VSB.Get_nPos()) ) ;
-
- if (VSB.Get_nInc())
- { VSB.Set_nPos(VSB.Get_nPos() + VSB.Get_nInc() ) ;
- VSB.ScrollWin(0, -FontSize.cyChar*VSB.Get_nInc()) ;
- VSB.SetPos(TRUE) ;
- Update() ;
- }
- break ;
-
-
- case WM_HSCROLL :
- switch (wParam)
- { case SB_TOP :
- HSB.Set_nInc(HSB.Get_nInc() - HSB.Get_nPos()) ;
- break ;
-
- case SB_BOTTOM :
- HSB.Set_nInc(HSB.Get_nMax() - HSB.Get_nPos()) ;
- break ;
-
- case SB_LINEUP :
- HSB.Set_nInc(-1) ;
- break ;
-
- case SB_LINEDOWN :
- HSB.Set_nInc(1) ;
- break ;
-
- case SB_PAGEUP :
- HSB.Set_nInc(min(-1,-cxClient/FontSize.cxChar)) ;
- break ;
-
- case SB_PAGEDOWN :
- HSB.Set_nInc(max(1,cxClient/FontSize.cxChar)) ;
- break ;
-
- case SB_THUMBTRACK :
- HSB.Set_nInc(LOWORD(lParam)-HSB.Get_nPos()) ;
- break ;
-
- default :
- HSB.Set_nInc(0) ;
- break ;
- }
-
- HSB.Set_nInc(min(HSB.Get_nInc() ,
- HSB.Get_nMax()-HSB.Get_nPos()) ) ;
- HSB.Set_nInc(max(HSB.Get_nInc() ,
- -HSB.Get_nPos()) ) ;
-
- if (HSB.Get_nInc())
- { HSB.Set_nPos(HSB.Get_nPos() + HSB.Get_nInc() ) ;
- HSB.ScrollWin(-FontSize.cxChar*HSB.Get_nInc(), 0) ;
- HSB.SetPos(TRUE) ;
- Update() ;
- }
- break ;
-
-
- case WM_PAINT :
- hdc = BeginPaint(hwnd, &ps) ;
-
- nBeginLine = max(0,VSB.Get_nPos() + ps.rcPaint.top /
- FontSize.cyChar - 1) ;
- nEndLine = min(NUMLINE,VSB.Get_nPos() + ps.rcPaint.bottom/
- FontSize.cyChar) ;
-
- for (i=nBeginLine ; i<nEndLine ; i++)
- { x = FontSize.cxChar * (1-HSB.Get_nPos()) ;
- y = FontSize.cyChar * (1-VSB.Get_nPos()+i) ;
-
- TextOut(hdc, x, y,
- WinStruct[i].szStruct,
- lstrlen(WinStruct[i].szStruct)) ;
-
- TextOut(hdc, x+20*FontSize.cxCaps, y,
- WinStruct[i].szDescriptor,
- lstrlen(WinStruct[i].szDescriptor)) ;
- }
-
- EndPaint(hwnd, &ps) ;
- break ;
-
-
- case WM_DESTROY :
- PostQuitMessage(0) ;
- break ;
-
-
- default :
- return DefWindowProc(hwnd, message, wParam, lParam) ;
- }
-
- return 0L ;
- }
-
-
- #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->CreateProc(hwnd, wParam, lParam);
- }
- else return DefWindowProc(hwnd, message, wParam, lParam);
- }
- else return pWindow->AppWndProc(message, wParam, lParam);
- }
-
-
- #pragma argsused
-
- #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() ;
- }
-