I would like to have my own caption bar and therefore I am using basically a panel (Name: pnCaption) and remove the original caption bar in CreateParams. But the ability to move the window by MouseDown-MouseMove in the new panel is a problem.
Normally you would use the NCHITTEST message. BUT this isn't signaled if the mouse is over the panel (my own caption). See code ...
procedure TForm1.CreateParams(var params: TCreateParams);
begin
inherited Createparams(Params);
with Params do
Style := (Style or WS_POPUP) and (not WS_DLGFRAME);
end;
procedure TForm1.WM_NCHitTest(var Msg: TWMNcHitTest);
begin
inherited;
if PtInRect(pnCaption.BoundsRect, ScreenToClient(Point(Msg.XPos, Msg.YPos)))
then Msg.Result := HTCAPTION;
end;
I would appreciate any hints how to accomplish that task.
Christian
You can always drag a window by any control that has a mousedown event by using the "Magic" $F012 number with a WM_SYSCOMMAND message. It's something I picked up from Ray Kanopka (author of the excellent raize components), but I no longer remember how this was imparted to me.
It is also a neat and simple way of allowing users to move borderless forms by giving them a label of panel that looks like a caption. For example, I use it to allow users to move a borderless about dialog:
As I am looking into our old code for custom StatusBar component, which is descendant of TWinControl, to provide form resizing using StatusBar's grip we handle WM_NCHITTEST in the control, not in the form and return HTBOTTOMRIGHT:
This means that you need to implement descendant of your panel component (or hook it's message handling) and handle WM_NCHITTEST there.
Also, I'd go the route of handling WM_NCCALCSIZE and WM_NCPAINT messages in the form in order to provide your own caption area and avoid using TPanel or other control. But this is only my preference.
Not exactly what you're looking for, but for others interested in a similar technique, here's code for a TLabel-descended component that can serve as a caption bar:
The easiest way is probably to use a component that hasn't a
HWND
window handle and therefore can't receive messages. They will be passed to your form, where they can be handled the way you show in your question.Simply replacing the
TPanel
with a top-alignedTPaintBox
,TImage
or similarTGraphicControl
descendant would make your code work. You keep both the message handling of the form and the alignment support of the VCL.