Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 603739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:57:49+00:00 2026-05-13T16:57:49+00:00

How can I implement a close button for a TTabsheet of a TPageControl like

  • 0

How can I implement a close button for a TTabsheet of a TPageControl like Firefox?

Edit:
Delphi Version: Delphi 2010
OS: Windows XP and up

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:57:50+00:00Added an answer on May 13, 2026 at 4:57 pm

    Now with Theme support (include Windows, UxTheme, Themes units)!

    type
      TFormMain = class(TForm)
        {...}
      private
        FCloseButtonsRect: array of TRect;
        FCloseButtonMouseDownIndex: Integer;
        FCloseButtonShowPushed: Boolean;
        {...}
      end;
    
    {...}
    
    procedure TFormMain.FormCreate(Sender: TObject);
    var
      I: Integer;
    begin
      PageControlCloseButton.TabWidth := 150;
      PageControlCloseButton.OwnerDraw := True;
    
      //should be done on every change of the page count
      SetLength(FCloseButtonsRect, PageControlCloseButton.PageCount);
      FCloseButtonMouseDownIndex := -1;
    
      for I := 0 to Length(FCloseButtonsRect) - 1 do
      begin
        FCloseButtonsRect[I] := Rect(0, 0, 0, 0);
      end;
    end;
    
    procedure TFormMain.PageControlCloseButtonDrawTab(Control: TCustomTabControl;
      TabIndex: Integer; const Rect: TRect; Active: Boolean);
    var
      CloseBtnSize: Integer;
      PageControl: TPageControl;
      TabCaption: TPoint;
      CloseBtnRect: TRect;
      CloseBtnDrawState: Cardinal;
      CloseBtnDrawDetails: TThemedElementDetails;
    begin
      PageControl := Control as TPageControl;
    
      if InRange(TabIndex, 0, Length(FCloseButtonsRect) - 1) then
      begin
        CloseBtnSize := 14;
        TabCaption.Y := Rect.Top + 3;
    
        if Active then
        begin
          CloseBtnRect.Top := Rect.Top + 4;
          CloseBtnRect.Right := Rect.Right - 5;
          TabCaption.X := Rect.Left + 6;
        end
        else
        begin
          CloseBtnRect.Top := Rect.Top + 3;
          CloseBtnRect.Right := Rect.Right - 5;
          TabCaption.X := Rect.Left + 3;
        end;
    
        CloseBtnRect.Bottom := CloseBtnRect.Top + CloseBtnSize;
        CloseBtnRect.Left := CloseBtnRect.Right - CloseBtnSize;
        FCloseButtonsRect[TabIndex] := CloseBtnRect;
    
        PageControl.Canvas.FillRect(Rect);
        PageControl.Canvas.TextOut(TabCaption.X, TabCaption.Y, PageControl.Pages[TabIndex].Caption);
    
        if not UseThemes then
        begin
          if (FCloseButtonMouseDownIndex = TabIndex) and FCloseButtonShowPushed then
            CloseBtnDrawState := DFCS_CAPTIONCLOSE + DFCS_PUSHED
          else
            CloseBtnDrawState := DFCS_CAPTIONCLOSE;
    
          Windows.DrawFrameControl(PageControl.Canvas.Handle,
            FCloseButtonsRect[TabIndex], DFC_CAPTION, CloseBtnDrawState);
        end
        else
        begin
          Dec(FCloseButtonsRect[TabIndex].Left);
    
          if (FCloseButtonMouseDownIndex = TabIndex) and FCloseButtonShowPushed then
            CloseBtnDrawDetails := ThemeServices.GetElementDetails(twCloseButtonPushed)
          else
            CloseBtnDrawDetails := ThemeServices.GetElementDetails(twCloseButtonNormal);
    
          ThemeServices.DrawElement(PageControl.Canvas.Handle, CloseBtnDrawDetails,
            FCloseButtonsRect[TabIndex]);
        end;
      end;
    end;
    
    procedure TFormMain.PageControlCloseButtonMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    var
      I: Integer;
      PageControl: TPageControl;
    begin
      PageControl := Sender as TPageControl;
    
      if Button = mbLeft then
      begin
        for I := 0 to Length(FCloseButtonsRect) - 1 do
        begin
          if PtInRect(FCloseButtonsRect[I], Point(X, Y)) then
          begin
            FCloseButtonMouseDownIndex := I;
            FCloseButtonShowPushed := True;
            PageControl.Repaint;
          end;
        end;
      end;
    end;
    
    procedure TFormMain.PageControlCloseButtonMouseMove(Sender: TObject;
      Shift: TShiftState; X, Y: Integer);
    var
      PageControl: TPageControl;
      Inside: Boolean;
    begin
      PageControl := Sender as TPageControl;
    
      if (ssLeft in Shift) and (FCloseButtonMouseDownIndex >= 0) then
      begin
        Inside := PtInRect(FCloseButtonsRect[FCloseButtonMouseDownIndex], Point(X, Y));
    
        if FCloseButtonShowPushed <> Inside then
        begin
          FCloseButtonShowPushed := Inside;
          PageControl.Repaint;
        end;
      end;
    end;
    
    procedure TFormMain.PageControlCloseButtonMouseLeave(Sender: TObject);
    var
      PageControl: TPageControl;
    begin
      PageControl := Sender as TPageControl;
      FCloseButtonShowPushed := False;
      PageControl.Repaint;
    end;
    
    procedure TFormMain.PageControlCloseButtonMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    var
      PageControl: TPageControl;
    begin
      PageControl := Sender as TPageControl;
    
      if (Button = mbLeft) and (FCloseButtonMouseDownIndex >= 0) then
      begin
        if PtInRect(FCloseButtonsRect[FCloseButtonMouseDownIndex], Point(X, Y)) then
        begin
          ShowMessage('Button ' + IntToStr(FCloseButtonMouseDownIndex + 1) + ' pressed!');
    
          FCloseButtonMouseDownIndex := -1;
          PageControl.Repaint;
        end;
      end;
    end;
    

    Looks like:

    page control with buttons

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to know if I can implement a method on MemoryCache that removes
I want to implement a close button on a PageControl and I have read
I can implement both variants - it's easy. But I'm interested: what approach is
How can implement reordring in winforms datagridview by using the feature of drag and
Can someone please tell me how I can implement the following line of pseudo-code.
Anyone knows how i can implement re-arrangable divs (Drag the divs around on the
How I can implement dynamic Jtree, which shows created instances of clases? For example,
I know that classes can implement various special methods, such as __iter__ , __setitem__
How I can implement Background Processing in asp.net. Can anyone give a small and
Any ideas of how best i can implement article revision history for a Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.