25

Prior to Delphi XE2, we have VCL only to create GUI apps. Delphi XE2 states that:

Caution: FireMonkey (FMX) and the Visual Component Library (VCL) are not compatible and cannot be used in the same project or application. That is, an application must be exclusively one or the other, either FireMonkey or VCL. The incompatibility is caused by framework differences between FireMonkey (FMX) and VCL.

My application is a pure VCL application that is built with runtime packages. All VCL forms are stored in a runtime package. If I am going to create a FireMonkey form and store in a package, do I have any chance to instantiate this FireMonkey form in my VCL application at runtime? So I may enjoy the 3D or HD effects of FireMonkey.

Shannon Matthews
  • 9,649
  • 7
  • 44
  • 75
Chau Chee Yang
  • 18,422
  • 16
  • 68
  • 132
  • This is an interesting question, I guess it should be possible(I haven't tried XE2 yet), tho' I've heard it's pretty buggy, I suggest you stay put until XE3/4 -- if you don't want your customer to have bad experiences. –  Sep 06 '11 at 05:07
  • 1
    I doubt this will ever be feasible, and why would you do it? The VCL is Windows-specific raster graphics with controls with Windows handles, and FireMonkey is cross-platform vector graphics with controls with no handles. The application framework for both is quite different. Choose one or the other. – Misha Sep 06 '11 at 05:16
  • 2
    @Dorin Duminica: Bugs in the new cross platform FireMonkey library (which is what I've mainly heard bug reports about) were somewhat inevitable. From what I can tell the Win32 compiler and VCL are as robust as ever. Sure I'd wait an update pack or two before releasing a FireMonkey app to your customers but personally I'll be using this time to get up to speed with FireMonkey while using the latest and greatest Win32 compiler and VCL for my existing applications. – LachlanG Sep 06 '11 at 05:19
  • 3
    I'm amazed how fast the "HD" marketing buzzword spreads out ... – jpfollenius Sep 06 '11 at 06:13
  • ISTM that this is decided on a unit by unit base. I don't see why you couldn't have both kinds of units in the same app, i.e. one that defines an FMX form another one that defines a VCL form. Using them together, at the same time, might be a problem though. Now, **designing** might be a problem, perhaps. – Rudy Velthuis Sep 06 '11 at 06:26
  • How about global classes like TApplication? My guess is that FireMonkey has it's own TApplication instance. They might not mix. – Lars Truijens Sep 06 '11 at 09:23
  • @Lars, that would be my guess as well. In the days of Delphi.NET this was also the case for WinForms applications (which used System.Windows.Forms.Application rather than the VCL TApplication class). You had to choose one or the other because the underlying application framework that hooked up the forms was different. – Misha Sep 06 '11 at 10:35
  • Giving this a try in XE7, not so much luck. Sure, I can create and use a FMX form in a VCL application. A very basic empty one. But then, upon closing my application. I receive endless access violations and other exceptions, as well as major memory leaks. – Jerry Dodge Nov 23 '15 at 03:40

6 Answers6

25

This is perfectly possible, since the FMX form can be assigned to a panel.

See this blog article for details:

Just create a new FireMonkey form (2D or 3D, doesn't matter) save it and then add it to your VCL application (just accept the warning). You can create your FMX form instance somewhere and just show it - no problem. But what if you want to create some nice control with animations or something and embed it into your existing VCL form? Well, put a TPanel on your VCL form and include the brandnew unit DSharp.Windows.FMXAdapter.pas after the Vcl.ExtCtrls. Then just create your FMX form somewhere and assign it to the new Form property of your Panel - and boom, there you go.

In fact, the FMXAdapter.pas code is very short:

procedure TPanel.Resize;
begin
  inherited;
  ResizeForm();
end;

procedure TPanel.ResizeForm;
begin
  if Assigned(FForm) then
    Platform.SetWindowRect(FForm, RectF(BorderWidth, BorderWidth,
      ClientWidth + BorderWidth, ClientHeight + BorderWidth));
end;

procedure TPanel.SetForm(const AForm: TCommonCustomForm);
begin
  FForm := AForm;  
  FForm.BorderIcons := [];
  FForm.BorderStyle := TFmxFormBorderStyle.bsNone;
  ResizeForm();
  FForm.Visible := True;
  Winapi.Windows.SetParent(FmxHandleToHWND(FForm.Handle), Handle);
end;
Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • As stated by this [blog review of XE2] article, even the IDE seems to embed some FireMonkey components inside its VCL forms: " Spy++ says the whole thing is only one window, of class `FMTStyleDesigner`." So it's definitively a YES. – Arnaud Bouchez Sep 07 '11 at 06:26
  • 1
    @Arnaud: the link doesn't appear to work - was it meant to go to http://itinerantdeveloper.blogspot.com/2011/09/c-builder-xe2.html ? – David Sep 15 '11 at 11:13
6

For a more modern approach try TFireMonkeyContainer. It's an open-source VCL component you can place on a VCL form, and it can host / embed a FireMonkey form inside it.

A FMX form embedded in a VCL form using TFireMonkeyContainer

A FMX form embedded in a VCL form using TFireMonkeyContainer

Details here: introduction article, and followup with some bugfixes and more features. Find a link to the Google Code page and source here.

It works with XE2 and above, including XE4+ (where FMX had some large changes.)

Disclaimer: this is my component. (I created it to solve this very problem.) It's MPL-licensed so can be freely used even in commercial apps. Hope it helps you and makes your life easier!

David
  • 13,360
  • 7
  • 66
  • 130
  • I used this in Delphi XE7 and it worked beautifully. Now that I get the latest version for Delphi 10 Seattle, sadly it doesn't work right. It appears to work at first, then I click on something and everything freezes up. – Jerry Dodge Apr 12 '16 at 22:15
  • So far, it appears to be related to clicking on things which trigger a `TChangeTabAction`. Clicking elsewhere **so far** appears to work fine. Will test more. – Jerry Dodge Apr 12 '16 at 22:27
  • That's exactly what's causing it. This form works perfectly when placed in a pure FMX application, but inside this container, triggering those `TChangeTabAction`s causes everything to freeze. – Jerry Dodge Apr 12 '16 at 22:29
  • Asked new question: http://stackoverflow.com/questions/36585316/fmx-form-in-vcl-app-via-tfiremonkeycontainer-application-freezing – Jerry Dodge Apr 12 '16 at 22:45
  • @JerryDodge The issue is solved. Please try the latest revision on github. – David Apr 14 '16 at 18:31
3

I haven't tried myself but I know of one confirmed way to mix VCL and FireMonkey in the same application using RemObjects Hydra and one unconfirmed report that you can have FireMonkey forms in a VCL application at Delphi Sorcery.

LachlanG
  • 4,047
  • 1
  • 23
  • 35
3

It is no big deal actually, to use both kinds of forms. But be sure that you instantiate them without any owner, as the owner types are not compatible.

I just created two simple test projects, Project46 and Project47. Project46 contained Unit25 with a VCL Form. Project 47 contained Unit26 with an FMX form. On Form25, I added a button, with the OnClick handler:

procedure TForm25.Button1Click(Sender: TObject);
var
  F: Unit26.TForm26;
begin
  F := TForm26.Create(nil);
  try
    F.ShowModal;
  finally
    F.Free;
  end;
end;

This worked as expected. But the forms should otherwise be strictly indepedent of each other. Only types they have in common can be exchanged.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
1

Download and install MonkeyMixer. Load up your VCL project (or create a new one), right-click on it in the Project Explorer, then you'll see either "Switch to FireMonkey". Click that and you'll be able to add FireMonkey forms seamlessly to your VCL project. You can then switch back to VCL mode (if you want to add VCL Forms/Frames) the same way, except the menu entry will say "Switch to VCL".

Enjoy!

LaKraven
  • 5,804
  • 2
  • 23
  • 49
0

All problems of the FMX+VCL mix can be resolved on the WinAPI level. But personally I don't recommend you to use such mix in the real world application.

Torbins
  • 2,111
  • 14
  • 16