Unit IME; {============================================= ================================== Design by: Peng Guo Date: http://kacarton.yeah.net/ blog: Http://blog.9cbs.net/nhconch email: kacarton@sohu.com Articles is the author original, please contact me before reprinting, please indicate the source of the article, retain the author information, thank you for your support! ============================================================================================================================================================================================================= =======================}
Interface
Uses IMM, Windows;
procedure ImmActive (hWindows: THandle; bChinese: Boolean); // open or close the input method procedure ImmEnabled (hWindows: THandle; bChinese: Boolean); // enable or disable the input method procedure SendDBCSString (hFocus: HWND; const Text: string) ; // Send a Chinese text to the control
IMPLEMentation
// Open or close the input method, ex: IMEACTIVE (0, true); // can search similar code on the Internet, but use IME_THOTKEY_IME_NONIME_TOGGLE, only for the traditional Chinese windows, use it in Simplified Chinese Windows procedure ImmActive (hWindows: THandle; bChinese: Boolean); begin if hWindows = 0 then hWindows: = GetFocus; if hWindows = 0 then Exit; if ImmIsIME (GetKeyboardLayOut (0)) <> bChinese then ImmSimulateHotKey (hWindows, IME_CHOTKEY_IME_NONIME_TOGGLE); end ;
// Allows the input method, EX: immenabled (edit1.handle, true) / / equivalent to set Edit1.imemode to Imchinese (True), Imclose (False) Procedure Immenabled (Hwindows: Thandle; BCHINESE: BOOLEAN); Var himmc: himc; begin if hwindows = 0 THEN HWINDOWS: = getfocus; if hwindows = 0 dam; hiMMC: = ImmgetContext (hwindows); ImmsetopenStatus (HIMMC, BCHINESE); END; // Send Chinese text to the control, EX: Senddbcsstring (edit1.handle, ', finally can send Chinese!') Procedure Senddbcsstring (Hfocus: hwnd; const text: string); var i: integer; ch: byte; begin if hfocus = 0 dam; = getfocus; I: = 1; while i <= length (text) do begin ch: = Byte (text [i]); if Windows.Indbcsleadbyte (ch) The Begin Inc (i); sendMessage (Hfocus , WM_IME_CHAR, MAKEWORD (Byte (Text [I]), CH), 0); ELSE SendMessage (Hfocus, WM_IME_CHAR, WORD (CH), 0); INC (I); END;
End.