home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue151 / delphi / SaveForm3 / movesave.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-01-18  |  7.9 KB  |  311 lines

  1. unit movesave;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, Menus;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     Button2: TButton;
  13.     Button3: TButton;
  14.     Button4: TButton;
  15.     PopupMenu1: TPopupMenu;
  16.     AlignLeft1: TMenuItem;
  17.     AlignTop1: TMenuItem;
  18.     AlignRight1: TMenuItem;
  19.     AlignBottom1: TMenuItem;
  20.     MakeSameSize1: TMenuItem;
  21.     GroupBox1: TGroupBox;
  22.     SaveBtn: TButton;
  23.     LoadBtn: TButton;
  24.     ShowCmpsBtn: TButton;
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure AButtonMouseDown(Sender: TObject; Button: TMouseButton;
  27.       Shift: TShiftState; X, Y: Integer);
  28.     procedure AButtonMouseMove(Sender: TObject; Shift: TShiftState; X,
  29.       Y: Integer);
  30.     procedure AButtonClick(Sender: TObject);
  31.     procedure AlignLeft1Click(Sender: TObject);
  32.     procedure AlignTop1Click(Sender: TObject);
  33.     procedure AlignRight1Click(Sender: TObject);
  34.     procedure AlignBottom1Click(Sender: TObject);
  35.     procedure MakeSameSize1Click(Sender: TObject);
  36.     procedure SaveBtnClick(Sender: TObject);
  37.     procedure LoadBtnClick(Sender: TObject);
  38.     procedure ShowCmpsBtnClick(Sender: TObject);
  39.   private
  40.     { Private declarations }
  41.   public
  42.     { Public declarations }
  43.     Xdiff  : integer;
  44.     Ydiff  : integer;
  45.     XRtMargin : integer;
  46.     YBtmMargin : integer;
  47.     TriggerBtn : TButton;
  48.     procedure MoveBtn( btn : TButton; X, Y : integer );
  49.     procedure SizeBtn( btn : TButton; X, Y : integer );
  50.     procedure ZapComponents;
  51.     function SaveForm( fname : string ) : boolean;
  52.     function LoadForm( fname : string ) : boolean;
  53.   end;
  54.  
  55. const
  56.   ResizeZone = 10;{Allow resizing if pointer is 10 pixels from rt button corner}
  57.   MinW = 70;      { Minimum possible Width and Height of button                }
  58.   MinH = 20;
  59.   TooSmall = 2;   { Stop resizing action if W or H is less than this           }
  60.  
  61.     { Define Cursors }
  62.   NormalCursor = crDefault;
  63.   MoveCursor = crDrag;
  64.   SizeCursor = crSizeNWSE;
  65.  
  66.   SAVEFILE = 'MyFile.dwf';
  67.  
  68. type
  69.     { Used to store the currently active Mouse Mode or action                  }
  70.   MouseMode =(Moving,Sizing,LeftClicked,Nothing);
  71.  
  72. var
  73.   Form1: TForm1;
  74.   MsMode : MouseMode;
  75.  
  76. implementation
  77.  
  78. {$R *.DFM}
  79.  
  80. { ===== }
  81.  
  82.  
  83. procedure TForm1.ZapComponents;
  84. var
  85.    i : integer;
  86. begin
  87.    for i := 0 to ComponentCount - 1 do
  88.    begin
  89.      if TComponent(Components[0]) is TControl then
  90.        TControl(Components[0]).Parent := nil;
  91.      Form1.RemoveComponent(Components[0]);
  92.    end;
  93. end;
  94.  
  95.  
  96. function TForm1.SaveForm( fname : string ) : boolean;
  97. var
  98.    fs : TFileStream;
  99. begin
  100.    result := true;
  101.    fs := TFileStream.Create( fname, fmCreate );
  102.    try
  103.      try
  104.        fs.WriteComponent( Form1 );
  105.      except
  106.        result := false;
  107.      end;
  108.    finally
  109.      fs.Free;
  110.    end;
  111. end;
  112.  
  113. function TForm1.LoadForm( fname : string ) : boolean;
  114. var
  115.    fs : TFileStream;
  116. begin
  117.    result := true;
  118.    fs := TFileStream.Create( fname, fmOpenRead );
  119.    ZapComponents;
  120.    try
  121.      try
  122.        fs.ReadComponent( Form1 );
  123.      except
  124.        result := false;
  125.      end;
  126.    finally
  127.      fs.Free;
  128.    end;
  129. end;
  130.  
  131. { ===== }
  132.  
  133. procedure TForm1.FormCreate(Sender: TObject);
  134. begin
  135.   MsMode := Nothing;
  136.   TriggerBtn := Button1;
  137. end;
  138.  
  139. procedure TForm1.AButtonMouseDown(Sender: TObject; Button: TMouseButton;
  140.   Shift: TShiftState; X, Y: Integer);
  141.   { When mouse button is pressed take action as appropriate }
  142. var
  143.    btn : TButton;
  144. begin
  145.   btn := TButton(Sender);
  146.    { If it was a Left-Button click }
  147.   if Button = mbLeft then
  148.   begin
  149.     { If the click occured in bottom right corner, set Mode to Sizing }
  150.      if ( X >= btn.Width-ResizeZone) and (Y >= btn.Height-ResizeZone) then
  151.      begin
  152.         MsMode := Sizing;
  153.         Screen.Cursor := SizeCursor;
  154.      end
  155.      else  { else set Mode to LeftClicked }
  156.         MsMode := LeftClicked;
  157.            { Save X and Y coordinates at time of button click }
  158.      Xdiff := X;
  159.      Ydiff := Y;
  160.            { Also save the size of the Right and Bottom margins from mouse     }
  161.            { pointer to the edge of the button                                 }
  162.      XRtMargin := btn.Width - X;
  163.      YBtmMargin := btn.Height - Y;
  164.   end
  165.   else     { else it was a Right-Click. So remember which button was clicked!  }
  166.       TriggerBtn := TButton(Sender);
  167. end;
  168.  
  169. procedure TForm1.MoveBtn( btn : TButton; X, Y : integer  );
  170. begin
  171.    btn.Top := btn.Top + Y  - Ydiff;
  172.    btn.Left := btn.Left + X - Xdiff;
  173.    Screen.Cursor := MoveCursor;
  174. end;
  175.  
  176.  
  177. procedure TForm1.SizeBtn( btn : TButton; X, Y : integer  );
  178. begin
  179.    { if user tries to size beyond absolute minimum limits, stop Sizing     }
  180.    if (X < TooSmall ) or (Y < TooSmall ) then
  181.    begin
  182.       MsMode := Nothing;
  183.       Screen.Cursor := NormalCursor;
  184.    end
  185.    else { otherwise size button as pointer moves                            }
  186.    begin
  187.      if X >= MinW then
  188.         btn.Width := X + XRtMargin;
  189.      if Y >= MinH then
  190.         btn.Height := Y + YBtmMargin;
  191.    end;
  192. end;
  193.  
  194. procedure TForm1.AButtonMouseMove(Sender: TObject; Shift: TShiftState; X,
  195.   Y: Integer);
  196. { When the mouse pointer moves over this button, take action according to
  197.   the current value of MsMode.
  198.  
  199.   Val of MsMode     Action to Take
  200.   * Sizing          * Resize button on mouse move
  201.   * LeftClicked     * Set mode to Moving
  202.   * Moving          * Move the button
  203.   }
  204. var
  205.   btn : TButton;
  206. begin
  207.   btn := TButton(Sender);
  208.   Caption := btn.name + ' X:' + IntToStr(X) + ' Y:' + IntToStr(Y);
  209.   { --- SIZING --- }
  210.   if MsMode = Sizing then
  211.      SizeBtn( btn, X, Y )
  212.   else
  213.   begin
  214.   { --- LEFTCLICKED --- }
  215.   if MsMode = LeftClicked then
  216.      MsMode := Moving;
  217.   { --- MOVING --- }
  218.   if (MsMode = Moving) then
  219.       MoveBtn( btn, X, Y );
  220.   end;
  221.   // if MsMode = Nothing then { do nothing } ;
  222. end;
  223.  
  224. procedure TForm1.AButtonClick(Sender: TObject);
  225. begin
  226.   if MsMode = LeftClicked then
  227.      ShowMessage( 'You clicked: ' + TButton(Sender).name );
  228.   MsMode := Nothing;
  229.   Screen.Cursor := NormalCursor;
  230. end;
  231.  
  232. procedure TForm1.AlignLeft1Click(Sender: TObject);
  233. var
  234.   i : integer;
  235. begin
  236.  for i := 0 to ComponentCount - 1 do
  237.     if Components[i] is TButton then
  238.        TButton(Components[i]).Left := TriggerBtn.Left;
  239. end;
  240.  
  241. procedure TForm1.AlignTop1Click(Sender: TObject);
  242. var
  243.   i : integer;
  244. begin
  245.  for i := 0 to ComponentCount - 1 do
  246.     if Components[i] is TButton then
  247.        TButton(Components[i]).Top := TriggerBtn.Top;
  248. end;
  249.  
  250. procedure TForm1.AlignRight1Click(Sender: TObject);
  251. var
  252.   i : integer;
  253.   RightEdge : integer;
  254. begin
  255.  RightEdge := (TriggerBtn.Left + TriggerBtn.Width);
  256.  for i := 0 to ComponentCount - 1 do
  257.     if Components[i] is TButton then
  258.        TButton(Components[i]).Left := RightEdge - TButton(Components[i]).Width;
  259. end;
  260.  
  261. procedure TForm1.AlignBottom1Click(Sender: TObject);
  262. var
  263.   i : integer;
  264.   BottomEdge : integer;
  265. begin
  266.  BottomEdge := (TriggerBtn.Top + TriggerBtn.Height);
  267.  for i := 0 to ComponentCount - 1 do
  268.     if Components[i] is TButton then
  269.        TButton(Components[i]).Top := BottomEdge - TButton(Components[i]).Height;
  270. end;
  271.  
  272.  
  273. procedure TForm1.MakeSameSize1Click(Sender: TObject);
  274. var
  275.   i : integer;
  276. begin
  277.  for i := 0 to ComponentCount - 1 do
  278.     if Components[i] is TButton then
  279.        with TButton(Components[i]) do
  280.        begin
  281.           Width := TriggerBtn.Width;
  282.           Height := TriggerBtn.Height;
  283.        end;
  284. end;
  285.  
  286. procedure TForm1.SaveBtnClick(Sender: TObject);
  287. begin
  288.    if not SaveForm(SAVEFILE) then
  289.       ShowMessage( 'Error: Can''t save ' +  SAVEFILE );
  290. end;
  291.  
  292. procedure TForm1.LoadBtnClick(Sender: TObject);
  293. begin
  294.    if not LoadForm(SAVEFILE) then
  295.       ShowMessage( 'Error: Can''t load ' +  SAVEFILE );
  296. end;
  297.  
  298. procedure TForm1.ShowCmpsBtnClick(Sender: TObject);
  299. var
  300.   i : integer;
  301.   s : string;
  302. begin
  303.   s := '';
  304.   for i := 0 to (ComponentCount-1) do
  305.     s := s + Format('[%d] %s, ', [i, Components[i].name]);
  306.     ShowMessage( s );
  307. end;
  308.  
  309.  
  310. end.
  311.