Search code examples
delphithemescharacter-encodingvclvisual-styles

How to make Edit1.Font.Charset work with themes (visual styles)


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:

  1. Create a new VCL app.
  2. Close the default Unit1 without saving.
  3. Replace the project source code (Project1.pas) with the code at the bottom of this posting and save as CharsetTest.pas.
  4. Uncheck runtime theming in the project options.
  5. Run the program, click the radio buttons, watch the edit box' font.
  6. Now check runtime theming in the project options or add XPMan to the uses clause.
  7. 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

  • 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.