2010-11-02 67 views
1

如果您有一個帶TEdit「TestEdit」的非主題,非Unicode VCL應用程序並將TestEdit.Font.Charset設置爲RUSSIAN_CHARSET TestEdit顯示西里爾文字符。但是,如果您切換應用程序使用主題,這不再工作。請嘗試以下操作以查看此信息:如何使Edit1.Font.Charset與主題(視覺樣式)一起工作

  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. 
+1

字符集* *所以* DOS/Windows 3.1/Windows 9x ... – 2010-11-02 19:15:25

+0

我知道。但該應用程序的作品,我們現在不想做的Unicode步驟。 – 2010-11-02 19:17:12

回答

1

不是一個直接的答案,但你可以使用TMS Unicode Controls添加Unicode的支持只是編輯,並留下您的應用程序的其餘原樣。幾年前,我們通過一個組合框來獲得支持,開銷並不差。

TMS包基於的原始TNT Unicode庫可用here,但TMS並不昂貴,並且自從購買TMS後,他們進行了一系列改進。

+0

我們將(大部分)我們的數據存儲爲字符串[N]。這些不能存儲用戶可以在Unicode編輯中輸入的所有字符。 (或者他們可以嗎?)你是如何處理這個問題的?(沒有觸及每一個這樣的字符串)。 – 2010-11-02 22:06:54

+0

不,'string [N]'不能包含Unicode的所有內容。我會從TTntEdit下降,並用AnsiString替換Text屬性。在GetText/SetText方法中,轉換爲Unicode。由於聽起來像在非俄羅斯系統上使用俄語,因此應使用TntSystem.pas的StringToWideStringEx/WideStringToStringEx,並根據字體的字符集更改代碼頁。例如,RUSSION_CHARSET將映射到1251。你仍然可以輸入應用程序不支持的字符,這樣用戶就會有點小心。你可以通過掛鉤WM_PASTE和WM_CHAR來防止這種情況。 – 2010-11-02 22:25:10

+0

謝謝。我將不得不玩耍一下,看看有什麼可能。 – 2010-11-02 22:55:42