9
votes

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.

1
From a quick check with Resource Editor, it appears that the styles are added to a resource as VCLSTYLE resource type. This seems to imply you could create a standard .rc file with a list of the style files (the .vsf files from the $(BDS)\Redist\Styles\Vcl folder). I haven't had a chance to test it yet, though. You may also have to register the styles with the style manager in code as well.Ken White
Alternatively, you can use project->resources&images to add carbon.vsf with ID=CARBON, type=VCLSTYLE.Sertac Akyuz
Eureka, thanks @Sertac, that's the best solution I think.Jerry Dodge
Jerry, @Ken's method should also work, the only difference is that you add some 'some.rc' file to the project with project manager and then add a line {$R some.res}.Sertac Akyuz
@JerryDodge, this answer shows how add vcl styles manually using the Resource dialog or editing the .dproj fileRRUZ

1 Answers

15
votes

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>