Author: Jonas Bilinkevicius
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.
1 2 procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
3 Shift: TShiftState; X, Y: Integer);
4 begin5 if Button <> mbLeft then6 Exit;
7 if Shift <> [ssLeft] then8 Exit;
9 ReleaseCapture;
10 Perform(WM_SYSCOMMAND, SC_MOVE + 2, Integer(PointToSmallPoint(Point(X, Y))));
11 end;
Solve 2:
12 var13 OldX, OldY: Integer;
14 15 procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
16 Shift: TShiftState; X, Y: Integer);
17 begin18 OldX := X;
19 OldY := Y;
20 end;
21 22 procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
23 Integer);
24 begin25 if ssLeft in Shift then26 Form1.SetBounds(Left + (X - OldX), Top + (Y - OldY), Width, Height);
27 end;