從圖中可以看出,基本上實現了將輸入法的信息截獲顯示在下面的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.