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