How to make Edit1.Font.Charset work with themes (visual styles)
-
28-09-2019 - |
Question
If you have a non-themed, non-Unicode VCL application with an TEdit "TestEdit" and set TestEdit.Font.Charset to RUSSIAN_CHARSET TestEdit displays cyrillic characters. If however you switch the app to use theming this doesn't work anymore. Try the following to see this:
- Create a new VCL app.
- Close the default Unit1 without saving.
- Replace the project source code (Project1.pas) with the code at the bottom of this posting and save as CharsetTest.pas.
- Uncheck runtime theming in the project options.
- Run the program, click the radio buttons, watch the edit box' font.
- Now check runtime theming in the project options or add XPMan to the uses clause.
- Repeat step 5.
My question is: Is there a way to make the app honor the charset even when themed? (Without switching to Unicode.)
program CharsetTest;
uses
Windows,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls,
ExtCtrls;
{$R *.res}
type
TForm1 = class(TForm)
private
CharsetRadioGroup: TRadioGroup;
TestEdit: TEdit;
procedure CharsetRadioGroupClick(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
end;
constructor TForm1.Create(AOwner: TComponent);
begin
inherited CreateNew(AOwner);
BorderWidth := 8;
Caption := 'Charset Test';
ClientHeight := 180;
ClientWidth := 250;
CharsetRadioGroup := TRadioGroup.Create(Self);
CharsetRadioGroup.Name := 'CharsetRadioGroup';
CharsetRadioGroup.Height := 105;
CharsetRadioGroup.Align := alTop;
CharsetRadioGroup.Caption := 'Charset';
CharsetRadioGroup.Parent := Self;
CharsetRadioGroup.Items.Add('Default');
CharsetRadioGroup.Items.Add('Russian');
CharsetRadioGroup.Items.Add('Greek');
CharsetRadioGroup.OnClick := CharsetRadioGroupClick;
TestEdit := TEdit.Create(Self);
TestEdit.Name := 'TestEdit';
TestEdit.Align := alBottom;
TestEdit.Font.Size := 20;
TestEdit.Font.Name := 'Courier New';
TestEdit.Text := 'äöüÄÖÜß';
TestEdit.Parent := Self;
CharsetRadioGroup.ItemIndex := 1;
end;
procedure TForm1.CharsetRadioGroupClick(Sender: TObject);
begin
case CharsetRadioGroup.ItemIndex of
0:
TestEdit.Font.Charset := DEFAULT_CHARSET;
1:
TestEdit.Font.Charset := RUSSIAN_CHARSET;
2:
TestEdit.Font.Charset := GREEK_CHARSET;
end;
end;
var
Form1: TForm1;
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Solution 2
This seems to be an issue with the windows edit control:
Until we upgrade to a recent (read "Unicode enabled") Delphi some of our customers will have to live without themes.
OTHER TIPS
Not a direct answer, but you can use the TMS Unicode Controls to add Unicode support for just the edits, and leave the rest of your application as-is. We did that a few years back to get support in a single combobox, and the overhead wasn't bad.
The original TNT Unicode library that the TMS pack was based on is available here, but TMS isn't expensive, and they've made a bunch of improvements since they bought it.