home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpw / docdemos / step06.pas < prev    next >
Pascal/Delphi Source File  |  1991-05-20  |  5KB  |  187 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo program                                 }
  5. {   Copyright (c) 1991 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. program MyProgram;
  10.  
  11. uses Strings, WinTypes, WinProcs, WObjects, StdDlgs;
  12.  
  13. {$R COOKBOOK.RES}
  14.  
  15. type
  16.   TMyApplication = object(TApplication)
  17.     procedure InitMainWindow; virtual;
  18.   end;
  19.  
  20. type
  21.   PMyWindow = ^TMyWindow;
  22.   TMyWindow = object(TWindow)
  23.     DragDC: HDC;
  24.     ButtonDown: Boolean;
  25.     ThePen: HPen;
  26.     PenSize: Integer;
  27.     Points: PCollection;
  28.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  29.     destructor Done; virtual;
  30.     function CanClose: Boolean; virtual;
  31.     procedure WMLButtonDown(var Msg: TMessage);
  32.       virtual wm_First + wm_LButtonDown;
  33.     procedure WMLButtonUp(var Msg: TMessage);
  34.       virtual wm_First + wm_LButtonUp;
  35.     procedure WMMouseMove(var Msg: TMessage);
  36.       virtual wm_First + wm_MouseMove;
  37.     procedure WMRButtonDown(var Msg: TMessage);
  38.       virtual wm_First + wm_RButtonDown;
  39.     procedure SetPenSize(NewSize: Integer);
  40.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  41.   end;
  42.  
  43. type
  44.   PDPoint = ^TDPoint;
  45.   TDPoint = object(TObject)
  46.     X, Y: Integer;
  47.     constructor Init(AX, AY: Integer);
  48.   end;
  49.  
  50. {--------------------------------------------------}
  51. { TMyWindow's method implementations:              }
  52. {--------------------------------------------------}
  53.  
  54. constructor TMyWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  55. begin
  56.   TWindow.Init(AParent, ATitle);
  57.   ButtonDown := False;
  58.   PenSize := 1;
  59.   ThePen := CreatePen(ps_Solid, PenSize, 0);
  60.   Points := New(PCollection, Init(50, 50));
  61. end;
  62.  
  63. destructor TMyWindow.Done;
  64. begin
  65.   Dispose(Points, Done);
  66.   DeleteObject(ThePen);
  67.   TWindow.Done;
  68. end;
  69.  
  70. function TMyWindow.CanClose: Boolean;
  71. var
  72.   Reply: Integer;
  73. begin
  74.   CanClose := True;
  75.   Reply := MessageBox(HWindow, 'Do you want to save?',
  76.     'Drawing has changed', mb_YesNo or mb_IconQuestion);
  77.   if Reply = id_Yes then CanClose := False;
  78. end;
  79.  
  80. procedure TMyWindow.WMLButtonDown(var Msg: TMessage);
  81. begin
  82.   Points^.FreeAll;
  83.   InvalidateRect(HWindow, nil, True);
  84.   if not ButtonDown then
  85.   begin
  86.     ButtonDown := True;
  87.     SetCapture(HWindow);
  88.     DragDC := GetDC(HWindow);
  89.     SelectObject(DragDC, ThePen);
  90.     MoveTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  91.     Points^.Insert(New(PDPoint, Init(Msg.LParamLo, Msg.LParamHi)));
  92.   end;
  93. end;
  94.  
  95. procedure TMyWindow.WMMouseMove(var Msg: TMessage);
  96. begin
  97.   if ButtonDown then
  98.   begin
  99.     LineTo(DragDC, Integer(Msg.LParamLo), Integer(Msg.LParamHi));
  100.     Points^.Insert(New(PDPoint, Init(Integer(Msg.LParamLo), Integer(Msg.LParamHi))));
  101.   end;
  102. end;
  103.  
  104. procedure TMyWindow.WMLButtonUp(var Msg: TMessage);
  105. begin
  106.   if ButtonDown then
  107.   begin
  108.     ButtonDown := False;
  109.     ReleaseCapture;
  110.     ReleaseDC(HWindow, DragDC);
  111.   end;
  112. end;
  113.  
  114. procedure TMyWindow.WMRButtonDown(var Msg: TMessage);
  115. var
  116.   InputText: array[0..5] of Char;
  117.   NewSize, ErrorPos: Integer;
  118. begin
  119.   if not ButtonDown then
  120.   begin
  121.     Str(PenSize, InputText);
  122.     if Application^.ExecDialog(New(PInputDialog,
  123.       Init(@Self, 'Line Thickness', 'Input a new thickness:',
  124.         InputText, SizeOf(InputText)))) = id_Ok then
  125.     begin
  126.       Val(InputText, NewSize, ErrorPos);
  127.       if ErrorPos = 0 then SetPenSize(NewSize);
  128.     end;
  129.   end;
  130. end;
  131.  
  132. procedure TMyWindow.SetPenSize(NewSize: Integer);
  133. begin
  134.   DeleteObject(ThePen);
  135.   ThePen := CreatePen(ps_Solid, NewSize, 0);
  136.   PenSize := NewSize;
  137. end;
  138.  
  139. procedure TMyWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  140. var
  141.   First: Boolean;
  142.  
  143. procedure DrawLine(P: PDPoint); far;
  144. begin
  145.   if First then MoveTo(PaintDC, P^.X, P^.Y)
  146.   else LineTo(PaintDC, P^.X, P^.Y);
  147.   First := False;
  148. end;
  149.  
  150. begin
  151.   SelectObject(PaintDC, ThePen);
  152.   First := True;
  153.   Points^.ForEach(@DrawLine);
  154. end;
  155.  
  156. {--------------------------------------------------}
  157. { TDPoints's method implementations:               }
  158. {--------------------------------------------------}
  159.  
  160. constructor TDPoint.Init(AX, AY: Integer);
  161. begin
  162.   X := AX;
  163.   Y := AY;
  164. end;
  165.  
  166. {--------------------------------------------------}
  167. { TMyApplication's method implementations:         }
  168. {--------------------------------------------------}
  169.  
  170. procedure TMyApplication.InitMainWindow;
  171. begin
  172.   MainWindow := New(PMyWindow, Init(nil, 'Sample ObjectWindows Program'));
  173. end;
  174.  
  175. {--------------------------------------------------}
  176. { Main program:                                    }
  177. {--------------------------------------------------}
  178.  
  179. var
  180.   MyApp : TMyApplication;
  181.  
  182. begin
  183.   MyApp.Init('MyProgram');
  184.   MyApp.Run;
  185.   MyApp.Done;
  186. end.
  187.