如何在我的应用程序中手动包含VCL样式?

Jer*_*dge 6 delphi delphi-xe2 vcl-styles

我有一个应用程序,它使用条件来编译它作为VCL Forms应用程序或Delphi XE2中的Windows服务应用程序.但是,由于我手动更改了项目的主源文件,因此IDE将不再允许我使用标准的"项目选项"窗口进行某些修改.具体来说,我无法选择VCL样式来包含或实现.

因此,我必须手动实现VCL样式.所以,我添加了两个必要的单元Vcl.ThemesVcl.Styles我的项目的初始化单元(在这种情况下与项目的主单元不同),并且基本上将工作应用程序中的代码复制到这个新的应用程序中.

这是该项目的主要单位:

program MyServiceApplication;

uses
  uMyService in 'uMyService.pas' {MyService: TService},
  uMyServiceMain in 'uMyServiceMain.pas',
  uMyServiceInit in 'uMyServiceInit.pas',
  uMyServiceTest in 'uMyServiceTest.pas' {frmMyServiceTest};

{$R *.RES}

begin
  RunMyService;
end.
Run Code Online (Sandbox Code Playgroud)

然后在项目的初始化单元中:

unit uMyServiceInit;

interface

uses
{$IFDEF TESTAPP}
  Vcl.Forms,
  Vcl.Themes,
  Vcl.Styles,
  uMyServiceTest,
{$ELSE}
  Vcl.SvcMgr,
  uMyService,
{$ENDIF TESTAPP}
  uMyServiceMain
  ;

procedure RunMyService;

implementation

procedure RunMyService;
begin
{$IFDEF TESTAPP}
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  TStyleManager.TrySetStyle('Carbon'); //<--- WILL NOT RUN - STYLE DOES NOT EXIST
  Application.Title := 'My Windows Service Application';
  Application.CreateForm(TfrmMyServiceTest, frmMyServiceTest);
{$ELSE}
  if not Application.DelayInitialize or Application.Installing then
    Application.Initialize;
  Application.CreateForm(TMyService, MyService);
{$ENDIF TESTAPP}
  Application.Run;
end;

end.
Run Code Online (Sandbox Code Playgroud)

问题是,当应用程序运行时,我Style 'Carbon' could not be found.只是因为没有包含这个样式并将其编译到应用程序中而得到错误.

如何手动将此样式编译到此应用程序中,以便VCL样式可以实现它?

PS:初始化在一个单独的单元中的原因是因为如果条件在应用程序的主单元内实现,IDE将破坏代码.

编辑

我尝试了一件事:我打开了一个工作项目的.dproj文件并搜索了这种样式,carbon希望在那里找到一些配置,因为工作项目使用了这种风格,但没有运气.该文件中的任何位置都不存在该单词.

Ser*_*yuz 13

TStyleManager正在从可执行文件的"VCLSTYLE"资源部分加载可用样式(除非您设置TStyleManager.AutoDiscoverStyleResources为false).资源是您的方案中缺少的资源.基本上,有三种方法可以将您的样式添加为exe中的资源.

  • 通过'Project' - >'Resources and Images ..'菜单.单击对话框中的"添加"按钮添加样式,将其类型设置为"VCLSTYLE",将标识符设置为"CARBON".

  • 正如在评论中提到的,通过.rc文件.这是一个文本文件,每个样式(和/或其他资源)可以包含一行.就像

    CARBON VCLSTYLE "C:\..\RAD Studio\9.0\Redist\Styles\Vcl\Carbon.vsf"
    Run Code Online (Sandbox Code Playgroud) (you can use relative paths if it is feasible). Let's name the file 'styles.rc', add the file to the project through project manager (or use brcc32.exe in the bin folder to compile it to a .res file) and then add a {$R styles.res}你的单位一样.

  • 正如RRUZ他的回答中所说,他通过编辑.dproj文件将评论链接到该问题.在<PropertyGroup Condition="'$(Base)'!=''">键下面,添加一个VCL_Custom_Styles条目(他的例子包括几个样式): TStyleManager在函数体内部形成,但我想知道是否有一种更简洁的方法.