Sunday, September 4, 2011

Useful tips for fireMonkey and Delphi XE2

Originally Posted on  by hsauro

Here is a list of useful tips for using fireMonkey.

Update (7 Sept 2011): A report from Embarcadero indicates that Anchor support will eventually be provided in FireMonkey.

1. Fonts too fuzzy? You may find that font rendered in controls such as TMemo or TEdit are a bit fuzzy, particularly on a white background. To improve font quality, follow a suggestion from Jermey North:

This could be due to the Direct 2D canvas. Try the GDI+ plus canvas and see if this makes the fonts clearer.

Before Application.Initialize in the project file, add this line:

FMX.Types.GlobalUseDirect2D := False;

You need to put FMX.Types and FMX.Canvas.GDIP in the uses clause as

Works for me. Update 12 Sept: For an even better solution see tip #11.

2. Qualified Font Styles. From Jon Souter, can’t get this to compile?


Use this instead (from Jeremy North and Chris Rolliston):


Styles must now be qualified.

3. Font Size Differences: In VCL, font sizes are specified using points, eg 10 point, that is 72 points per inch. In fireMonkey font sizes are specified using device-independent pixels (DIPs), which at set at 96 per logical inch. A font set at 10 point in foreMonkey will therefore be slightly smaller compared so the same size in a VCL application.

4. Mouse Position: In Windows world we were used to getting the mouse position using GetCursorPos(). For crossplatform, we have to use a different approach. Lukily there is a platform object from which we can obtain the mouse coordinates (from Ralph Wesseling):

uses FMX.Platform;

p : TPointF

p := Platform.GetMousePos;

5. Unit Scope Helper: Drs Bob’s has release a simple browser tool to help you convert VCL unit names to their equivalent FMX names.

6. Multiple Select: If you select multiple controls on a form, the usual selection cues are not visible. However the group of controls is selected, you can move them about as if there were a group.

7. OnKeyDown/KeyUp in Forms: Unlike a VCL form which has over 40 events that one can tap into, a FM form has only 9 events exposed. In particular it has no OneKeyDown or OnKeyUp exposed. In addition, FM forms have no KeyPreview. As recently mentioned by Chris Rolliston, “Looking at the source, I’ve found the situation is in effect an enforced KeyPreview=True – the only window from an OS POV is the form itself, which
then passes on key events to the controls. While there are no OnKeyDown and OnKeyUp events exposed by TForm, you can just override the form’s KeyDown and KeyUp methods:”

  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure KeyDown(var Key: Word; var KeyChar: Char; Shift:
TShiftState); override;


procedure TForm1.KeyDown(var Key: Word; var KeyChar: Char; Shift:
  if Key = vkEscape then

Rolliston goes on to suggest that adding KeyPreview to FM forms should be straightforward and supplies the necessary code to accomplish this.


8. Where are my Anchors gone? Possibly one of the biggest surprises for many was the absence of the anchor property in FM controls. This has been discussed before and refer the reader to Update 2 on this page.

9. Glyphs on buttons Question (Gaetan Maerten): I have made a simple app with an OK and Cancel button, and now I am trying to find how I can add the standard OK and Cancel glyphs?

Answer (Chris Rolliston): With the TButton object selected, add a TImage (*not* a TImageControl) to it, assign the image’s Bitmap property, and set the image’s HitTest property to False so that any mouse events float through to the button. You also might want to use a TLabel control for the button’s text instead of using the button’s own Text property, so as to make sure it all aligns nicely.

10. Writing text on a TImage (GIAN 55)

Code to write text on a TImage canvas:

procedure TForm2.Button1Click(Sender: TObject);
LRect: TRectF;
myimage.Bitmap.LoadFromFile(CurrDir+'\bg.bmp');  // delphi need a brackground to write on

LRect.Create(0, 0, 300, 300);
myimage.Bitmap.Canvas.Font.Family:='Times New Roman';
myimage.Bitmap.Canvas.FillText(LRect, 'Delphi XE2 for iOS', True, 255, [],TTextAlign.taLeading,TTextAlign.taCenter);

11. More on Fuzzy Font. Not happy with the fuzzy fonts rendered by fireMonkey? Want to use cleartype? If so consider using a replacement canvas for fireMonkey by Mattias Anderson. Here is the contents of the readme file:



FMX.Canvas.VPR is a new FireMonkey TCanvas implementation that uses the
open source polygon rasterizer VPR for all of its rendering tasks.

How to use:

Add FMX.Canvas.VPR as the first unit in the project file of your
FireMonkey project.


- perfect antialiasing (superior to GDI+ and D2D);
- high performance;
- minimal API dependencies;
- support for gamma correction;
- support for cleartype font rendering;
- can be easily ported to other platforms.

Copyright (C) 2011 by Mattias Andersson

12. FastMM for Delphi XE2. For user of FastMM, Pierre Le Riche has released a version for Delphi XE2. FastMM is a memory manager and debugger tool. For more information consult this blog and this stackoverflow question.

13. Using Lua with Delphi XE2Lua is a high performance, light-weight and easy to use scripting language that games writers often use as a lightweight embedded language to develop the game’s AI for example. Lua4Delphi allows someone to use Lua from within a Delphi application. Delphi3Lua provides full Lua integration.

14. Creating Components at Runtime. Here is a simple example from Peter Soderman for creating a string grid at runtime:

procedure TForm1.Button1Click(Sender: TObject);
  p: TPanel;
  sg: TStringGrid;
  p := TPanel.Create(Self);
  sg := TStringGrid.Create(Self);
  sg.Parent := p;
  p.Parent := Self;
  sg.Align := TAlignLayout.alClient;
  p.Align := TAlignLayout.alLeft;

15. Adding a column to StringGrid. Peter Soderman reports how to add a column to a stringgrid at runtime as well as setting a cell’s text:


To set the text in a given cell:

StringGrid1.Cells[aCol,aRow] := ‘Hello!’;

16. How do I set the colour of the text of a TLabel Set the color of the font in the style editor but other on attributes in the property editor.

No comments: