form drag and drop

Started by
7 comments, last by TheSisko 22 years, 5 months ago
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?
Advertisement
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
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
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?
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?
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
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?
If you can make money with it by all means

k thx alot

BTW is code copyrighted?
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.
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers

This topic is closed to new replies.

Advertisement