Wednesday, January 5, 2011

Making A Draggable Titlebar

unit Unit1;

interface

uses
  Windows, Classes, Controls, Forms, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  X_pos: Integer;
  Y_pos: Integer;
implementation

{$R *.DFM}

procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  X_pos := X;
  Y_pos := Y;
end;

procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if HiWord(GetAsyncKeyState(VK_LBUTTON)) > 0 then
  begin
    Form1.Left := Form1.Left + X - X_pos;
    Form1.Top := Form1.Top + Y - Y_pos;
  end;
end;

end.


Draggable titlebars of this kind are  most useful in cases where you have a form without a titlebar, but still need to be able to drag the form.  Use a Label and position and size it where you want the draggable 'hook' to be.  If you wish, set the Label as transparent, and leave the Caption empty; and remember to set the Label's Autosize as false.
In this example we show the attachment to a Label; however, the same technique may be applied to most controls with MouseMove and MouseDown events.  See also

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum