Search code examples
delphidelphi-xe2tpagecontrol

draw a close button in each Ttabsheet of a TPageControl


I want to implement a close button on a PageControl and I have read this question also How to implement a close button for a TTabsheet of a TPageControl
The thing is I can't figure it out how to implement the code provided in the answer of Ulrichb... are they building a new component descendant from TPageControl or not? if someone could explain where to write that certain code i would be thankfull! I have a single teacher who knows a little bit of delphi at my school but he couldn`t help me out..and I am sorry if this is a silly question but i am new to delphi and programming.


Solution

  • The code in the question you link to does not create a new component. Instead it implements custom drawing by using events of the page control. Specifically these events:

    • OnDrawTab
    • OnMouseDown
    • OnMouseMove
    • OnMouseLeave
    • OnMouseUp

    You must use the Delphi form designer to connect these event handlers up to the matching events to make the code work.

    This approach was probably chosen for simplicity when answering that question but it does not scale to an application with many forms that have page controls. In that situation you would want to derive a new page control component.

    If you do that then, rather than using events, you need to override the following methods:

    • DrawTab
    • MouseDown
    • MouseMove
    • MouseUp

    In addition to this you must replicate the OnMouseLeave behaviour. That requires a message handler.

    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
    ....
    procedure TMyPageControl.CMMouseLeave(var Message: TMessage);
    begin
      inherited;
      if Message.LParam=0 then
      begin
        // move OnMouseLeave code here
      end;
    end;