home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpwinst / docdemos.pak / STEP07.PAS < prev    next >
Pascal/Delphi Source File  |  1991-05-21  |  6KB  |  231 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. const
  16.   cm_New    = 101;
  17.   cm_Open   = 102;
  18.   cm_Save   = 103;
  19.   cm_SaveAs = 104;
  20.   cm_Help   = 901;
  21.  
  22. type
  23.   TMyApplication = object(TApplication)
  24.     procedure InitMainWindow; virtual;
  25.   end;
  26.  
  27. type
  28.   PMyWindow = ^TMyWindow;
  29.   TMyWindow = object(TWindow)
  30.     DragDC: HDC;
  31.     ButtonDown: Boolean;
  32.     ThePen: HPen;
  33.     PenSize: Integer;
  34.     Points: PCollection;
  35.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  36.     destructor Done; virtual;
  37.     function CanClose: Boolean; virtual;
  38.     procedure WMLButtonDown(var Msg: TMessage);
  39.       virtual wm_First + wm_LButtonDown;
  40.     procedure WMLButtonUp(var Msg: TMessage);
  41.       virtual wm_First + wm_LButtonUp;
  42.     procedure WMMouseMove(var Msg: TMessage);
  43.       virtual wm_First + wm_MouseMove;
  44.     procedure WMRButtonDown(var Msg: TMessage);
  45.       virtual wm_First + wm_RButtonDown;
  46.     procedure SetPenSize(NewSize: Integer);
  47.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  48.     procedure FileNew(var Msg: TMessage);
  49.       virtual cm_First + cm_New;
  50.     procedure FileOpen(var Msg: TMessage);
  51.       virtual cm_First + cm_Open;
  52.     procedure FileSave(var Msg: TMessage);
  53.       virtual cm_First + cm_Save;
  54.     procedure FileSaveAs(var Msg: TMessage);
  55.       virtual cm_First + cm_SaveAs;
  56.     procedure Help(var Msg: TMessage);
  57.       virtual cm_First + cm_Help;
  58.   end;
  59.  
  60. type
  61.   PDPoint = ^TDPoint;
  62.   TDPoint = object(TObject)
  63.     X, Y: Integer;
  64.     constructor Init(AX, AY: Integer);
  65.   end;
  66.  
  67. {--------------------------------------------------}
  68. { TMyWindow's method implementations:              }
  69. {--------------------------------------------------}
  70.  
  71. constructor TMyWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  72. begin
  73.   TWindow.Init(AParent, ATitle);
  74.   Attr.Menu := LoadMenu(HInstance, PChar(100));
  75.   ButtonDown := False;
  76.   PenSize := 1;
  77.   ThePen := CreatePen(ps_Solid, PenSize, 0);
  78.   Points := New(PCollection, Init(50, 50));
  79. end;
  80.  
  81. destructor TMyWindow.Done;
  82. begin
  83.   Dispose(Points, Done);
  84.   DeleteObject(ThePen);
  85.   TWindow.Done;
  86. end;
  87.  
  88. function TMyWindow.CanClose: Boolean;
  89. var
  90.   Reply: Integer;
  91. begin
  92.   CanClose := True;
  93.   Reply := MessageBox(HWindow, 'Do you want to save?',
  94.     'Drawing has changed', mb_YesNo or mb_IconQuestion);
  95.   if Reply = id_Yes then CanClose := False;
  96. end;
  97.  
  98. procedure TMyWindow.WMLButtonDown(var Msg: TMessage);
  99. begin
  100.   Points^.FreeAll;
  101.   InvalidateRect(HWindow, nil, True);
  102.   if not ButtonDown then
  103.   begin
  104.     ButtonDown := True;
  105.     SetCapture(HWindow);
  106.     DragDC := GetDC(HWindow);
  107.     SelectObject(DragDC, ThePen);
  108.     MoveTo(DragDC, Msg.LParamLo, Msg.LParamHi);
  109.     Points^.Insert(New(PDPoint, Init(Msg.LParamLo, Msg.LParamHi)));
  110.   end;
  111. end;
  112.  
  113. procedure TMyWindow.WMMouseMove(var Msg: TMessage);
  114. begin
  115.   if ButtonDown then
  116.   begin
  117.     LineTo(DragDC, Integer(Msg.LParamLo), Integer(Msg.LParamHi));
  118.     Points^.Insert(New(PDPoint, Init(Integer(Msg.LParamLo), Integer(Msg.LParamHi))));
  119.   end;
  120. end;
  121.  
  122. procedure TMyWindow.WMLButtonUp(var Msg: TMessage);
  123. begin
  124.   if ButtonDown then
  125.   begin
  126.     ButtonDown := False;
  127.     ReleaseCapture;
  128.     ReleaseDC(HWindow, DragDC);
  129.   end;
  130. end;
  131.  
  132. procedure TMyWindow.WMRButtonDown(var Msg: TMessage);
  133. var
  134.   InputText: array[0..5] of Char;
  135.   NewSize, ErrorPos: Integer;
  136. begin
  137.   if not ButtonDown then
  138.   begin
  139.     Str(PenSize, InputText);
  140.     if Application^.ExecDialog(New(PInputDialog,
  141.       Init(@Self, 'Line Thickness', 'Input a new thickness:',
  142.         InputText, SizeOf(InputText)))) = id_Ok then
  143.     begin
  144.       Val(InputText, NewSize, ErrorPos);
  145.       if ErrorPos = 0 then SetPenSize(NewSize);
  146.     end;
  147.   end;
  148. end;
  149.  
  150. procedure TMyWindow.SetPenSize(NewSize: Integer);
  151. begin
  152.   DeleteObject(ThePen);
  153.   ThePen := CreatePen(ps_Solid, NewSize, 0);
  154.   PenSize := NewSize;
  155. end;
  156.  
  157. procedure TMyWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  158. var
  159.   First: Boolean;
  160.  
  161. procedure DrawLine(P: PDPoint); far;
  162. begin
  163.   if First then MoveTo(PaintDC, P^.X, P^.Y)
  164.   else LineTo(PaintDC, P^.X, P^.Y);
  165.   First := False;
  166. end;
  167.  
  168. begin
  169.   SelectObject(PaintDC, ThePen);
  170.   First := True;
  171.   Points^.ForEach(@DrawLine);
  172. end;
  173.  
  174. procedure TMyWindow.FileNew(var Msg: TMessage);
  175. begin
  176.   Points^.FreeAll;
  177.   InvalidateRect(HWindow, nil, True);
  178. end;
  179.  
  180. procedure TMyWindow.FileOpen(var Msg: TMessage);
  181. begin
  182.   MessageBox(HWindow, 'Feature not implemented', 'FileOpen', mb_Ok);
  183. end;
  184.  
  185. procedure TMyWindow.FileSave(var Msg: TMessage);
  186. begin
  187.   MessageBox(HWindow, 'Feature not implemented', 'FileSave', mb_Ok);
  188. end;
  189.  
  190. procedure TMyWindow.FileSaveAs(var Msg: TMessage);
  191. begin
  192.   MessageBox(HWindow, 'Feature not implemented', 'FileSaveAs', mb_Ok);
  193. end;
  194.  
  195. procedure TMyWindow.Help(var Msg: TMessage);
  196. begin
  197.   MessageBox(HWindow, 'Feature not implemented', 'Help', mb_Ok);
  198. end;
  199.  
  200. {--------------------------------------------------}
  201. { TDPoints's method implementations:               }
  202. {--------------------------------------------------}
  203.  
  204. constructor TDPoint.Init(AX, AY: Integer);
  205. begin
  206.   X := AX;
  207.   Y := AY;
  208. end;
  209.  
  210. {--------------------------------------------------}
  211. { TMyApplication's method implementations:         }
  212. {--------------------------------------------------}
  213.  
  214. procedure TMyApplication.InitMainWindow;
  215. begin
  216.   MainWindow := New(PMyWindow, Init(nil, 'Sample ObjectWindows Program'));
  217. end;
  218.  
  219. {--------------------------------------------------}
  220. { Main program:                                    }
  221. {--------------------------------------------------}
  222.  
  223. var
  224.   MyApp : TMyApplication;
  225.  
  226. begin
  227.   MyApp.Init('MyProgram');
  228.   MyApp.Run;
  229.   MyApp.Done;
  230. end.
  231.