Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert TStringGrid from Delphi 7 to Delphi XE

Just for test how hard to convert my Delphi 7 program to Delphi XE 5 i wrote simple application on Delphi 7 - placed TStringGrid on Form, and added code on form create:

procedure TFMain.FormCreate(Sender: TObject);
begin
  With StringGrid1 do
    begin
      Cells[0,0]:='čęжэ€';
    end;
end;

(actually it wrote as Cells[0,0]:='ce??€', but i expected that). Compile, build, run, no Unicode, of cause. Then reopened project in Delphi XE 5, changed line again to Cells[0,0]:='čęжэ€', compile, build, run - and no Unicode (got something like čę|||) ! That was strange to me. New project build from zero on Delphi Xe 5 with the same code, same TStringGrid is working as expected. I know here is some simple trick, maybe some change in project settings, but i can't google it... Maybe someone can help ?

Best regards.

like image 616
Nihila Avatar asked Oct 21 '14 17:10

Nihila


2 Answers

The default font used by Delphi 7 is MS Sans Serif. When you use this font under Unicode Delphi, the string grid control appears not to draw text correctly with that font. Many other controls will draw your text correctly in that font. But for some reason the string grid control cannot manage to do so.

When you upgrade an old project to XE5 you inherit that Delphi 7 default. When you create a new project in XE5 the default font is different, Tahoma I think, and the string grid painting correctly shows your Cyrillic in that font.

You can work around this problem by using a different font like Tahoma or Segoe UI. You surely don't want to be using MS Sans Serif anyway. A list view in report view style would be another good option. Not least because it is the native platform control.

I must admit that I do not really understand why the string grid control is not behaving better. It would be great if somebody else could shed some light on this.

like image 149
David Heffernan Avatar answered Oct 14 '22 23:10

David Heffernan


As David mentions, the issue is the font used in your string grid. However, it is not strictly accurate to say that the default font in Delphi is MS Sans Serif. It used to be MS Sans Serif but was changed (in Delphi 2006) to Tahoma.

You can see how a particular version of Delphi chooses the default font by inspecting the source of the Graphics unit in the RTL source of that particular Delphi version (since the IDE is built using that code). Specifically the InitDefFontData procedure (and, in older versions of Delphi, the DefFontData record).

As of (at least) Delphi XE4 the default Tahoma font will be replaced by any setting for a font substitution for a value identified as MS Shell Dlg 2, as set in the registry:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes\

NB - from inspecting the code in XE4 it is possible that if this key does not exist or cannot be accessed, or if there is no substitution entry for MS Shell Dlg 2 font, then MS Sans Serif will still be used. It is hard to tell since this is the behaviour when "CLR" is defined, which should not be the case any more since Delphi no longer supports .NET and the IDE is presumably not compiled with CLR defined, but there is no way to be certain simply from inspecting the code what conditional defines might be applied when the IDE is compiled.

However, whichever font the IDE is using and however it is choosing that font, this affects only new forms created in the IDE.

For existing forms, as in this case, the issue is not with TStringGrid as such but rather the fact that you created the project in a version of Delphi which applied a default font which did/does not support Unicode.

The act of opening the project in the newer version of Delphi did not change the font used in your form(s), so the form saved in Delphi 7 using the MS Sans Serif font is still using that font when opened in Delphi XE5.

The TStringGrid control is then using the MS Sans Serif font because this is the font set on the form, and the default for controls on a form is to use their parent control font.

i.e. This specific instance of TStringGrid is using MS Sans Serif because the form on which it is placed is (still) using MS Sans Serif.

In such cases you should change the form font to Tahoma or a. n. other suitable, Unicode enabled font.

All controls on the form still set to use their parent control's font will then adopt this font also. When performing this on an actual application you may find some controls with ParentFont set FALSE which will need to be addressed individually and that even where font settings are being "inherited" your form designs may need further work to tidy things up due to changes in appearance resulting from the change of font.

Note that even this change to Tahoma has been overtaken by changes in Windows itself, and if you wish to apply some other default font (in new forms/projects) you may find useful information here.

like image 43
Deltics Avatar answered Oct 14 '22 22:10

Deltics