home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 10.ddi / STEPS.ZIP / STEP6.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.5 KB  |  184 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <array.h>
  6. #include <abstarry.h>
  7. #include <owl.h>
  8. #include <inputdia.h>
  9.  
  10. class TMyApp : public TApplication
  11. {
  12. public:
  13.   TMyApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.     LPSTR lpCmdLine, int nCmdShow)
  15.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  16.   virtual void InitMainWindow();
  17. };
  18.  
  19. _CLASSDEF(TPoint)
  20. class TPoint: public Object
  21. {
  22. public:
  23.   int X, Y;
  24.   TPoint(int AX, int AY) {X = AX, Y = AY;}
  25.   virtual classType isA() const { return __firstUserClass; }
  26.   virtual Pchar nameOf() const { return "TPoint"; }
  27.   virtual hashValueType hashValue() const { return 0; }
  28.   virtual int isEqual(RCObject APoint) const
  29.     { return X == ((RTPoint)APoint).X && Y == ((RTPoint)APoint).Y; }
  30.   virtual void printOn(Rostream outputStream) const
  31.     { outputStream << "(" << X << "," << Y << ")"; }
  32. };
  33.  
  34. _CLASSDEF(TPointArray)
  35. class TPointArray : public Array
  36. {
  37. public:
  38.   TPointArray(int upper, int lower = 0, sizeType aDelta = 0)
  39.     : Array(upper, lower, aDelta){};
  40.   virtual classType isA() const { return __firstUserClass + 1; }
  41.   virtual Pchar nameOf() const { return "TPointArray"; }
  42. };
  43.  
  44. _CLASSDEF(TMyWindow)
  45. class TMyWindow : public TWindow
  46. {
  47. public:
  48.   HDC DragDC;
  49.   BOOL ButtonDown;
  50.   HPEN ThePen;
  51.   int PenSize;
  52.   PTPointArray Points;
  53.   TMyWindow(PTWindowsObject AParent, LPSTR ATitle);
  54.   ~TMyWindow();
  55.   virtual BOOL CanClose();
  56.   void SetPenSize(int NewSize);
  57.   virtual void Paint(HDC DC, PAINTSTRUCT& PS);
  58.   virtual void WMLButtonDown(RTMessage Msg)
  59.     = [WM_FIRST + WM_LBUTTONDOWN];
  60.   virtual void WMLButtonUp(RTMessage Msg)
  61.     = [WM_FIRST + WM_LBUTTONUP];
  62.   virtual void WMMouseMove(RTMessage Msg)
  63.     = [WM_FIRST + WM_MOUSEMOVE];
  64.   virtual void WMRButtonDown(RTMessage Msg)
  65.     = [WM_FIRST + WM_RBUTTONDOWN];
  66. };
  67.  
  68. TMyWindow::TMyWindow(PTWindowsObject AParent, LPSTR ATitle)
  69.   : TWindow(AParent, ATitle)
  70. {
  71.   ButtonDown = FALSE;
  72.   PenSize = 1;
  73.   ThePen = CreatePen(PS_SOLID, PenSize, 0);
  74.   Points = new TPointArray(50, 0, 50);
  75. }
  76.  
  77. TMyWindow::~TMyWindow()
  78. {
  79.   delete Points;
  80.   DeleteObject(ThePen);
  81. }
  82.  
  83. BOOL TMyWindow::CanClose()
  84. {
  85.   return MessageBox(HWindow, "Do you want to save?",
  86.     "Drawing has changed", MB_YESNO | MB_ICONQUESTION) == IDNO;
  87. }
  88.  
  89. void TMyWindow::WMLButtonDown(RTMessage Msg)
  90. {
  91.   Points->flush();
  92.   InvalidateRect(HWindow, NULL, TRUE);
  93.   if ( !ButtonDown )
  94.   {
  95.     ButtonDown = TRUE;
  96.     SetCapture(HWindow);
  97.     DragDC = GetDC(HWindow);
  98.     SelectObject(DragDC, ThePen);
  99.     MoveTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  100.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  101.   }
  102. }
  103.  
  104. void TMyWindow::WMMouseMove(RTMessage Msg)
  105. {
  106.   if ( ButtonDown )
  107.   {
  108.     LineTo(DragDC, Msg.LP.Lo, Msg.LP.Hi);
  109.     Points->add(* (new TPoint(Msg.LP.Lo, Msg.LP.Hi)));
  110.   }
  111. }
  112.  
  113. void TMyWindow::WMLButtonUp(RTMessage)
  114. {
  115.   if ( ButtonDown )
  116.   {
  117.     ButtonDown = FALSE;
  118.     ReleaseCapture();
  119.     ReleaseDC(HWindow, DragDC);
  120.   }
  121. }
  122.  
  123. void TMyWindow::WMRButtonDown(RTMessage)
  124. {
  125.   char InputText[6];
  126.   int NewPenSize;
  127.  
  128.   sprintf(InputText, "%d", PenSize);
  129.   if ( GetApplication()->ExecDialog(new TInputDialog(this, "Line Thickness",
  130.     "Input a new thickness:", InputText, sizeof InputText)) == IDOK )
  131.   {
  132.       NewPenSize = atoi(InputText);
  133.       if ( NewPenSize < 0 )
  134.         NewPenSize = 1;
  135.       SetPenSize(NewPenSize);
  136.   }
  137. }
  138.  
  139. void TMyWindow::SetPenSize(int NewSize)
  140. {
  141.   DeleteObject(ThePen);
  142.   ThePen = CreatePen(PS_SOLID, NewSize, 0);
  143.   PenSize = NewSize;
  144. }
  145.  
  146. void TMyWindow::Paint(HDC DC, PAINTSTRUCT&)
  147. {
  148.   RArrayIterator PointIterator = (RArrayIterator)(Points->initIterator());
  149.   BOOL First = TRUE;
  150.  
  151.   SelectObject(DC, ThePen);
  152.   while ( int(PointIterator) != 0 )
  153.   {
  154.     RObject AnObject = PointIterator++;
  155.     if ( AnObject != NOOBJECT )
  156.     {
  157.       if ( First )
  158.       {
  159.         MoveTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  160.         First = FALSE;
  161.       }
  162.       else
  163.         LineTo(DC, ((PTPoint)(&AnObject))->X, ((PTPoint)(&AnObject))->Y);
  164.     }
  165.   }
  166.   delete &PointIterator;
  167. }
  168.  
  169. void TMyApp::InitMainWindow()
  170. {
  171.   MainWindow = new TMyWindow(NULL, Name);
  172. }
  173.  
  174. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  175.   LPSTR lpCmdLine, int nCmdShow)
  176. {
  177.   TMyApp MyApp("Sample ObjectWindows Program", hInstance, hPrevInstance,
  178.                lpCmdLine, nCmdShow);
  179.   MyApp.Run();
  180.   return MyApp.Status;
  181. }
  182.  
  183.  
  184.