Archived

This topic is now archived and is closed to further replies.

TheSisko

form drag and drop

Recommended Posts

Well you guys know the blue bar at the top of each window? I hate it. I wanna make one of my own. But how on earth do I make my entire form move as soon as the users starts to drag the bar? Is there any way to get to see the sourcecode for the normal blue bar?

Share this post


Link to post
Share on other sites
Add this procedure declaration to your form's private section

procedure WMNCHitTest(var Msg: TWMNcHitTest); message WM_NCHITTEST;

And this to the implementation section

procedure TForm1.WMNCHitTest(var Msg: TWMNcHitTest);
var
Index: Integer;
CursorPos: TPoint;
begin
inherited;
Msg.Result := HTCLIENT;
CursorPos := Point(Msg.XPos, Msg.YPos);
CursorPos := ScreenToClient(CursorPos);
Index := 0;
while Index < ComponentCount do
begin
if (Components[Index] is TControl) and PtInRect(TControl(Components[Index]).BoundsRect, CursorPos) then
Exit;
Inc(Index);
end;
{ HTCAPTION makes Windows think that we grabbed the caption bar. }
Msg.Result := HTCAPTION;
end;

Setting Msg.Result to HTCAPTION makes Windows think that you clicked on the form caption. This allows the mouse to drag the form around. The test for components does not allow the dragging if the user clicked on a component.

If you set the BorderStyle property to bsNone, then you can draw whatever you want at the top of the form and treat it as if it was the caption.

Steve 'Sly' Williams   Monkey Wrangler  Krome Studios

Edited by - sly on November 8, 2001 5:54:08 PM

Share this post


Link to post
Share on other sites
Hi there,

well i assume you want to totally rid yourself of the windows
border etc by using BorderStyle := bsNone. Do the following to move the window

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm1 = class(TForm)
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
FDifference,FOriginalPos,FCurrentPos: TPoint;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in Shift then
begin
GetCursorPos(FCurrentPos);

Left := FDifference.x - (FOriginalPos.x - FCurrentPos.x);
Top := FDifference.y - (FOriginalPos.y - FCurrentPos.y);
end;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if ssLeft in Shift then
begin
FDifference.x := Left;
FDifference.y := Top;

GetCursorPos(FOriginalPos);
end;
end;

end.

Hope its of some help

Mark

Edited by - MButchers on November 9, 2001 7:41:16 AM

Share this post


Link to post
Share on other sites
That code that I posted checked if the mouse was clicked inside a control. If so, then it exited. So if you put a button at the top of the form, then the code would do nothing. If you had no button at the top or any other control, then it would drag the form around. The code will allow the form to be dragged by clicking anywhere on the form that has no control on it. So the caption bar you want can be any size you wish.

Share this post


Link to post
Share on other sites