delphidelphi-xe2vcl-styles

How to manually include a VCL Style in my application?


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.


Solution

  • 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.