I'm trying to create a custom label based on TWinControl. I'm using TWinControl because on the control there will be 2 labels and one shape.
Now, for simplicity reasons, in this example from below, I would like to populate the Caption property of this TWinControl with the name of component.
Because the Name of the component is not available during creation, I need to find another way.
unit MappLabelTest;
interface
uses
Winapi.Windows, Winapi.Messages, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Controls, Vcl.Graphics, Vcl.Forms, Vcl.Themes, Vcl.ComCtrls,
System.Classes, System.SysUtils, System.Types;
type
[ComponentPlatformsAttribute(pidWin32 or pidWin64)]
TMappLabelTest = class(TWinControl)
private
FCaption: TCaption;
procedure SetCaption(const AValue: TCaption);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Caption: TCaption read FCaption write SetCaption;
end;
implementation
constructor TMappLabelTest.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ParentBackground:= True;
Width:= 121;
Height:= 21;
FCaption:= Name; // Name is blank here...
end;
destructor TMappLabelTest.Destroy;
begin
inherited Destroy;
end;
procedure TMappLabelTest.SetCaption(const AValue: TCaption);
begin
FCaption:= AValue;
end;
end.
CodePudding user response:
Override your Component's SetName method. Look at TControl.SetName for an example as to when or when not to set your Caption property.
CodePudding user response:
Result can be achieved by using CreateWnd
procedure. This procedure is invoked before loading design values.
unit MappLabelTest;
interface
uses
Winapi.Windows, Winapi.Messages, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Controls, Vcl.Graphics, Vcl.Forms, Vcl.Themes, Vcl.ComCtrls, System.Classes, System.SysUtils, System.Types;
type
[ComponentPlatformsAttribute(pidWin32 or pidWin64)]
TMappLabelTest = class(TWinControl)
private
FCaption: TCaption;
procedure SetCaption(const AValue: TCaption);
protected
procedure CreateWnd; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Caption: TCaption read FCaption write SetCaption;
end;
implementation
constructor TMappLabelTest.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ParentBackground:= True;
Width:= 121;
Height:= 21;
FCaption:= '';
end;
destructor TMappLabelTest.Destroy;
begin
inherited Destroy;
end;
procedure TMappLabelTest.CreateWnd;
begin
inherited;
FCaption:= Name;
end;
procedure TMappLabelTest.SetCaption(const AValue: TCaption);
begin
FCaption:= AValue;
end;
end.