Conditional compilatation to detect VCL or FireMonkey in a Delphi Form by Scott Hollows

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,438
Credits
573
Conditional compilatation to detect VCL or FireMonkey in a Delphi Form
Scott Hollows - 13/Oct/2016
[SHOWTOGROUPS=4,20]
This shows how to detect if your application is using FireMonkey (FMX) or VCL when using conditional compilation in a Delphi form

UPDATE – a better solution has been provided by a Rudy Velthuis. I recommend to use that instead of my original post. Ive provided examples based on his feedback. Thanks Rudy
Код:
   -- Rudy Velthuis' updated solution
{$IF not declared(FireMonkeyVersion)}
     ShowMessage('VCL');
{$ELSE}
     ShowMessage('FMX');
{$IFEND}
-- Making it even simpler, this is the same code
-- with the "not" removed so the logic is flipped

{$IF declared(FireMonkeyVersion)}
  ShowMessage('FMX');
{$ELSE}
  ShowMessage('VCL');
{$IFEND}

-- a handy function that can be used in regular code
-- rather than conditional compilation
function IsFMX : boolean;
begin
 {$IF declared(FireMonkeyVersion)}
   result := TRUE;
 {$ELSE}
   result := FALSE;
 {$IFEND}
end;

The following is my original post
Код:
{$IF FMX.Types.FireMonkeyVersion >= 0} // if FireMonkey
     DoSomethingFMX;
{$ELSE}                  // its not FMX, so it must be VCL
     DoSomethingVCL;
{$ENDIF}

Can I Reverse the logic ?
No, reversing the logic does not work. The reason for this is we are relying on the behaviour of Delphi conditional compile to return FALSE if the variable in the {$IF} does not exist.

To clarify … this works correctly
Код:
{$IF FMX.Types.FireMonkeyVersion >= 0} // if FireMonkey
     ShowMessage ('FMX 1');
{$ELSE} // its not FMX, so it must be VCL
     ShowMessage ('VCL 1');
{$ENDIF}

But this does NOT work
Код:
     // DO NOT USE THIS - IT DOES NOT WORK
{$IF FMX.Types.FireMonkeyVersion < 0} // if VCL
     ShowMessage ('VCL 2');
{$ELSE}            // its not VCL, so it must be FMX
     ShowMessage ('FMX 2');
{$ENDIF}

What if I mix and match VCL and FMX ?
Although not officially supported, it is possible for a Delphi application to use both FireMonkey and VCL units.

It is possible to embed a FireMonkey form in a VCL application and vice versa using unsupported techniques. However, I haven’t tested those scenarios with my conditional compilation code. Maybe it will work, maybe not. Ill leave it to you to look into it if you are interested. Please post a comment here if you find anything interested.


[/SHOWTOGROUPS]