In Delphi 10.4, in a VCL Application, using the OnMessage
event-handler of a TApplicationEvents
component, I increase the font-size of the right-clicked Control:
procedure TformMain.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var
ThisControl: TControl;
begin
if (Msg.Message = WM_RBUTTONDOWN) then
begin
ThisControl := FindDragTarget(Mouse.CursorPos, True);
CodeSite.Send('TformMain.ApplicationEvents1Message: RIGHTCLICK!', ThisControl.Name);
if ThisControl is TLabel then
TLabel(ThisControl).Font.Size := TLabel(ThisControl).Font.Size + 1
else if ThisControl is TCheckBox then
TCheckBox(ThisControl).Font.Size := TCheckBox(ThisControl).Font.Size + 1;
// ETC. ETC. ETC.! :-(
end;
end;
This is an extremely INEFFICIENT way to make this work for all Control Types because I would have to enumerate all existent Control Types, as TControl
does not have a TFont
property.
A better way would be to get the TFont
property of the control without having to ask for the TYPE and then having to TYPECAST the Control.
But HOW?
If you redeclare the type, you get access to the protected properties of the class. Nowadays you do that with an interposer class, but I'm still used to the old ways. You might have to add a check, if it turns out that a particular control bombs, when you do something with the font. It has always worked for me so far.
type
TCrackControl = class(TControl);
procedure TformMain.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var
ThisControl: TCrackControl;
begin
if (Msg.Message = WM_RBUTTONDOWN) then
begin
ThisControl := TCrackControl(FindDragTarget(Mouse.CursorPos, True));
CodeSite.Send('TformMain.ApplicationEvents1Message: RIGHTCLICK!', ThisControl.Name);
If assigned(ThisControl.Font) then
ThisControl.Font.Size := ThisControl.Font.Size + 1;
end;
end;