【问题标题】:How to make Edit1.Font.Charset work with themes (visual styles)如何使 Edit1.Font.Charset 与主题(视觉样式)一起使用
【发布时间】:2010-11-02 19:13:39
【问题描述】:

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

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

【问题讨论】:

  • 字符集 so DOS/Windows 3.1/Windows 9x...
  • 我知道。但是该应用程序可以运行,我们目前不想进行 Unicode 步骤。

标签: delphi themes character-encoding vcl visual-styles


【解决方案1】:

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

TMS 包所基于的原始 TNT Unicode 库在 here 可用,但 TMS 并不昂贵,而且他们在购买后进行了一系列改进。

【讨论】:

  • 我们将(大部分)数据存储为字符串[N]。这些不能存储用户可以在 Unicode 编辑中输入的所有字符。 (或者他们可以吗?)您是如何/将如何处理这个问题的(不涉及对这样一个字符串的每个分配)?
  • 不,string[N] 不能包含 Unicode 可以包含的所有内容。我将从 TTntEdit 下降并用 AnsiString 替换 Text 属性。在 GetText/SetText 方法中,转换为/从 Unicode。由于听起来您在非俄语系统上使用俄语,您应该使用 TntSystem.pas 的 StringToWideStringEx/WideStringToStringEx,并根据字体的字符集更改代码页。例如,RUSSION_CHARSET 将映射到 1251。您仍然可以键入应用程序不支持的字符,所以这有点用户小心。您可以通过挂钩 WM_PASTE 和 WM_CHAR 来防止这种情况发生。
  • 谢谢。我得玩一会儿看看有什么可能。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2010-12-23
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    相关资源
    最近更新 更多