gpt4 book ai didi

delphi - 如何制作一个自定义组件来检测表单上是否有新组件

转载 作者:行者123 更新时间:2023-12-03 15:30:58 24 4
gpt4 key购买 nike

我在delphi2009中创建了自定义组件SkinMgr和SkinPanel。我希望这两个组件自动链接在一起,即使 SkinMgr 位于 DataModule 或其他形式的任何位置。

任何帮助、示例或建议。

提前谢谢您。

最佳答案

这是使用 TActionList 后代和 TCustomActionManager 进行的快速测试:当在设计时将 TActionListEx 组件的实例拖放到表单上时,它会枚举当前项目的所有模块并尝试查找具有 TCustomActionManager 实例的表单/数据模块。如果找到,则会将新创建的 TActionListEx 添加到其 LinkedActionLists 集合中。

运行时包:

unit TestComponents;

interface

uses
System.Classes, Vcl.ActnList;

type
TActionListEx = class(TActionList)
public
constructor Create(AOwner: TComponent); override;
end;
TNotifyProc = procedure(Sender: TObject);

var
CreateNotify: TNotifyProc = nil;

implementation

{ TActionListEx }

constructor TActionListEx.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if (csDesigning in ComponentState) and Assigned(CreateNotify) then
CreateNotify(Self);
end;

end.

设计时包:

unit TestComponentsDesign;

interface

procedure Register;

implementation

uses
System.Classes, System.SysUtils,
Vcl.ActnMan,
ToolsAPI,
TestComponents;

procedure CreateNotifyProc(Sender: TObject);
var
ActionList: TActionListEx absolute Sender;
ModuleServices: IOTAModuleServices;
ActiveProject: IOTAProject;
I, J: Integer;
ModuleInfo: IOTAModuleInfo;
Module: IOTAModule;
Editor: IOTAFormEditor;
RootComponent: IOTAComponent;
Component: INTAComponent;
ActionManager: TCustomActionManager;
ActionListItem: TActionListItem;
begin
if not (Sender is TActionListEx) or not (csDesigning in ActionList.ComponentState) then
Exit;

if not Supports(BorlandIDEServices, IOTAModuleServices, ModuleServices) then
Exit;

ActiveProject := ModuleServices.GetActiveProject;
if not Assigned(ActiveProject) then
Exit;

for I := 0 to ActiveProject.GetModuleCount - 1 do
begin
Module := nil;
Editor := nil;
RootComponent := nil;

ModuleInfo := ActiveProject.GetModule(I);
if Assigned(ModuleInfo) and (ModuleInfo.FormName <> '') then
Module := ModuleInfo.OpenModule;

if Assigned(Module) then
for J := 0 to Module.ModuleFileCount - 1 do
if Supports(Module.ModuleFileEditors[J], IOTAFormEditor, Editor) then
Break;

if Assigned(Editor) then
RootComponent := Editor.GetRootComponent;

if Assigned(RootComponent) then
for J := 0 to RootComponent.GetComponentCount - 1 do
if Supports(RootComponent.GetComponent(J), INTAComponent, Component) and (Component.GetComponent is TCustomActionManager) then
begin
ActionManager := TCustomActionManager(Component.GetComponent);
ActionListItem := ActionManager.LinkedActionLists.Add as TActionListItem;
try
ActionListItem.ActionList := ActionList;
Editor.MarkModified;
Exit;
except
ActionListItem.Free;
raise;
end;
end;
end;
end;

procedure Register;
begin
RegisterComponents('Test', [TActionListEx]);
CreateNotify := CreateNotifyProc;
end;

initialization

finalization
CreateNotify := nil;

end.

关于delphi - 如何制作一个自定义组件来检测表单上是否有新组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12241585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com