home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / TOOLBAR3 / MAIN.PAS < prev    next >
Pascal/Delphi Source File  |  1998-04-13  |  3KB  |  123 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Windows, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Buttons, ExtCtrls, StdCtrls;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     FloatingToolbar: TPanel;
  12.     SpeedButton1: TSpeedButton;
  13.     SpeedButton2: TSpeedButton;
  14.     SpeedButton3: TSpeedButton;
  15.     SpeedButton4: TSpeedButton;
  16.     SpeedButton5: TSpeedButton;
  17.     SpeedButton6: TSpeedButton;
  18.     SpeedButton7: TSpeedButton;
  19.     SpeedButton8: TSpeedButton;
  20.     BitBtn1: TBitBtn;
  21.     Label1: TLabel;
  22.     Bevel1: TBevel;
  23.     AllowDraggingCB: TCheckBox;
  24.     ResetBitBtn: TBitBtn;
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure SpeedButton1Click(Sender: TObject);
  27.     procedure FloatingToolbarMouseDown(Sender: TObject;
  28.       Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  29.     procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
  30.       Shift: TShiftState; X, Y: Integer);
  31.     procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  32.       Y: Integer);
  33.     procedure ResetBitBtnClick(Sender: TObject);
  34.     procedure AllowDraggingCBClick(Sender: TObject);
  35.   private
  36.     { Private declarations }
  37.     Dragging: Boolean;
  38.     XOffset, YOffset: Integer;
  39.     procedure MoveToolbar(X, Y: Integer);
  40.   public
  41.     { Public declarations }
  42.   end;
  43.  
  44. var
  45.   MainForm: TMainForm;
  46.  
  47. implementation
  48.  
  49. {$R *.DFM}
  50.  
  51. {- Initialize }
  52. procedure TMainForm.FormCreate(Sender: TObject);
  53. begin
  54.   Dragging := False;
  55. end;
  56.  
  57. {- Display number of selected SpeedButton in toolbar }
  58. procedure TMainForm.SpeedButton1Click(Sender: TObject);
  59. begin
  60.   ShowMessage('You selected button number ' +
  61.     IntToStr(TSpeedButton(Sender).Tag));
  62. end;
  63.  
  64. {- Start dragging operation on clicking in toolbar }
  65. procedure TMainForm.FloatingToolbarMouseDown(Sender: TObject;
  66.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  67. begin
  68.   if AllowDraggingCB.Checked then
  69.   begin
  70.     Dragging := True;     { Dragging opertion in effect }
  71.     SetCapture(Handle);   { Send all mouse messages to form }
  72.     XOffset := X;         { Save mouse coordinates to compute }
  73.     YOffset := Y;         { offset from top-left corner }
  74.   end;
  75. end;
  76.  
  77. {- End dragging operation on releasing mouse button }
  78. procedure TMainForm.FormMouseUp(Sender: TObject; Button: TMouseButton;
  79.   Shift: TShiftState; X, Y: Integer);
  80. begin
  81.   if Dragging then      { Ignore if not dragging }
  82.   begin
  83.     MoveToolbar(X, Y);  { Move toolbar to final location }
  84.     Dragging := False;  { End dragging operation }
  85.     ReleaseCapture;     { Return message handling to normal }
  86.   end;
  87. end;
  88.  
  89. {- Move toolbar if dragging operation in progress }
  90. procedure TMainForm.FormMouseMove(Sender: TObject; Shift:
  91.   TShiftState; X, Y: Integer);
  92. begin
  93.   if Dragging then      { Ignore if not dragging }
  94.     MoveToolbar(X, Y);  { Move toolbar to mouse location }
  95. end;
  96.  
  97. {- Move the toolbar to mouse location X and Y }
  98. procedure TMainForm.MoveToolbar(X, Y: Integer);
  99. begin
  100.   FloatingToolbar.Left := X - XOffset; // Adjust location of
  101.   FloatingToolbar.Top := Y - YOffset;  // panel top-left corner
  102. end;
  103.  
  104. {- Reset toolbar to startup location }
  105. procedure TMainForm.ResetBitBtnClick(Sender: TObject);
  106. begin
  107.   with FloatingToolbar do
  108.   begin
  109.     Left := 24;
  110.     Top := 24;
  111.   end;
  112.   AllowDraggingCB.Checked := True;
  113. end;
  114.  
  115. procedure TMainForm.AllowDraggingCBClick(Sender: TObject);
  116. begin
  117.   with Label1 do
  118.     Enabled := not Enabled;
  119. end;
  120.  
  121. end.
  122.  
  123.