我认为我已经找到了您所要求的有效解决方案,即在创建组件实例(放置在窗体上或包含其实例的窗体/模块在IDE中打开时)时从项目选项中禁用运行时主题。这不会阻止用户后来手动重新启用运行时主题,但对您可能仍然有用。
顺便说一句,在这种情况下,IOTAProjectOptions似乎没有帮助;看起来需要IOTAProjectResource。
TestComponentU.pas(运行时包的一部分):
unit TestComponentU;
interface
uses
Windows, Classes;
type
ITestComponentDesign = interface
function DisableRuntimeThemes: Boolean;
end;
TTestComponent = class(TComponent)
public
constructor Create(AOwner: TComponent); override;
end;
var
TestComponentDesign: ITestComponentDesign = nil;
implementation
uses
Dialogs;
constructor TTestComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if (csDesigning in ComponentState) and Assigned(TestComponentDesign) and
TestComponentDesign.DisableRuntimeThemes then
ShowMessage('Project runtime themes disabled');
end;
end.
TestComponentRegU.pas
是IDE中安装的设计包的一部分:
unit TestComponentRegU;
interface
procedure Register;
implementation
uses
Windows, Classes, SysUtils, TestComponentU, ToolsAPI;
type
TTestComponentDesign = class(TInterfacedObject, ITestComponentDesign)
public
function DisableRuntimeThemes: Boolean;
end;
procedure Register;
begin
RegisterComponents('Test', [TTestComponent]);
end;
function GetProjectResource(const Project: IOTAProject): IOTAProjectResource;
var
I: Integer;
begin
Result := nil;
if not Assigned(Project) then
Exit;
for I := 0 to Project.ModuleFileCount - 1 do
if Supports(Project.ModuleFileEditors[I], IOTAProjectResource, Result) then
Break;
end;
function GetProjectResourceHandle(const ProjectResource: IOTAProjectResource; ResType, ResName: PChar): TOTAHandle;
var
I: Integer;
ResEntry: IOTAResourceEntry;
begin
Result := nil;
if not Assigned(ProjectResource) then
Exit;
for I := 0 to ProjectResource.GetEntryCount - 1 do
begin
ResEntry := ProjectResource.GetEntry(I);
if Assigned(ResEntry) and (ResEntry.GetResourceType = ResType) and (ResEntry.GetResourceName = ResName) then
begin
Result := ResEntry.GetEntryHandle;
Break;
end;
end;
end;
function DisableProjectRuntimeThemes(const Project: IOTAProject): Boolean;
var
ProjectResource: IOTAProjectResource;
ResHandle: TOTAHandle;
begin
Result := False;
ProjectResource := GetProjectResource(Project);
if not Assigned(ProjectResource) then
Exit;
ResHandle := GetProjectResourceHandle(ProjectResource, RT_MANIFEST, CREATEPROCESS_MANIFEST_RESOURCE_ID);
if Assigned(ResHandle) then
begin
ProjectResource.DeleteEntry(ResHandle);
Result := True;
end;
end;
function TTestComponentDesign.DisableRuntimeThemes: Boolean;
var
Project: IOTAProject;
begin
Project := GetActiveProject;
Result := Assigned(Project) and DisableProjectRuntimeThemes(Project);
end;
initialization
TestComponentDesign := TTestComponentDesign.Create;
finalization
TestComponentDesign := nil;
end.
IOTAProjectOptions
接口暴露。 - Ondrej Kelle