This shows how to detect if your application is using FireMonkey (FMX) or VCL when using conditional compilation within a Delphi form unit.
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.
Supported Versions of Delphi
The code has been tested on XE2 to XE10.1
+1 this post
I am trying to get this blog listed on DelphiFeeds.com
If you like this post, please +1 for me here on Delphi Feeds
and here on BeginEnd
Thank You !
About The Author
The Usual Suspect
– Scott Hollows –
- Oracle and Delphi software developer.
- Australian Delphi User Group – Western Australia Chief Cat Herder
- Australian Delphi User Group – President
![]() | ![]() | ![]() |
Leave a Reply