在 Delphi 10.4 中,在 VCL 应用程序中,使用组件的OnMessage
事件处理程序TApplicationEvents
,我增加了右键单击控件的字体大小:
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;
这是一种非常低效的方法来使所有控件类型都可以使用,因为我必须枚举所有现有的控件类型,因为TControl
没有TFont
属性。
更好的方法是获取TFont
控件的属性,而不必询问类型,然后必须对控件进行 TYPECAST。
但是如何?