Programming Delphi How to hide the 3D border of TPageControl in Delphi?

An very easy way to hide TPageControl's 3D border is to reload the window form procedure. To capture the TCM_ADJUSTRECT message and redraw the pagecontrol's border. See below.

protected procedure WndProc(var Message:TMessage); override;

procedure TPageControl.WndProc(var Message:TMessage);
begin
  if Message.Msg=TCM_ADJUSTRECT then
  begin
    Inherited WndProc(Message);
    if Fborder=bsNone then
    begin
      PRect(Message.LParam)^.Left:=0;
      PRect(Message.LParam)^.Right:=ClientWidth;            
      PRect(Message.LParam)^.Top:=PRect(Message.LParam)^.Top-4;
      PRect(Message.LParam)^.Bottom:=ClientHeight;
    end;
  end
  else
    Inherited WndProc(Message);
end;

{Then we draw the tabsheets by our own method}
TPageControl=class(ComCtrls.TPageControl)
private
  procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
protected
  procedure CreateParams(var Params: TCreateParams); override;
end;

type
  TMyPageControl=class(TPageControl)
  end;

{When form create, set the ctl3D property to false}
procedure TForm1.FormCreate(Sender: TObject);
begin
  TMyPageControl(PageControl1).Ctl3D:=False;
end;

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

procedure TPageControl.CNDrawItem(var Message: TWMDrawItem);
var
  SaveIndex: Integer;
  Caption:string;
  Size   :TSize;
  x,y    :integer;
begin
  with Message.DrawItemStruct^ do
  begin
    SaveIndex := SaveDC(hDC);
    Canvas.Lock;
    try
      Canvas.Handle:=hDC;
      Canvas.Font  :=Font;
      Canvas.Brush :=Brush;
      //DrawTab(itemID, rcItem, itemState and ODS_SELECTED 0);
      Caption:=Self.Tabs.Strings[ItemID];
      Size:=Canvas.TextExtent(Caption);
      x:=rcItem.Left+(rcItem.Right-rcItem.Left-Size.cx) div 2;
      y:=rcItem.Top +(rcItem.Bottom-rcItem.Top-Size.cy) div 2+1;
      if itemState and ODS_SELECTED0 then dec(y);
      Canvas.TextRect(rcItem,x,y,Caption);
    finally
      Canvas.Handle := 0;
      Canvas.Unlock;
      RestoreDC(hDC, SaveIndex);
    end;
  end;
  Message.Result := 1;
end;


Add comment


Security code
Refresh

Programming - Delphi