如何使 Edit1.Font.Charset 与主题(视觉样式)一起使用

发布于 2024-09-30 15:55:02 字数 2100 浏览 7 评论 0原文

如果您有一个带有 TEdit“TestEdit”的非主题、非 Unicode VCL 应用程序,并将 TestEdit.Font.Charset 设置为 RUSSIAN_CHARSET TestEdit 将显示西里尔字符。但是,如果您将应用程序切换为使用主题,这将不再起作用。请尝试以下操作来查看:

  1. 创建一个新的 VCL 应用程序。
  2. 关闭默认的Unit1而不保存。
  3. 将项目源代码 (Project1.pas) 替换为本文底部的代码,并保存为 CharsetTest.pas。
  4. 取消选中项目选项中的运行时主题。
  5. 运行程序,单击单选按钮,观察编辑框的字体。
  6. 现在检查项目选项中的运行时主题或将 XPMan 添加到使用子句中。
  7. 重复步骤 5。

我的问题是:有没有办法让应用程序即使在主题化时也遵循字符集? (无需切换到 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雄赳赳气昂昂 2024-10-07 15:55:02

不是直接答案,但您可以使用 TMS Unicode 控件 添加 Unicode 支持编辑,并将应用程序的其余部分保留原样。几年前,我们这样做是为了在单个组合框中获得支持,而且开销还不错。

TMS 包所基于的原始 TNT Unicode 库可在此处,但 TMS 并不昂贵,而且自从购买以来他们已经做了很多改进。

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.

╭⌒浅淡时光〆 2024-10-07 15:55:02

这似乎是 Windows 编辑控件的问题:

在我们升级到最新的(阅读“启用 Unicode”)Delphi 之前,我们的一些客户将不得不忍受没有主题的生活。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文