Search code examples
delphiwinapidelphi-10.2-tokyo

How to hide borders created by WS_SIZEBOX using Delphi?


I create custom panels (TMyPanel) with runtime resizing feature. Is there any way to hide the borders without losing the resizing feature?

type
  TMyPanel = class(TPanel)
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  end;

...

procedure TMyPanel.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.Style := Params.Style or WS_SIZEBOX;
end;

...

var
  MyPanel1: TMyPanel;
begin
  MyPanel1:=TMyPanel.Create(self);
  MyPanel1.Parent:=self;
  ...
end;

Solution

  • As I can see from your question, you want to achieve resing of TPanel even if has no non-client borders. If I got you right I can share with you one of solution. For some of my control I used this technique and it works.
    Indeed, WS_SIZEBOX style is not needed here.

    unit PanelEx;
    
    interface
    
    uses
      Windows,
      Messages,
      ExtCtrls;
    
    type
      TPanelEx = class(TPanel)
      protected
        procedure WMNCHITTEST(var Message: TMessage); message WM_NCHITTEST;
      end;
    
    
    implementation
    
    
    procedure TPanelEx.WMNCHITTEST(var Message: TMessage);
    const
      BorderSize = 10;
    var
      P: TPoint;
    begin
      Inherited;
    
      if not Assigned(Parent) then
        Exit;
    
      P.X := LOWORD(Message.LParam);
      P.Y := HIWORD(Message.LParam);
    
      // Convert screen coordinates into client
      P := Parent.ScreenToClient(P);
    
      // Decide what result message should have
      if (Abs(Left + Width - P.X) < BorderSize) and (Abs(Top + Height - P.Y) < BorderSize) then
        Message.Result := HTBOTTOMRIGHT
      else
      if (Abs(Left - P.X) < BorderSize) and (Abs(Top + Height - P.Y) < BorderSize) then
        Message.Result := HTBOTTOMLEFT
      else
      if (Abs(Left + Width - P.X) < BorderSize) and (Abs(Top - P.Y) < BorderSize) then
        Message.Result := HTTOPRIGHT
      else
      if (Abs(Left - P.X) < BorderSize) and (Abs(Top - P.Y) < BorderSize) then
        Message.Result := HTTOPLEFT
      else
      if Abs(Left - P.X) < BorderSize then
        Message.Result := HTLEFT
      else
      if Abs(Top - P.Y) < BorderSize then
        Message.Result := HTTOP
      else
      if Abs(Left + Width - P.X) < BorderSize then
        Message.Result := HTRIGHT
      else
      if Abs(Top + Height - P.Y) < BorderSize then
        Message.Result := HTBOTTOM;
    end;
    
    
    end.