4

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

任何帮助、样品或建议。

先感谢您。

4

1 回答 1

3

这是一个使用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.
于 2012-09-03T08:11:20.557 回答