Move form without border style
Asked Answered
S

2

14

how do I move a borderless form? I tried looking on the internet, but nothing. Thanks a lot.

Swaggering answered 6/6, 2012 at 19:45 Comment(2)
may be here : delphi.about.com/od/windowsshellapi/a/dragnocaption.htmUnconquerable
Duplicate of C++Builder: Create a TForm with BorderStyle bsNone that is nevertheless movable and resizable - that code is for C++Builder, but it's still the same VCL! It should be trivial to change to Delphi.Launceston
G
30

You can drag a form using any contained control, including itself.

Using the following example, you can move a form by clicking on its canvas and dragging. You could do the same with a panel on the form by putting the same code in the panel's MouseDown event, which would let you create your own pseudo caption bar.

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
const
  SC_DRAGMOVE = $F012;
begin
  if Button = mbLeft then
  begin
    ReleaseCapture;
    Perform(WM_SYSCOMMAND, SC_DRAGMOVE, 0);
  end;
end;
Guideline answered 6/6, 2012 at 20:7 Comment(0)
B
13

If you mean dragging the window by the mouse, you can override WM_NCHITTEST message handling and return HTCAPTION for the drag region. The below will drag the window within the upper 30 pixels for insance:

type
  TForm1 = class(TForm)
  private
  protected
    procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
  end;

..

procedure TForm1.WMNCHitTest(var Message: TWMNCHitTest);
var
  Pt: TPoint;
begin
  Pt := ScreenToClient(SmallPointToPoint(Message.Pos));
  if Pt.Y < 30 then
    Message.Result := HTCAPTION
  else
    inherited;
end;
Bixler answered 6/6, 2012 at 19:55 Comment(6)
@David: Lately, I have been a bit sceptic to this approach, because, basically, you are telling the OS that the entire form is the window's caption, which might not actually be semantically true. (Well, in Sertac's code, perhaps the first 30 pixels are in fact the caption, but generally, this might not be true.)Oberheim
@andreas Do you have any concrete reason to reject this? What are the downsides? This is the standard approach.Valency
@David: No, no concrete downside that I can think of.Oberheim
An alternative is to implement an OnMouseDown handler, where you set a move state and store the mouse down position, a OnMouseMove handler where you move the form, and an OnMouseUp handler, where you clear the move state. It's not that much work. It for instance has the benefit of letting you decide for yourself how many pixels the mouse should move before you initiate the move. This sometimes can be desirable.Pulsometer
I'm trying to use this method and I've found that I can't close the form by the "X" button. So, there's one downside. I suppose I could just drop a tiny button and write the code, of course.Cosmetician
@Craig - Call the inherited first, modify the result only if it's 'HTCLIENT'. Alternatively modify your intervention region to exclude the buttons.Bixler

© 2022 - 2024 — McMap. All rights reserved.