home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue150 / delphi / MoveAlign2 / MvAlUnit2.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1998-12-26  |  6.1 KB  |  224 lines

  1. unit MvAlUnit2;
  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.     procedure FormCreate(Sender: TObject);
  22.     procedure AButtonMouseDown(Sender: TObject; Button: TMouseButton;
  23.       Shift: TShiftState; X, Y: Integer);
  24.     procedure AButtonMouseMove(Sender: TObject; Shift: TShiftState; X,
  25.       Y: Integer);
  26.     procedure AButtonClick(Sender: TObject);
  27.     procedure AlignLeft1Click(Sender: TObject);
  28.     procedure AlignTop1Click(Sender: TObject);
  29.     procedure AlignRight1Click(Sender: TObject);
  30.     procedure AlignBottom1Click(Sender: TObject);
  31.     procedure MakeSameSize1Click(Sender: TObject);
  32.   private
  33.     { Private declarations }
  34.   public
  35.     { Public declarations }
  36.     Xdiff  : integer;
  37.     Ydiff  : integer;
  38.     XRtMargin : integer;
  39.     YBtmMargin : integer;
  40.     TriggerBtn : TButton;
  41.     procedure MoveBtn( btn : TButton; X, Y : integer );
  42.     procedure SizeBtn( btn : TButton; X, Y : integer );
  43.   end;
  44.  
  45. const
  46.   ResizeZone = 10;{Allow resizing if pointer is 10 pixels from rt button corner}
  47.   MinW = 70;      { Minimum possible Width and Height of button                }
  48.   MinH = 20;
  49.   TooSmall = 2;   { Stop resizing action if W or H is less than this           }
  50.   
  51.     { Define Cursors }
  52.   NormalCursor = crDefault;
  53.   MoveCursor = crDrag;
  54.   SizeCursor = crSizeNWSE;
  55.  
  56. type
  57.     { Used to store the currently active Mouse Mode or action                  }
  58.   MouseMode =(Moving,Sizing,LeftClicked,Nothing);
  59.  
  60. var
  61.   Form1: TForm1;
  62.   MsMode : MouseMode;
  63.  
  64. implementation
  65.  
  66. {$R *.DFM}
  67.  
  68.  
  69.  
  70. procedure TForm1.FormCreate(Sender: TObject);
  71. begin
  72.   MsMode := Nothing;
  73.   TriggerBtn := Button1;
  74. end;
  75.  
  76. procedure TForm1.AButtonMouseDown(Sender: TObject; Button: TMouseButton;
  77.   Shift: TShiftState; X, Y: Integer);
  78.   { When mouse button is pressed take action as appropriate }
  79. var
  80.    btn : TButton;
  81. begin
  82.   btn := TButton(Sender);
  83.    { If it was a Left-Button click }
  84.   if Button = mbLeft then
  85.   begin
  86.     { If the click occured in bottom right corner, set Mode to Sizing }
  87.      if ( X >= btn.Width-ResizeZone) and (Y >= btn.Height-ResizeZone) then
  88.      begin
  89.         MsMode := Sizing;
  90.         Screen.Cursor := SizeCursor;
  91.      end
  92.      else  { else set Mode to LeftClicked }
  93.         MsMode := LeftClicked;
  94.            { Save X and Y coordinates at time of button click }
  95.      Xdiff := X;
  96.      Ydiff := Y;
  97.            { Also save the size of the Right and Bottom margins from mouse     }
  98.            { pointer to the edge of the button                                 }
  99.      XRtMargin := btn.Width - X;
  100.      YBtmMargin := btn.Height - Y;
  101.   end
  102.   else     { else it was a Right-Click. So remember which button was clicked!  }
  103.       TriggerBtn := TButton(Sender);
  104. end;
  105.  
  106. procedure TForm1.MoveBtn( btn : TButton; X, Y : integer  );
  107. begin
  108.    btn.Top := btn.Top + Y  - Ydiff;
  109.    btn.Left := btn.Left + X - Xdiff;
  110.    Screen.Cursor := MoveCursor;
  111. end;
  112.  
  113.  
  114. procedure TForm1.SizeBtn( btn : TButton; X, Y : integer  );
  115. begin
  116.    { if user tries to size beyond absolute minimum limits, stop Sizing     }
  117.    if (X < TooSmall ) or (Y < TooSmall ) then
  118.    begin
  119.       MsMode := Nothing;
  120.       Screen.Cursor := NormalCursor;
  121.    end
  122.    else { otherwise size button as pointer moves                            }
  123.    begin
  124.      if X >= MinW then
  125.         btn.Width := X + XRtMargin;
  126.      if Y >= MinH then
  127.         btn.Height := Y + YBtmMargin;
  128.    end;
  129. end;
  130.  
  131. procedure TForm1.AButtonMouseMove(Sender: TObject; Shift: TShiftState; X,
  132.   Y: Integer);
  133. { When the mouse pointer moves over this button, take action according to
  134.   the current value of MsMode.
  135.  
  136.   Val of MsMode     Action to Take
  137.   * Sizing          * Resize button on mouse move
  138.   * LeftClicked     * Set mode to Moving
  139.   * Moving          * Move the button
  140.   }
  141. var
  142.   btn : TButton;
  143. begin
  144.   btn := TButton(Sender);
  145.   Caption := btn.name + ' X:' + IntToStr(X) + ' Y:' + IntToStr(Y);
  146.   { --- SIZING --- }
  147.   if MsMode = Sizing then
  148.      SizeBtn( btn, X, Y )
  149.   else
  150.   begin
  151.   { --- LEFTCLICKED --- }
  152.   if MsMode = LeftClicked then
  153.      MsMode := Moving;
  154.   { --- MOVING --- }
  155.   if (MsMode = Moving) then
  156.       MoveBtn( btn, X, Y );
  157.   end;
  158.   // if MsMode = Nothing then { do nothing } ;
  159. end;
  160.  
  161. procedure TForm1.AButtonClick(Sender: TObject);
  162. begin
  163.   if MsMode = LeftClicked then
  164.      ShowMessage( 'You clicked: ' + TButton(Sender).name );
  165.   MsMode := Nothing;
  166.   Screen.Cursor := NormalCursor;
  167. end;
  168.  
  169. procedure TForm1.AlignLeft1Click(Sender: TObject);
  170. var
  171.   i : integer;
  172. begin
  173.  for i := 0 to ComponentCount - 1 do
  174.     if Components[i] is TButton then
  175.        TButton(Components[i]).Left := TriggerBtn.Left;
  176. end;
  177.  
  178. procedure TForm1.AlignTop1Click(Sender: TObject);
  179. var
  180.   i : integer;
  181. begin
  182.  for i := 0 to ComponentCount - 1 do
  183.     if Components[i] is TButton then
  184.        TButton(Components[i]).Top := TriggerBtn.Top;
  185. end;
  186.  
  187. procedure TForm1.AlignRight1Click(Sender: TObject);
  188. var
  189.   i : integer;
  190.   RightEdge : integer;
  191. begin
  192.  RightEdge := (TriggerBtn.Left + TriggerBtn.Width);
  193.  for i := 0 to ComponentCount - 1 do
  194.     if Components[i] is TButton then
  195.        TButton(Components[i]).Left := RightEdge - TButton(Components[i]).Width;
  196. end;
  197.  
  198. procedure TForm1.AlignBottom1Click(Sender: TObject);
  199. var
  200.   i : integer;
  201.   BottomEdge : integer;
  202. begin
  203.  BottomEdge := (TriggerBtn.Top + TriggerBtn.Height);
  204.  for i := 0 to ComponentCount - 1 do
  205.     if Components[i] is TButton then
  206.        TButton(Components[i]).Top := BottomEdge - TButton(Components[i]).Height;
  207. end;
  208.  
  209.  
  210. procedure TForm1.MakeSameSize1Click(Sender: TObject);
  211. var
  212.   i : integer;
  213. begin
  214.  for i := 0 to ComponentCount - 1 do
  215.     if Components[i] is TButton then
  216.        with TButton(Components[i]) do
  217.        begin
  218.           Width := TriggerBtn.Width;
  219.           Height := TriggerBtn.Height;
  220.        end;
  221. end;
  222.  
  223. end.
  224.