9

我有一个应用程序,它使用条件能够将其编译为 VCL 表单应用程序或 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.

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

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.

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

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

PS:之所以在单独的单元中进行初始化,是因为如果条件是在应用程序的主单元中实现的,IDE 会破坏代码。

编辑

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

4

1 回答 1

16

TStyleManager is loading available styles from a 'VCLSTYLE' resource section of the executable (unless you set TStyleManager.AutoDiscoverStyleResources to false). The resource is what's missing in your scenario. Basically, there are three ways to add your style as a resource in the exe.

  • Through the 'Project' -> 'Resources and Images..' menu. Add the style clicking the 'Add' button in the dialog, set its type to 'VCLSTYLE' and identifier to 'CARBON'.

  • As Ken mentioned in the comment to the question, through an .rc file. This is a text file which can contain a line per style (and/or other resources). Like

    CARBON VCLSTYLE "C:\..\RAD Studio\9.0\Redist\Styles\Vcl\Carbon.vsf"
    (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} line to your unit.

  • As RRUZ told in his answer that he linked in a comment to the question, by editing the .dproj file. Under the <PropertyGroup Condition="'$(Base)'!=''"> key, add a VCL_Custom_Styles entry (his example includes several styles):

    <VCL_Custom_Styles>&quot;Amakrits|VCLSTYLE|$(PUBLIC)\Documents\RAD Studio\9.0\Styles\Amakrits.vsf&quot;;&quot;Amethyst Kamri|VCLSTYLE|$(PUBLIC)\Documents\RAD Studio\9.0\Styles\AmethystKamri.vsf&quot;;&quot;Aqua Graphite|VCLSTYLE|$(PUBLIC)\Documents\RAD Studio\9.0\Styles\AquaGraphite.vsf&quot;</VCL_Custom_Styles>

于 2012-09-25T01:36:11.537 回答