home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_prog / ddjwin.arj / CPP.ASC < prev    next >
Text File  |  1991-11-14  |  15KB  |  534 lines

  1. _WINDOWS MEETS C++_
  2. by Scott Robert Ladd
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7.  
  8. //  WINDOWS CLASSES
  9. //  winclass.h -- Class definitions for Windows programming
  10. //  Copyright 1991 by Scott Robert Ladd. All Rights Reserved.
  11.  
  12. #if !defined(__WINCLASS_H)
  13. #define __WINCLASS_H
  14.  
  15. #include "windows.h"
  16.  
  17. // macro resolving to size of this pointer
  18. #define SIZEOF_THIS (sizeof(void *))
  19. // macro resolving to number of extra bytes required by this window
  20. #define PTR_BYTES   (SIZEOF_THIS)
  21. // macros used to store and extract class pointer from extra window bytes
  22. #if sizeof(void *) == 4
  23.     #define SetWindowPtr(wdw,ptr) SetWindowLong((wdw),0,(DWORD)(ptr))
  24.     #define GetWindowPtr(wdw)     (Window *)GetWindowLong(wdw,0)
  25. #else
  26.     #define SetWindowPtr(wdw,ptr) SetWindowWord((wdw),0,(WORD)(ptr))
  27.     #define GetWindowPtr(wdw)     (Window *)GetWindowWord(wdw,0)
  28. #endif
  29. // callback function types
  30. typedef long (_far _pascal * WinCallBack)(HWND,WORD,WORD,DWORD);
  31. typedef int  (_far _pascal * DlgCallBack)(HWND,WORD,WORD,DWORD);
  32. //------------- CLASS WinApp -------------
  33. class WinApp
  34.     {
  35.     public:
  36.         static int Dispatcher();
  37.  
  38.         static void SetInstance(HANDLE inst);
  39.         static HANDLE GetInstance();
  40.     private:
  41.         static HANDLE Instance;
  42.     };
  43. // set instance handle
  44. inline void WinApp::SetInstance(HANDLE inst)
  45.     {
  46.     if (Instance == NULL)
  47.         Instance = inst;
  48.     }
  49. // get instance handle
  50. inline HANDLE WinApp::GetInstance()
  51.     {
  52.     return Instance;
  53.     }
  54. //------------------ CLASS WindowClass ------------------
  55. class WindowClass : public WinApp
  56.     {
  57.     public:
  58.         WindowClass();                // constructor
  59.  
  60.         BOOL Register();              // register
  61.         BOOL Unregister();            // unregister
  62.  
  63.         const char * GetName() const; // get class name string
  64.     protected:
  65.         WNDCLASS ClassData;
  66.     };
  67. //------------------ CLASS BasicWindow ------------------
  68. class BasicWindow : private WinApp
  69.     {
  70.     public:
  71.         HWND   GetHandle()   const;
  72.         HANDLE GetInstance() const;
  73.         DWORD  GetStyle()    const;
  74.         void Show(int cmdShow = SW_SHOW);
  75.         void Update();
  76.         void SetText(char * text);
  77.         void GetText(char * buffer, int n) const;
  78.         void Disable();
  79.         void Enable();
  80.         void GetExtents(int * x, int * y, int * width, int * height) const;
  81.         void Move(int x, int y, int width, int height);
  82.     protected:
  83.         BasicWindow(const char * cname);
  84.         HWND   Handle;
  85.         HANDLE Instance;
  86.         DWORD  Style;
  87.         char   ClassName[32];
  88.     };
  89. inline HWND BasicWindow::GetHandle() const
  90.     {
  91.     return Handle;
  92.     }
  93. inline HANDLE BasicWindow::GetInstance() const
  94.     {
  95.     return Instance;
  96.     }
  97. inline DWORD BasicWindow::GetStyle() const
  98.     {
  99.     return Style;
  100.     }
  101. inline void BasicWindow::Show(int cmdShow)
  102.     {
  103.     ShowWindow(Handle,cmdShow);
  104.     }
  105. inline void BasicWindow::Update()
  106.     {
  107.     UpdateWindow(Handle);
  108.     }
  109. inline void BasicWindow::SetText(char * text)
  110.     {
  111.     SetWindowText(Handle,text);
  112.     }
  113. inline void BasicWindow::GetText(char * buffer, int n) const
  114.     {
  115.     GetWindowText(Handle,buffer,n);
  116.     }
  117. inline void BasicWindow::Disable()
  118.     {
  119.     EnableWindow(Handle,FALSE);
  120.     }
  121. inline void BasicWindow::Enable()
  122.     {
  123.     EnableWindow(Handle,TRUE);
  124.     }
  125. inline void BasicWindow::GetExtents(int * x, int * y, int * width, 
  126.                                                            int * height) const
  127.     {
  128.     RECT r;
  129.     GetWindowRect(Handle,&r);
  130.     *x = r.left;
  131.     *y = r.top;
  132.     *width  = r.right  - r.left + 1;
  133.     *height = r.bottom - r.top  + 1;
  134.     }
  135. inline void BasicWindow::Move(int x, int y, int width, int height)
  136.     {
  137.     MoveWindow(Handle,x,y,width,height,TRUE);
  138.     }
  139. //------------- CLASS Window -------------
  140. class Window : public BasicWindow
  141.     {
  142.     public:
  143.         Window(const char * cname);
  144.  
  145.         BOOL Actualize(const char * title,
  146.                        int x      = CW_USEDEFAULT,
  147.                        int y      = CW_USEDEFAULT,
  148.                        int width  = CW_USEDEFAULT,
  149.                        int height = CW_USEDEFAULT);
  150.         HDC  OpenDC();
  151.         void CloseDC(HDC dc);
  152.     protected:
  153.         virtual void Create(HWND wdw, CREATESTRUCT _far * cs);
  154.         virtual void Close(HWND wdw);
  155.         virtual void Paint(HDC dc);
  156.         virtual void Command(HWND wdw, WORD wParam, DWORD lParam);
  157.  
  158.         virtual long AuxMsgHandler(HWND wdw,    WORD  message, 
  159.                                                    WORD wParam, DWORD lParam);
  160.     private:
  161.         static long _far _pascal _export MessageHandler(HWND  wdw,
  162.                                     WORD  message, WORD  wParam, DWORD lParam);
  163.     };
  164. inline HDC Window::OpenDC()
  165.     {
  166.     return GetDC(Handle);
  167.     }
  168. inline void Window::CloseDC(HDC dc)
  169.     {
  170.     ReleaseDC(Handle,dc);
  171.     }
  172. //-------------- CLASS Control --------------
  173. class Control : public BasicWindow
  174.     {
  175.     public:
  176.         BOOL Actualize(const Window & parent, const char * text, WORD id, 
  177.                                           int x, int y, int width, int height);
  178.         WORD GetID() const;
  179.         WinCallBack SubClass(WinCallBack newHandler);
  180.     protected:
  181.         Control(const char * cname);
  182.     };
  183. inline WORD Control::GetID() const
  184.     {
  185.     return GetWindowWord(Handle,GWW_ID);
  186.     }
  187. inline WinCallBack Control::SubClass(WinCallBack newHandler)
  188.     {
  189.     return (WinCallBack)SetWindowLong(Handle,GWL_WNDPROC,(DWORD)newHandler);
  190.     }
  191. inline Control::Control(const char * cname) : BasicWindow(cname)
  192.     {
  193.     // does nothing else
  194.     }
  195. //----------------- CLASS StaticLeft -----------------
  196. class StaticLeft : public Control
  197.     {
  198.     public:
  199.         StaticLeft();
  200.     };
  201. //----------------- CLASS PushButton -----------------
  202. class PushButton : public Control
  203.     {
  204.     public:
  205.         PushButton();
  206.     };
  207. #endif // __WINCLASS_H
  208.  
  209.  
  210. [LISTING TWO]
  211.  
  212. //  WINDOWS CLASSES
  213. //  winclass.cpp -- Windows class implementations.
  214. //  Copyright 1991 by Scott Robert Ladd. All Rights Reserved.
  215.  
  216. #include "winclass.h"
  217. #include "string.h"
  218.  
  219. //------------- CLASS WinApp -------------
  220.  
  221. // define values for static member of WinApp class
  222. HANDLE WinApp::Instance = NULL;
  223. // message dispatcher
  224. int WinApp::Dispatcher()
  225.     {
  226.     if (Instance == NULL)
  227.         return !0;
  228.     MSG msg;
  229.     while (GetMessage(&msg, NULL, NULL, NULL))
  230.         {
  231.         TranslateMessage(&msg);
  232.         DispatchMessage(&msg);
  233.         }
  234.  
  235.     return msg.wParam;
  236.     }
  237. //------------------ CLASS WindowClass ------------------
  238. WindowClass::WindowClass()
  239.     {
  240.     #if defined(__BCPLUSPLUS__)
  241.         ClassData.lpfnWndProc   = DefWindowProc;
  242.     #else
  243.         ClassData.lpfnWndProc   = (long (_far _pascal *)())DefWindowProc;
  244.     #endif
  245.     ClassData.hInstance     = WinApp::GetInstance();
  246.     ClassData.style         = 0;
  247.     ClassData.cbClsExtra    = 0;
  248.     ClassData.cbWndExtra    = PTR_BYTES;
  249.     ClassData.hIcon         = NULL;
  250.     ClassData.hCursor       = LoadCursor(NULL,IDC_ARROW);
  251.     ClassData.hbrBackground = GetStockObject(WHITE_BRUSH);
  252.     ClassData.lpszMenuName  = NULL;
  253.     ClassData.lpszClassName = "DefaultWindowClass";
  254.     }
  255. BOOL WindowClass::Register()
  256.     {
  257.     if (!RegisterClass((WNDCLASS _far *)&ClassData))
  258.         return FALSE;
  259.     else
  260.         return TRUE;
  261.     }
  262. BOOL WindowClass::Unregister()
  263.     {
  264.     if (!UnregisterClass(ClassData.lpszClassName,ClassData.hInstance))
  265.         return FALSE;
  266.     else
  267.         return TRUE;
  268.     }
  269. const char * WindowClass::GetName() const
  270.     {
  271.     return (const char *)ClassData.lpszClassName;
  272.     }
  273. //------------------ CLASS BasicWindow ------------------
  274. BasicWindow::BasicWindow(const char * cname)
  275.     {
  276.     Handle   = NULL;
  277.     Instance = WinApp::GetInstance();
  278.     Style    = 0UL;
  279.  
  280.     if (cname != NULL)
  281.         strncpy(ClassName,cname,32);
  282.     else
  283.         ClassName[0] = 0; // blank class name
  284.     }
  285. //------------- CLASS Window -------------
  286. Window::Window(const char * cname) : BasicWindow(cname)
  287.     {
  288.     Style = WS_OVERLAPPEDWINDOW;
  289.     }
  290. BOOL Window::Actualize(const char * title, int x, int y, int width, int height)
  291.     {
  292.     Handle = CreateWindow(ClassName, (char *)title, Style, x, y,
  293.                                     width, height, NULL, NULL, Instance, 0L);
  294.     if (Handle == NULL)
  295.         return FALSE;
  296.     FARPROC fp = MakeProcInstance((FARPROC)((DWORD)(Window::MessageHandler)),
  297.                                                                      Instance);
  298.     SetWindowLong(Handle,GWL_WNDPROC,(DWORD)fp);
  299.     SetWindowPtr(Handle,this);
  300.     SendMessage(Handle,WM_CREATE,0,0L);
  301.     return TRUE;
  302.     }
  303. void Window::Create(HWND wdw, CREATESTRUCT _far * cs)
  304.     {
  305.     // by default, does NOTHING!
  306.     }
  307. void Window::Close(HWND wdw)
  308.     {
  309.     // by default, does NOTHING!
  310.     }
  311. void Window::Paint(HDC dc)
  312.     {
  313.     // by default, does NOTHING!
  314.     }
  315. void Window::Command(HWND wdw, WORD wParam, DWORD lParam)
  316.     {
  317.     // by default, does NOTHING!
  318.     }
  319. long Window::AuxMsgHandler(HWND wdw, WORD message, WORD wParam, DWORD lParam)
  320.     {
  321.     return DefWindowProc(wdw,message,wParam,lParam);
  322.     }
  323. long _far _pascal _export Window::MessageHandler(HWND wdw, WORD  message,
  324.                                                  WORD wParam, DWORD lParam)
  325.     {
  326.     HDC           dc;
  327.     PAINTSTRUCT   ps;
  328.     Window *      wptr;
  329.     wptr = GetWindowPtr(wdw);
  330.     switch (message)
  331.         {
  332.         case WM_CREATE:
  333.             wptr->Create(wdw, (CREATESTRUCT _far *)lParam);
  334.             break;
  335.         case WM_CLOSE:
  336.             wptr->Close(wdw);
  337.             break;
  338.         case WM_PAINT:
  339.             dc = BeginPaint(wdw,&ps);
  340.             wptr->Paint(dc);
  341.             EndPaint(wdw,&ps);
  342.             break;
  343.         case WM_COMMAND:
  344.             wptr->Command(wdw,wParam,lParam);
  345.             break;
  346.         default:
  347.             return wptr->AuxMsgHandler(wdw,message,wParam,lParam);
  348.         }
  349.     return 0L;
  350.     }
  351. //-------------- CLASS Control --------------
  352. BOOL Control::Actualize(const Window & parent, const char * text, WORD id,
  353.                                            int x, int y, int width, int height)
  354.     {
  355.     Handle = CreateWindow(ClassName, (char *)text, Style, x,y,width,height,
  356.                              parent.GetHandle(), id, parent.GetInstance(), 0L);
  357.     if (Handle == NULL)
  358.         return FALSE;
  359.     return TRUE;
  360.     }
  361. //-----------------  CLASS StaticLeft -----------------
  362. StaticLeft::StaticLeft() : Control("STATIC")
  363.     {
  364.     Style = WS_CHILD | SS_LEFT | SS_NOPREFIX | WS_BORDER;
  365.     }
  366. //----------------- CLASS PushButton -----------------
  367. PushButton::PushButton() : Control("BUTTON")
  368.     {
  369.     Style = WS_CHILD | BS_PUSHBUTTON;
  370.     }
  371.  
  372.  
  373.  
  374. [LISTING THREE]
  375.  
  376. //  WINDOWS CLASSES
  377. //  woop.cpp -- A program to test basic windows classes.
  378. //  Copyright 1991 by Scott Robert Ladd. All Rights Reserved.
  379.  
  380. #include "winclass.h"
  381. #include "woop.h"
  382.  
  383. #define IDC_BUTTON 1000
  384.  
  385. //---------------------- CLASS MainWindowClass ----------------------
  386. class WOOPWindowClass : public WindowClass
  387.     {
  388.     public:
  389.         WOOPWindowClass();
  390.     };
  391. WOOPWindowClass::WOOPWindowClass() : WindowClass()
  392.     {
  393.     ClassData.hIcon         = LoadIcon(WinApp::GetInstance(),"WOOPIcon");
  394.     ClassData.hbrBackground = GetStockObject(LTGRAY_BRUSH);
  395.     ClassData.lpszMenuName  = "WOOPMenu";
  396.     ClassData.lpszClassName = "MainWindowClass";
  397.     }
  398. //----------------- CLASS WOOPWindow -----------------
  399. class WOOPWindow : public Window
  400.     {
  401.     public:
  402.         WOOPWindow(const char * cname);
  403.  
  404.         virtual BOOL Actualize();
  405.     protected:
  406.         virtual void Close(HWND wdw);
  407.         virtual void Command(HWND wdw, WORD wParam, DWORD lParam);
  408.         StaticLeft SCtl;
  409.         PushButton BCtl;
  410.     };
  411. WOOPWindow::WOOPWindow(const char * cname) : Window(cname)
  412.     {
  413.     }
  414. BOOL WOOPWindow::Actualize()
  415.     {
  416.     BOOL res;
  417.     res = Window::Actualize("WOOP Window"); // call base class member
  418.     if (res == FALSE)
  419.         return FALSE;
  420.     res = SCtl.Actualize(*this,"Static control",0,100,10,120,16);
  421.     if (res == FALSE)
  422.         return FALSE;
  423.     SCtl.Show();
  424.     SCtl.Update();
  425.     res = BCtl.Actualize(*this,"Button",IDC_BUTTON,100,50,60,40);
  426.     if (res == FALSE)
  427.         return FALSE;
  428.     BCtl.Show();
  429.     BCtl.Update();
  430.     return TRUE;
  431.     }
  432. void WOOPWindow::Close(HWND wdw)
  433.     {
  434.     PostQuitMessage(0);
  435.     }
  436. void WOOPWindow::Command(HWND wdw, WORD wParam, DWORD lParam)
  437.     {
  438.     switch (wParam)
  439.         {
  440.         case IDM_EXIT:
  441.             PostQuitMessage(0);
  442.             break;
  443.         case IDM_ABOUT:
  444.             MessageBox(GetFocus(),"WOOP version 1.00","About...",MB_OK);
  445.             break;
  446.         case IDM_SET:
  447.             SCtl.SetText("Set!");
  448.             break;
  449.         case IDM_RESET:
  450.             SCtl.SetText("Reset!");
  451.             break;
  452.         case IDC_BUTTON:
  453.             MessageBeep(0);
  454.             MessageBeep(0);
  455.         }
  456.     }
  457. //----------------- FUNCTION WinMain -----------------
  458. int _pascal WinMain(HANDLE instance,HANDLE prevInst,LPSTR cmdLine,int cmdShow)
  459.     {
  460.     WinApp::SetInstance(instance);
  461.     WOOPWindowClass wc;
  462.     if (prevInst == NULL)
  463.         wc.Register();
  464.     WOOPWindow wdw(wc.GetName());
  465.  
  466.     wdw.Actualize();
  467.     wdw.Show(cmdShow);
  468.     wdw.Update();
  469.  
  470.     return WinApp::Dispatcher();
  471.     };
  472.  
  473.  
  474.  
  475. [LISTING FOUR]
  476.  
  477. #include "windows.h"
  478. #include "woop.h"
  479.  
  480. WOOPIcon ICON WOOP.ICO
  481.  
  482. WOOPMenu MENU
  483.     BEGIN
  484.         POPUP "&Menu"
  485.             BEGIN
  486.             MENUITEM "Set",       IDM_SET
  487.             MENUITEM "Reset",     IDM_RESET
  488.             MENUITEM SEPARATOR
  489.             MENUITEM "&About...", IDM_ABOUT
  490.             MENUITEM SEPARATOR
  491.             MENUITEM "E&xit",     IDM_EXIT
  492.             END
  493.     END
  494.  
  495.  
  496.  
  497. [LISTING FIVE]
  498.  
  499. #define IDM_SET     100
  500. #define IDM_RESET   101
  501. #define IDM_ABOUT   102
  502. #define IDM_EXIT    103
  503.  
  504.  
  505.  
  506. [LISTING SIX]
  507.  
  508. NAME        OOPAPP
  509.  
  510. DESCRIPTION 'OPP Windows App'
  511.  
  512. EXETYPE     WINDOWS
  513. STUB        'WINSTUB.EXE'
  514. CODE        PRELOAD MOVEABLE DISCARDABLE
  515. DATA        PRELOAD MOVEABLE MULTIPLE
  516.  
  517. HEAPSIZE    4096
  518. STACKSIZE   4096
  519.  
  520.  
  521.  
  522. [MAKE FILE]
  523.  
  524. windows = y
  525.  
  526. woop.exe     : woop.obj winclass.obj woop.res woop.def
  527.  
  528. woop.obj     : woop.cpp winclass.h woop.h
  529.  
  530. winclass.obj : winclass.cpp winclass.h
  531.  
  532. woop.res     : woop.rc woop.h woop.ico
  533.  
  534.