Recent

Author Topic: TCustomControl class and OnPaint event  (Read 835 times)

VisualLab

  • Hero Member
  • *****
  • Posts: 669
TCustomControl class and OnPaint event
« on: January 14, 2024, 01:31:40 pm »
I create my own control class. It is to be available for Lazarus and Delphi. Basically, it is ported from Delphi and expanded a bit. Currently I use:

- operating system: Windows 10,
- Lazarus 2.2.4 64-bit,
- Delphi 12 Prof.

To make the most of what the libraries provide (VCL and LCL, respectively), I based my class on the TCustomControl class. Unfortunately, there is one problem. In the LCL library, the TCustomControl class has an OnPaint event added, which is public (maybe it was supposed to be published, but someone made a mistake?). However, in the VCL library (Delphi), the TCustomControl class does not have this event. To better illustrate this issue, I present the definitions of this class in both libraries below.

Definition of the TCustomControl class in the VCL library (Delphi):

Code: Pascal  [Select][+][-]
  1. TCustomControl = class(TWinControl)
  2.   private
  3.     FCanvas: TCanvas;
  4.     procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  5.   protected
  6.     procedure Paint; virtual;
  7.     procedure PaintWindow(DC: HDC); override;
  8. {$IF NOT DEFINED(CLR)}
  9.     property Canvas: TCanvas read FCanvas;
  10. {$ENDIF}
  11.   public
  12. {$IF DEFINED(CLR)}                                                                    
  13.     function get_Canvas: TCanvas;
  14.     property Canvas: TCanvas read get_Canvas;
  15.     property Color;
  16. {$ENDIF}
  17.     constructor Create(AOwner: TComponent); override;
  18.     destructor Destroy; override;
  19.   end;

Definition of the TCustomControl class in the LCL library (Lazarus):

Code: Pascal  [Select][+][-]
  1. TCustomControl = class(TWinControl)
  2.   private
  3.     FCanvas: TCanvas;
  4.     FOnPaint: TNotifyEvent;  // <- an event that is not in the VCL
  5.   protected
  6.     class procedure WSRegisterClass; override;
  7.     procedure WMPaint(var Message: TLMPaint); message LM_PAINT;
  8.     procedure DestroyWnd; override;
  9.     procedure PaintWindow(DC: HDC); override;
  10.     procedure FontChanged(Sender: TObject); override;
  11.     procedure SetColor(Value: TColor); override;
  12.     procedure Paint; virtual;
  13.   public
  14.     constructor Create(AOwner: TComponent); override;
  15.     destructor Destroy; override;
  16.   public
  17.     property Canvas: TCanvas read FCanvas write FCanvas;
  18.     property BorderStyle;
  19.     property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;  // <- an event that is not in the VCL
  20.   end;

The class I am creating does not need the OnPaint event. Moreover, once installed in the IDE, the user would be misled to see this event. He would be even more surprised to find that this event is not used. The OnPaint event should not be present in the TCustomControl class. There should be a separate class for this purpose.

I understand that there are people who may use this event, but in such a situation there should be two classes:

- TCustomControl, behaving similarly to the one in VCL,
- TPaintWinControl, which would be a window control with drawing capabilities like in TPaintBox.

The question for people supervising the work on the LCL foundation is as follows: is there a chance to change the definition of the TCustomControl class?

If there is no chance for change, then too bad. I will have to create my own base class, similar to TCustomControl but without the OnPaint event. The problem is that there will be a lot of conditional compilation directives for Delphi and Lazarus in the code. And this is not the only class I create that is based on TCustomControl.

jamie

  • Hero Member
  • *****
  • Posts: 6888
Re: TCustomControl class and OnPaint event
« Reply #1 on: January 14, 2024, 05:45:32 pm »
Did you try redefining the OnPaint Property in your control within a protected/Private section?

 ...
  Protected
    Property OnPaint;
...

etc, something like that.
The only true wisdom is knowing you know nothing

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1505
    • Lebeau Software
Re: TCustomControl class and OnPaint event
« Reply #2 on: January 15, 2024, 09:41:55 pm »
To make the most of what the libraries provide (VCL and LCL, respectively), I based my class on the TCustomControl class. Unfortunately, there is one problem. In the LCL library, the TCustomControl class has an OnPaint event added, which is public (maybe it was supposed to be published, but someone made a mistake?).

No mistake.  TCustom... classes don't generally expose properties and events as published, that is up to descendant classes to decide what they want to expose.

The class I am creating does not need the OnPaint event. Moreover, once installed in the IDE, the user would be misled to see this event.

As you can see, the OnPaint event in LCL is public not published, so the user should not see the event in the IDE unless a derived class intentionally promotes the event to published.

The question for people supervising the work on the LCL foundation is as follows: is there a chance to change the definition of the TCustomControl class?

My guess is - not likely.  They added it to LCL for a reason, even though it doesn't exist in VCL.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018