首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 輸入法自動切換與使用者自選輸入法實作

     編輯:Delphi

問題背景

中西文混合輸入介面中,使用者頻繁手動切換輸入法極為不便。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.

執行效果

  1. 下拉選單選擇慣用輸入法 (如「新酷音」、「微軟新注音」)。
  2. 游標移至 Edit1 或 Memo1 → 自動開啟所選輸入法。
  3. 游標移至 Edit2 → 自動關閉輸入法 (西文模式)。
  4. 再次移回中文欄位 → 輸入法自動恢復。

補充:TDataSet.Filter 字串中單引號處理


// 正確寫法:使用 Chr(39) 代表單引號
Table1.Filter := 'Name=' + Chr(39) + '李*' + Chr(39);
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved