中西文混合輸入介面中,使用者頻繁手動切換輸入法極為不便。Delphi 編輯元件 (TEdit, TMemo 等) 提供 ImeName 與 ImeMode 屬性,可實現焦點進入時自動切換指定輸入法狀態。
| 屬性 | 說明 |
|---|---|
| ImeName | 輸入法名稱,下拉選單列出系統安裝的所有輸入法。 |
| ImeMode | 輸入法模式:imClose 關閉、imOpen 開啟、imChinese 中文模式、imDontCare 維持上次狀態、imSAlpha 半形、imAlpha 全形。 |
在表單放置一個 ComboBox,啟動時列出系統所有輸入法 (Screen.Imes.CommaText),使用者選擇後,程式將該名稱指派給所有需中文輸入元件的 ImeName。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Label1, Label2, Label3, Label4: TLabel;
Edit1, Edit2: TEdit;
Memo1: TMemo;
ComboBox1: TComboBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure ComboBox1DropDown(Sender: TObject);
procedure ComboBox1Exit(Sender: TObject);
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption := '輸入法自動切換示範';
Label1.Caption := '中文輸入編輯框'; Label1.Font.Size := 12;
Label2.Caption := '西文輸入編輯框'; Label2.Font.Size := 12;
Label3.Caption := '中文多行文字編輯器'; Label3.Font.Size := 12;
Label4.Caption := '選擇慣用輸入法'; Label4.Font.Size := 12; Label4.Font.Color := clRed;
// 中文欄位預設開啟輸入法
Edit1.ImeMode := imOpen;
Memo1.ImeMode := imOpen;
// 西文欄位不強制
Edit2.ImeMode := imDontCare;
Button1.Caption := '離開'; Button1.Font.Size := 12;
end;
procedure TForm1.ComboBox1DropDown(Sender: TObject);
begin
// 列出系統所有已安裝輸入法
ComboBox1.Items.CommaText := Screen.Imes.CommaText;
end;
procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
// 使用者選定後套用至中文輸入元件
Edit1.ImeName := ComboBox1.Text;
Memo1.ImeName := ComboBox1.Text;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Close;
end;
end.
Edit1 或 Memo1 → 自動開啟所選輸入法。Edit2 → 自動關閉輸入法 (西文模式)。
// 正確寫法:使用 Chr(39) 代表單引號
Table1.Filter := 'Name=' + Chr(39) + '李*' + Chr(39);