#### Archived

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

# form drag and drop

This topic is 5910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
    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 on other sites
Hey cool. But uhm, does it matter how big the thing is I put at the top? How on earth does da compiler know which one I want to be the bar?

##### Share on other sites
So how on earth do I get this to work? Say I put a button at the top, and made it fill the entire top, how do I get this button to activate the code? Or what else do I do?

##### 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 on other sites
cooooooooooooool thank you sooooooooo much!!!
heheh

BTW is it ok if I use this code in my program, even if it''s going to be commercial?

##### Share on other sites
If you can make money with it by all means

k thx alot