如何在我的应用程序中手动包括VCL样式? [英] How to manually include a VCL Style in my application?

查看:109
本文介绍了如何在我的应用程序中手动包括VCL样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用条件的应用程序,可以将其编译为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,希望在那里找到一些配置,因为工作项目使用了这种样式,但是没有运气.该文件中的任何地方都不存在该词.

解决方案

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

I have an application which uses conditionals to be able to compile it either as a VCL Forms Application or as a Windows Service Application in Delphi XE2. However, since I have manually altered the project's main source file, the IDE will no longer allow me to make certain modifications using the standard Project Options window. Specifically, I am not able to select VCL Styles to include or implement.

Therefore, I have to implement VCL Styles manually. So, I added the two necessary units Vcl.Themes and Vcl.Styles to my project's initialization unit (which in this case is NOT the same as the project's main unit), and essentially copied the code from a working application into this new application.

Here's the project's main unit:

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.

And then in the project's initialization unit:

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.

The trouble is, when the application runs, I get an error Style 'Carbon' could not be found. simply because this style has not been included and compiled into the application.

How can I compile this style manually into this application so that the VCL Styles can implement it?

PS: The reason why the initialization is in a separate unit is because if the conditionals were implemented inside the application's main unit, the IDE would destroy the code.

EDIT

One thing I have tried: I opened a working project's .dproj file and searched for this style carbon hoping to find some configuration for it there, since the working project used this style, but with no luck. The word does not exist anywhere in that file.

解决方案

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>

这篇关于如何在我的应用程序中手动包括VCL样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆