程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 控制與捕捉輸入法的實現單元

控制與捕捉輸入法的實現單元

編輯:Delphi
 今天,有個朋友問,如何去實現讓一個輸入控件,比如Edit中不能輸入中文!而且不能讓用戶切換到任何其他輸入法,始終保持為英文輸入!群中很多人都說用ImeCode等來控制,這個控制是沒錯!但是,本控制無法控制使用的過程中用戶自己切換輸入法的問題,此時我們應該不讓用戶去切換輸入法!所以最終的實現方式,應該對系統的IME的消息進行攔截來達到目的,在以前做游戲中彈出窗口輸入信息時,需要自己模擬輸入法的信息,然後在游戲內部窗口中繪制出輸入法的相關信息,那時對輸入法的控制有研究過幾天,而且也基本上實現了對輸入法的輸出信息進行捕捉和控制等!現在將那個東西開源出來,以便於大家共同探討提高!示例截圖:

  從圖中可以看出,基本上實現了將輸入法的信息截獲顯示在下面的Label控件中,並且有對每個Edit的輸入法做相應的控制!

  詳細代碼:

(******************************************************)
(*        得閒工作室             *)
(*     輸入法截獲與控制單元庫          *)
(*                          *)
(*    DxImeOperControl Unit            *)
(*   DxImeOperControl Version 1.x         *)
(*  Copyright(c) 2009-2010  不得閒        *)
(*  email:[email protected]   QQ:75492895  *)
(******************************************************)
unit DxImeOperControl;
interface
uses Windows,SysUtils,Messages,Classes,Imm,Controls,Forms;
type
 TDxImeControl = class;
 pControlRec = ^TControlRec;
 TControlRec = record
  LinkControl: TWinControl;
  OldWndProc: Pointer;
  OldWndMethod: TWndMethod;
  OwnIme: TDxImeControl;
 end;
 TImeChangeEvent = procedure(Sender: TObject;ImeControl: TWinControl;ImeName: string;ImeFileName: string;var AcceptChange: Boolean) of object;
 TImeKeyChangeEvent = procedure(Sender: TObject;ImeControl: TWinControl;KeyInputStr: string;CursorPos: integer;InResult: string)of object;
 TImeKeyResultEvent = procedure(Sender: TObject;ImeControl: TWinControl;InputResult: string) of object;
 TImeCandListOpenEvent = procedure(Sender: TObject;ImeControl: TWinControl;List: TStrings) of object;
 TNotifyInfo = (SetConversionModeInfo,OpenCandDateInfo,ChangeCandDateInfo,CloseCandDateInfo);
 {通知類型:全半角/中英文標點變化,進入選字,翻頁,關閉選字列表}
 TImeNotifyEvent = procedure(Sender: TObject;NotifyInfo: TNotifyInfo) of object;
 TDxImeControl = class(TComponent)
 private
  FLinkHandle: THandle;
  FImc: HIMC;
  FLinkForm: TCustomForm;
  FOnImeActive: TNotifyEvent;
  FEnabled: boolean;
  FOnImeChange: TImeChangeEvent;
  FOnKeyInput: TImeKeyChangeEvent;
  FPageCount: Integer;
  FCurrentPage: Integer;
  CandList: TStringList;
  KeyBoardLayOut: Integer;
  FShowSoftKeyBoard: Boolean;
  FOnKeyInputed: TImeKeyResultEvent;
  FOnStartKeyInput: TNotifyEvent;
  FCurrentHwnd: THandle;
  FImeName: array[0..50] of char;
  FOnImeNotify: TImeNotifyEvent;
  FOnCandListOpen: TImeCandListOpenEvent;
  FShowOpenStatus: Boolean;
  FKeyListStr: string;
  procedure SetLinkHandle(const Value: THandle);
  function GetImeCompositonString: string;
  function GetImeResult: string;
  function GetImeOpen: Boolean;
  procedure SetLinkForm(const Value: TCustomForm);
  procedure SetEnabled(const Value: boolean);
  procedure RestoreLinkWndProc;
  procedure SetShowSoftKeyBoard(const Value: Boolean);
  procedure setCurrentHwnd(const Value: THandle);
  procedure SetShowOpenStatus(const Value: Boolean);
  function GetShowOpenStatus: Boolean;
  function GetImeName: string;
 public
  constructor Create(AOwner: TComponent);override;
  destructor Destroy;override;
  function SetInputIme(Control: TWinControl;ToImeFileName: string): Boolean;//設置輸入法
  procedure SetIme(Control: TWinControl;AImeName: string);
  procedure SetCompositionString(Control: TWinControl;SetStr: string);//設定組字字符
  property InputStr: string read GetImeCompositonString;//獲得正在輸入的字符串
  property InputResult: string read GetImeResult;//獲得輸入的選字結果
  property LinkForm: TCustomForm read FLinkForm write SetLinkForm;//指定連接上哪個窗體,連接上的窗體都會被處理
  property Imc: HIMC read FImc;
  property ImeName: string read GetImeName;
  property ImeOpen: Boolean read GetImeOpen;
  property PageCount: Integer read FPageCount;
  property CurrentPage: Integer read FCurrentPage;
  procedure GetCandList(FCandList: TStrings;Page: Integer=0);//獲得侯選字列表
  property CurrentHwnd: THandle read FCurrentHwnd write setCurrentHwnd;
  procedure ChangeToChinese(ChangeControl: TWinControl;ChangeChildren: Boolean=true;ToChinese: Boolean=true);
  procedure NextIme;
  property KeyListStr: string read FKeyListStr;
 published
  property Enabled: boolean read FEnabled write SetEnabled; //是否可用,捕獲輸入法消息
  property OnImeActive: TNotifyEvent read FOnImeActive write FOnImeActive;//輸入法激活時觸發
  property OnImeChange: TImeChangeEvent read FOnImeChange write FOnImeChange;//輸入法改變時觸發事件
  property OnKeyInput: TImeKeyChangeEvent read FOnKeyInput write FOnKeyInput;//輸入時觸發
  property OnKeyInputed: TImeKeyResultEvent read FOnKeyInputed write FOnKeyInputed;
  property OnStartKeyInput: TNotifyEvent read FOnStartKeyInput write FOnStartKeyInput;
  //property ShowSoftKeyBoard: Boolean read FShowSoftKeyBoard write SetShowSoftKeyBoard;//是否顯示小鍵盤
  property OnImeNotify: TImeNotifyEvent read FOnImeNotify write FOnImeNotify;
  property OnCandListOpen: TImeCandListOpenEvent read FOnCandListOpen write FOnCandListOpen;
  property ShowOpenStatus: Boolean read GetShowOpenStatus write SetShowOpenStatus;
 end;
implementation
var
 WndList: TList;
end.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved