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.
Asked
Active
Viewed 3,847 times
4

Community
- 1
- 1

Cristian Vasuica
- 369
- 9
- 22
-
See also [How to implement a close button for a TTabsheet of a TPageControl](https://stackoverflow.com/q/2201850/850848). – Martin Prikryl Mar 25 '21 at 13:49
1 Answers
4
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;

David Heffernan
- 601,492
- 42
- 1,072
- 1,490
-
i will give it a try in delphi xe2 and let you now if it works. thank you very much David. – Cristian Vasuica May 03 '12 at 12:38
-
I still can't get it to an end: David you are telling me that the DrawTab event is used, but I couldn't find it in the example. I found only TFormMain.PageControlCloseButtonDrawTab this event and when I try to implement it i am getting an error `Undeclared Identifier Page Control Close Button`.. Do you know where I can find the whole source code of that example? – Cristian Vasuica May 03 '12 at 14:48
-
In the Object Inspector find OnDrawTab etc. and assign your handlers to those events. This is intro stuff and you should read a tutlrial to master it. – David Heffernan May 03 '12 at 15:19
-
I have read a couple of books about delphi programming and built a few applications with db(for my last semestre project), I found the OnDrawTab event wrote that code and getting errors(_Undeclared Identifier Close Button of Page Control_); but in the example shown on the other question I am not sure if this one is used( i think that the event PageControlCloseButtonDrawTab is used- please excuse me if I am wrong). i am sure that it isn`t so hard, but i can`t get it- I know that is difficult to find the whole source code, I need a very simple example just to figure how to do it. – Cristian Vasuica May 03 '12 at 19:23
-
Thank You David; You are the man! I wrote almost the same code but I wasn't declaring the procedures correctly in the _type area_; Now i need to figure it out with applying styles to the buttons. – Cristian Vasuica May 04 '12 at 05:07