Recent

Author Topic: Separate module for closing TPageControl tabs  (Read 1464 times)

BIT

  • Full Member
  • ***
  • Posts: 119
Separate module for closing TPageControl tabs
« on: September 14, 2021, 08:10:07 am »
Hello, can this code be saved in separate modules and used in the main form for the TPageControl component
close tab icon.
I do not know how to work with classes))

Code: Pascal  [Select][+][-]
  1. unit PageControlButtonClose;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, ComCtrls, LCLType, Controls, types, Graphics, LCLIntf;
  9.  
  10. type
  11.  
  12.   { TPageControl }
  13.   TPageControl = class(ComCtrls.TPageControl)
  14.   private
  15.   const
  16.     btnSize = 14;
  17.  
  18.   protected
  19.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  20.       X, Y: integer); override;
  21.     procedure PaintWindow(DC: HDC); override;
  22.   end;
  23.  
  24. implementation
  25.  
  26. { TPageControl }
  27.  
  28. procedure TPageControl.MouseDown(Button: TMouseButton; Shift: TShiftState;
  29.   X, Y: integer);
  30.  
  31. var
  32.   R: TRect;
  33. begin
  34.   inherited MouseDown(Button, Shift, X, Y);
  35.   if Button = mbLeft then
  36.   begin
  37.     R := TabRect(ActivePageIndex);
  38.     if PtInRect(Classes.Rect(R.Right - btnSize - 5, R.Top + 4,
  39.       R.Right - 5, R.Top + btnSize + 4), Classes.Point(X, Y)) then
  40.     begin
  41.  
  42.       ActivePage.Free;
  43.     end;
  44.  
  45.   end;
  46. end;
  47.  
  48. procedure TPageControl.PaintWindow(DC: HDC);
  49. var
  50.   i: integer;
  51.   R: TRect;
  52.   bm: TBitmap;
  53. begin
  54.   inherited PaintWindow(DC);
  55.  
  56.   bm := TBitmap.Create;
  57.   try
  58.     bm.SetSize(16, 16);
  59.    Images.GetBitmap(0, bm);
  60.  
  61.     for i := 0 to Pred(PageCount) do
  62.     begin
  63.       R := TabRect(i);
  64.    
  65.       StretchBlt(DC, R.Right - btnSize - 4, R.Top + 2,
  66.         btnSize, btnSize, bm.Canvas.Handle, 0, 0, 16, 16, cmSrcCopy);
  67.     end;
  68.   finally
  69.     bm.Free;
  70.   end;
  71. end;
  72.  
  73. end.
« Last Edit: September 14, 2021, 12:07:52 pm by BIT »

BIT

  • Full Member
  • ***
  • Posts: 119
Re: Separate module for closing TPageControl tabs
« Reply #1 on: September 14, 2021, 09:02:44 am »
This code does not work when connected to the main form from a separate module. If this code is inserted into the main form it works fine. The question is how to use this code from the plug-in in the main one?

Awkward

  • Jr. Member
  • **
  • Posts: 95
Re: Separate module for closing TPageControl tabs
« Reply #2 on: September 14, 2021, 09:54:21 am »
Easy to use... LCL must be patched ^_^

zamtmn

  • Sr. Member
  • ****
  • Posts: 402
Re: Separate module for closing TPageControl tabs
« Reply #3 on: September 14, 2021, 11:06:31 am »
This is a hack based on class substitution, it will not work in a separate module

jamie

  • Hero Member
  • *****
  • Posts: 4911
Re: Separate module for closing TPageControl tabs
« Reply #4 on: September 14, 2021, 01:17:26 pm »
This is a hack based on class substitution, it will not work in a separate module

If you place the unit at the end or after the unit that houses the original class the compiler should see that one first in the local uses list.
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 734
Re: Separate module for closing TPageControl tabs
« Reply #5 on: September 14, 2021, 01:20:21 pm »
Why not write as a class helper? (instead of inheritance)
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

korba812

  • Full Member
  • ***
  • Posts: 196
Re: Separate module for closing TPageControl tabs
« Reply #6 on: September 14, 2021, 02:11:04 pm »
Why not write as a class helper? (instead of inheritance)
Because you can't override virtual methods this way.

Zvoni

  • Hero Member
  • *****
  • Posts: 734
Re: Separate module for closing TPageControl tabs
« Reply #7 on: September 14, 2021, 02:20:54 pm »
Why not write as a class helper? (instead of inheritance)
Because you can't override virtual methods this way.
https://wiki.freepascal.org/Helper_types#Method_hiding
Quote
Restrictions

A helper type may not

    contain (class) destructors (except in trunk FPC 3.3.1)
    contain class constructors (except in trunk FPC 3.3.1)
    contain fields
    contain abstract methods
    "override" virtual methods of the extended class (they can be hidden by the helper though)

Methods of the extended type can be overloaded (thus they are not hidden by the helper) by using the overload keyword.
A call to Inherited MethodName should still work, or not?
No, it doesn't, but the question remains: Does he even need to call the "Ancestor"-Method?
« Last Edit: September 14, 2021, 02:25:33 pm by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

BIT

  • Full Member
  • ***
  • Posts: 119
Re: Separate module for closing TPageControl tabs
« Reply #8 on: September 14, 2021, 02:21:53 pm »
This is a hack based on class substitution, it will not work in a separate module

If you place the unit at the end or after the unit that houses the original class the compiler should see that one first in the local uses list.
Thank you earned!

 

TinyPortal © 2005-2018