Marcoscdoni
Marcoscdoni

Reputation: 985

Resize Form even if it is borderless - Remove bevel edge

Using the following code to make a borderless form resizable works great:

type
 TForm1 = class(TForm)
 protected
   procedure CreateParams(var Params: TCreateParams); override;
 end;

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
 BorderStyle := bsNone;
 inherited;
 Params.ExStyle := Params.ExStyle or WS_EX_STATICEDGE;
 Params.Style := Params.Style or WS_SIZEBOX;
end;

But, see the image:

enter image description here Why at the top is there a bevel around the edge.? Any suggestions to remove it?

Upvotes: 5

Views: 3069

Answers (3)

MohsenB
MohsenB

Reputation: 1921

This code create borderless form resizable and fix maximize cover taskbar:

protected
     procedure CreateParams(var Params: TCreateParams); override;

...

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  BorderStyle := bsNone;
  inherited;
  if WindowState = TWindowState.wsNormal then begin
    Params.ExStyle := Params.ExStyle or WS_EX_STATICEDGE;
    Params.Style := Params.Style or WS_SIZEBOX;
  end;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  if WindowState = TWindowState.wsMaximized then begin
    SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) and (not WS_SIZEBOX));
    SetWindowLong(Handle,GWL_EXSTYLE,GetWindowLong(Handle,GWL_EXSTYLE) and (not WS_EX_STATICEDGE));
  end
  else begin
    SetWindowLong(Handle,GWL_STYLE,GetWindowLong(Handle,GWL_STYLE) or WS_SIZEBOX);
    SetWindowLong(Handle,GWL_EXSTYLE,GetWindowLong(Handle,GWL_EXSTYLE) or WS_EX_STATICEDGE);
  end;
end;
    
procedure TForm1.BtnMaximaizeClick(Sender: TObject);
begin
  if WindowState = TWindowState.wsNormal then begin
    WindowState:= TWindowState.wsMaximized;
    SetBounds(Screen.WorkAreaRect.Left, Screen.WorkAreaRect.Top, Screen.WorkAreaRect.Width, Screen.WorkAreaRect.Height);
  end else
    WindowState:= TWindowState.wsNormal;
end;

Upvotes: 1

Fables Alive
Fables Alive

Reputation: 2808

Here is absolute answer. works on win 7,8 win10 as well. also weird thin top bar that appears are gone.
(if you are using modern themes, remove styleelements of form (sefont,seclient,seborder))

yes it works if you have a panel set align to alclient. just modify panel.alignwithmargins:=true; and panel.margins>1

(note: do forget not to set form.borderstyle to bsNone;)

 protected
    procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;

..

procedure Tfmmain.WMNCHitTest(var Message: TWMNCHitTest);
const
  EDGEDETECT = 7; // adjust
var
  deltaRect: TRect;
begin
  inherited;
  if BorderStyle = bsNone then
    with Message, deltaRect do
    begin
      Left := XPos - BoundsRect.Left;
      Right := BoundsRect.Right - XPos;
      Top := YPos - BoundsRect.Top;
      Bottom := BoundsRect.Bottom - YPos;
      if (Top < EDGEDETECT) and (Left < EDGEDETECT) then
        Result := HTTOPLEFT
      else if (Top < EDGEDETECT) and (Right < EDGEDETECT) then
        Result := HTTOPRIGHT
      else if (Bottom < EDGEDETECT) and (Left < EDGEDETECT) then
        Result := HTBOTTOMLEFT
      else if (Bottom < EDGEDETECT) and (Right < EDGEDETECT) then
        Result := HTBOTTOMRIGHT
      else if (Top < EDGEDETECT) then
        Result := HTTOP
      else if (Left < EDGEDETECT) then
        Result := HTLEFT
      else if (Bottom < EDGEDETECT) then
        Result := HTBOTTOM
      else if (Right < EDGEDETECT) then
        Result := HTRIGHT
    end;
end;

Upvotes: 6

Somebody
Somebody

Reputation: 733

add 5 panels

pnlTop

pnlRight

pnlLeft

pnlBottom

pnlRSZ

        procedure Tfrmmain.FormCreate(Sender: TObject);
          begin
            BorderStyle := bsNone;
            pnlTop.height:=1;
            pnlTop.Align:=alTop;
            pnlBottom.height:=1;
            pnlBottom.Align:=alBottom;
            pnlRight.width:=1;
            pnlRight.Align:=alRight;
            pnlLeft.width:=1;
            pnlLeft.Align:=alLeft;
            pnlRSZ.width:=3;
            pnlRSZ.height:=3;
            pnlRSZ.left:=frmmain.width-3;
            pnlRSZ.top:=frmmain.height-3;
            pnlRSZ.anchors:=[akRight,akBottom];


            pnlTop.Cursor:=crSizeNS;
            pnlBottom.Cursor:=crSizeNS;
            pnlRight.Cursor:=crSizeWE;
            pnlLeft.Cursor:=crSizeWE;
            pnlRSZ.Cursor:=crSizeNWSE;

            pnlTop.color:=frmmain.color;
            pnlBottom.color:=frmmain.color;
            pnlRight.color:=frmmain.color;
            pnlLeft.color:=frmmain.color;
            pnlRSZ.color:=frmmain.color;
          End;


    procedure Tfrmmain.pnlTopMouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer);
    begin
      if Button = mbLeft then
      begin
        ReleaseCapture;
        TForm(FrmMain).Perform(WM_SYSCOMMAND, SC_SIZE + WMSZ_Top, 0);
      end;
    end;

    procedure Tfrmmain.pnlRightMouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer);
    begin
      if Button = mbLeft then
      begin
        ReleaseCapture;
        TForm(FrmMain).Perform(WM_SYSCOMMAND, SC_SIZE + WMSZ_RIGHT, 0);
      end;
    end;

    procedure Tfrmmain.pnlLeftMouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer);
    begin
      if Button = mbLeft then
      begin
        ReleaseCapture;
        TForm(FrmMain).Perform(WM_SYSCOMMAND, SC_SIZE + WMSZ_Left, 0);
      end;
    end;

    procedure Tfrmmain.pnlBottomMouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer);
    begin
      if Button = mbLeft then
      begin
        ReleaseCapture;
        TForm(FrmMain).Perform(WM_SYSCOMMAND, SC_SIZE + WMSZ_Bottom, 0);
      end;
    end;

    procedure Tfrmmain.pnlRSZMouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer);
    begin
      if Button = mbLeft then
      begin
        ReleaseCapture;
        TForm(FrmMain).Perform(WM_SYSCOMMAND, SC_SIZE + WMSZ_BOTTOMRIGHT, 0);
      end;
    end;

Upvotes: 3

Related Questions