How to convert TStringGrid from Delphi 7 to Delphi XE
Asked Answered
B

2

5

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.

Bloxberg answered 21/10, 2014 at 17:0 Comment(0)
W
7

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.

Wildee answered 21/10, 2014 at 17:32 Comment(4)
Doh... :) Thank You very much, can't vote for answer now (no reputation), but i do it in future. Best regards.Bloxberg
So problem was really with TStringGrid?Sagittate
@FreeConsulting Yes, it's TStringGrid that appears to be at fault here. Probably some lame painting code.Wildee
Beyond the font selection issue, the problem with rendering when using the MS Sans Serif font appears to lie with TCanvas. TStringGrid uses TCanvas for it's text drawing and so shares the problem, as does the default drawing code in an owner draw TComboBox (for another example).Uturn
U
1

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.

Uturn answered 21/10, 2014 at 21:34 Comment(7)
Actually, for me anyway, a new project in XE7 uses MS Sans Serif. Try it. Maybe it's just me. XE7 is where I did all my testing. This doesn't tally with the info in the Q. Not sure why.Wildee
@David - I updated the answer with info that might shed some light. Do you have a MS Shell Dlg 2 value in FontSubstitutions that specifies MS Sans Serif ? If not, and in particular if this entry or the entire key is missing on your system, then it might be that the $IF DEFINED(CLR) code is actually active in the IDE (or that the XE7 InitDefFontData procedure has changed yet again with some new condition(s) that might result in MS Sans Serif being chosen).Uturn
Perhaps you already followed the advice in that article you linked to. It's all kind of irrelevant anyway. The point is that the control is no good with that font.Wildee
Argh. It could be the design time package that I have installed. I'll update.Wildee
@David - nope, this was in a Windows 7 VM with an install of Delphi (7 and XE4 as it happened). The only non-vanilla aspects being the installation of GExperts, Delphi Speedup, DDevExtensions etc etc. But I don't believe these play any part - the behaviour I see is consistent with the InitDefFontData code (My MS Shell Dlg 2 setting is "Tahoma", which is the default setting for Windows 7 as documented by Microsoft).Uturn
Also @David :). The problem goes deeper than TStringGrid - TCanvas doesn't seem to apply the font properly, although I haven't figured out where the problem lies. TStringGrid is affected because it uses TCanvas, but as far as I can tell this problem will affect any control that uses TCanvas for text output.Uturn
For another example of the problem: Default drawing in an owner draw TComboBox.Uturn

© 2022 - 2024 — McMap. All rights reserved.