Title: Moving form without titlebar
Question: How to create a form without a title bar that you can still move around
Answer:
This application shows you how to build a form without a titlebar, while still being able to move the window with your mouse. No extra components needed, just delphi, so no hassle with third party components!
Answer:
========================================
========================================
Moving Window Without Titlebar
========================================
========================================
Project1.dpr
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
========================================
Unit1.pas
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormCreate(Sender: TObject);
procedure Label1DblClick(Sender: TObject);
procedure FormDblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var Moving: boolean;
OldX, OldY, OldLeft, OldTop: integer;
{$R *.DFM}
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (Button = mbLeft) then begin
Moving := True;
OldX := X;
OldY := Y;
OldLeft := X;
OldTop := Y;
end;
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Moving := false
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if Moving then begin
OldX := X;
OldY := Y;
SetBounds( Left+OldX-OldLeft,
Top+OldY-OldTop,
Width,
Height);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Moving := false;
end;
procedure TForm1.Label1DblClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.FormDblClick(Sender: TObject);
begin
Close;
end;
end.
========================================
Unit1.dfm
object Form1: TForm1
Left = 199
Top = 162
BorderStyle = bsNone
Caption = 'Form1'
ClientHeight = 446
ClientWidth = 688
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDblClick = FormDblClick
OnMouseDown = FormMouseDown
OnMouseMove = FormMouseMove
OnMouseUp = FormMouseUp
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 242
Top = 216
Width = 204
Height = 13
Caption = 'Double-Click anywhere to close this'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
OnDblClick = Label1DblClick
end
end
========================================
========================================