2007. december 26., szerda

Move a form without a caption bar


Problem/Question/Abstract:

I have a panel that acts as a custom title bar, i.e. the window should be dragged by clicking inside this panel. In this case WM_NCHITTEST is not posted to TForm when the mouse pointer is over TPanel.

Answer:

Solve 1:

Basically you intercept the mouse-down and convert it into the equivalent of choosing "Move" from the System menu. You can hook the main form and any container-objects such as panels to the same handler.

procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button <> mbLeft then
    Exit;
  if Shift <> [ssLeft] then
    Exit;
  ReleaseCapture;
  Perform(WM_SYSCOMMAND, SC_MOVE + 2, Integer(PointToSmallPoint(Point(X, Y))));
end;


Solve 2:

var
  OldX, OldY: Integer;

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  OldX := X;
  OldY := Y;
end;

procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if ssLeft in Shift then
    Form1.SetBounds(Left + (X - OldX), Top + (Y - OldY), Width, Height);
end;

Nincsenek megjegyzések:

Megjegyzés küldése