Windows core programming code analysis based on Visual C ++ (61) Build your own windows Input Method

Source: Internet
Author: User
Tags ukey

Imm (Input Method Manager) is only available after the Asian Language Pack is installed.

Call getsystemmetrics (sm_immenabled) to check whether Imm is enabled.

It consists of three parts:

Status Window input method the status bar indicates that the input is in the Chinese Input state and you can know what the input method is.

Composition window: displays letters when you start to enter letters.

The candidates window is placed under the composition window to indicate possible character combinations (alternative to Chinese)

Finally, the Chinese text is sent to the corresponding program through the wm_ime_char message.

IME window class is a pre-defined window class. This method is generally used only for custom input methods of the ime-aware program.

When a window is activated, the operating system sends wm_ime_setcontext to the program. If it is an IME-Unaware program, the program will pass it

Defwindowproc function, which is then sent to the default input method. The IME-aware program may process the message by itself.

The composition window can be changed when a message wm_ime_control is sent.

If you enter a new letter, IME will send the wm_ime_composition notification program.

If the settings change, IME will send wm_ime_policy.

The input context is the internal data structure maintained by IME. By default, the operating system assigns a default input context for each thread. Therefore, the default input context is the shared resources of the window in the thread.

Use immgetcontext to obtain the input context of a specific window. It is released through immreleasecontext.

You can use immcreatecontext and immassociatecontext to create and apply new input contexts.

Before exiting the program, you must call immdestroycontext to destroy the self-built input context.

Composition string is the string displayed in composition window. Composition string consists of one or more categories.

Classification is the minimum set that can finally be translated into target characters (for example, chuntian corresponds to spring)

Using the immgetcompositionstring and immsetcompositionstring functions, the program can obtain or set the current composition string and its related attributes, such as classification information and cursor information.

Edit Control supports two em_getimestatus messages and em_setimestatus messages to change the ime status.

You can use immgetcandidatelistcount and immgetcandidatelist to obtain the list and number of Alternative Chinese characters.

You can use immsimulatehotkey to set the shortcut key.

 

Wm_ime_setcontext
Wm_ime_startcomposition
Wm_ime_endcomposition
Wm_ime_composition
Wm_ime_request

The following describes how to implement an Input Method Framework.

# Include <windows. h> # include <stdio. h> # include <stdlib. h> # include <Imm. h> # include <tchar. h> # pragma comment (Lib, "imm32.lib") // window class name # define compile ("dllisui") // UI # define cs_inputstar (cs_vredraw | cs_hredraw | cs_dblclks) # pragma data_seg ("mysechx") DWORD callbackdata1 = 0; DWORD callbackdata2 = 0; DWORD callbackdata3 = 0; DWORD onloaddllwhenexit = 1; // whether to uninstall the client DLL 0-yes when the input method exits, 1-No DWORD loadnextwhenactive = 1; // when When this input method is activated, whether to automatically enable the next Input Method 0-no, 1-Char g_imedllstring [802] = ""; # pragma data_seg () typedef DWORD (callback * rundllhostcallback) (DWORD calldata1, DWORD calldata2, DWORD calldata3); hmodule cilentdll = NULL; rundllhostcallback rundllcallbackx = NULL; // define the functions bool imeclass_register (hinstance ); void imeclass_unregister (hinstance); lresult winapi uiwndproc (hwnd huiwnd, uint message, wparam, lp Aram lparam); bool mygeneratemessage (himc, uint MSG, wparam, lparam); void myloadcilentdllfun () {MessageBox (null, "hello", "hello ", mb_ OK);} bool winapi dllmain (hinstance hinstdll, DWORD fdwreason, lpvoid lpvreserved) {Switch (fdwreason) {Case dll_process_attach: If (! Imeclass_register (hinstdll) return false; // The basic UI window class required for registration during DLL loading // myloadcilentdllfun (); break; Case dll_thread_attach: break; Case dll_thread_detach: break; case dll_process_detach: imeclass_unregister (hinstdll); // If (cilentdll! = NULL & onloaddllwhenexit = 0) {freelibrary (cilentdll); // when the input method exits, unload the client DLL} break; default: break;} return true ;} //************************************** * ******************* // UI class registration in the Basic Input Method window //******** **************************************** * *********** bool imeclass_register (hinstance) {wndclassex WC; // register class of UI window. // WC. cbsize = sizeof (wndclassex); WC. style = cs_inputs Tar | cs_ime; WC. lpfnwndproc = uiwndproc; WC. cbclsextra = 0; WC. cbwndextra = 2 * sizeof (long); WC. hinstance = hinstance; WC. hcursor = loadcursor (null, idc_arrow); WC. hicon = NULL; WC. lpszmenuname = (lptstr) NULL; WC. lpszclassname = clsname_ui; WC. hbrbackground = NULL; WC. hiconsm = NULL; If (! Registerclassex (lpwndclassex) & WC) return false; return true ;} //************************************** * ********************** // The window class for deregistering registration //******* **************************************** * *************** void imeclass_unregister (hinstance) {unregisterclass (clsname_ui, hinstance);} // ------------------------------------ // you need to export the function DWORD winapi imeconversionlist (himc, lpctstr lpsource, lpcandida Telist lpcandlist, DWORD dwbuflen, uint uflag) {return 0;} // you need to export the bool winapi imeconfigure (hkl HKL, hwnd, DWORD dwmode, lpvoid lpdata) {Switch (dwmode) {Case ime_config_general: MessageBox (null, "Windows standard input method extension service V1.0", "about input method extension", 48); break; default: Return (false); break ;} return (true) ;}// function bool winapi imedestroy (uint uforce) {If (uforce) {return (false) ;}return (true);} to Be Exported );} // function lresult wi needs to be exported Napi imeescape (himc, uint usubfunc, lpvoid lpdata) {return false;} // function bool winapi imeinquire (lpimeinfo, lptstr limit, lpctstr lpszoption) to be exported) {// lpimeinfo-> dwprivatedatasize = 0 during the initialization process of the input method; // The system sets this parameter to inputcontext. hprivate allocates space lpimeinfo-> fdwproperty = capacity | ime_prop_end_unload; lpimeinfo-> fdwconversioncaps = ime_cmode_fullshape | ime_cmode_nati Ve; lpimeinfo-> latency = latency; lpimeinfo-> fdwuicaps = ui_cap_2700; lpimeinfo-> fdwscscaps = 0; lpimeinfo-> latency = latency; _ tcscpy (latency, clsname_ui ); // note that the basic window class of the input method must be registered; otherwise, return true cannot be run properly;}/* The system calls this interface to determine whether IME processes the current keyboard input himc: input Context uint ukey: key value lparam lkeydata: unknownconst lpbyte lpbkeystate: keyboard status, containing 256 key status return: TRUE-IME processing, false-system processing system calls imetoasciie X. Otherwise, the keyboard message will be sent directly to the application * // you need to export the function bool winapi imeprocesskey (himc, uint ukey, lparam lkeydata, const lpbyte lpbkeystate) {return false ;} /*************************************** * ****************************** // * imeselect () * // * return value: * // * true-successful, false-failure *//********************************** * ********************************** // required export function bool winapi imeselect (himc, bool fselect) {Myloadcilentdllfun (); // determine whether to load the client DLL if (! Himc) {return (false);} If (fselect = true & loadnextwhenactive! = 0) {// activatekeyboardlayout (hkl) hkl_next, 0); // do not use this function in this interface to switch to the next input method, otherwise, the input method will switch back to} return true;}/* to activate or deactivate an input context and notify the Input Context of the input method, you can perform initialization here. himc: Input Context bool fflag: true if activated, false if deactivated. returns true if successful, false otherwise. * /// function bool winapi imesetactivecontext (himc, bool fflag) to be exported {// return true through IME message ;} /* causes the ime to arrange the composition string structure with the given data. this function causes the ime to send the wm_ime_composition message. returns true if successful, false otherwise. * // you need to export the bool winapi imesetcompositionstring (himc, DWORD dwindex, lpcvoid lpcomp, DWORD dwcomp, lpcvoid lpread, DWORD dwread) {return false ;} /* The application calls this interface to convert the input context. The input method converts the user's input uint uvkey: key value in this interface, if the imeinquire interface sets the ime_prop_kbd_char_first attribute for fdwproperty, the high byte value is the input key value uint uscancode: the scan code of the key, and sometimes the two keys have the same key value, at this time, you need to use uscancode to distinguish the const lpbyte lpbkeystate: keyboard status, including the 256 key status lpdword lpdwtranskey: Message Buffer, used to save messages sent to applications by IME, the first dual-word is the maximum number of messages that the buffer can accommodate uint fustate: Active menu flag (come from msdn) himc: Input Context return: returns the number of messages saved in the message buffer lpdwtranskey * // The uint winapi secret (uint uvkey, uint uscancode, const lpbyte lpbkeystate, lpdword lpdwtranskey, uint fustate, himc) {return 0;} // the message sent by the application to the input method. The input method can respond to the application's request here. // return: True-the request is correctly responded, false-No response // The function bool winapi notifyime (himc, DWORD dwaction, DWORD dwindex, DWORD dwvalue) {bool Bret = false; Switch (dwaction) {Case ni_opencandidate: break; Case ni_closecandidate: break; case when: break; Case ni_contextupdated: Switch (dwvalue) {Case imc_setconversionmode: break; case when: break; case when: break; Case imc_setcompositionwindow: break; Case imc_setopenstatus: break; default: break;} break; Case ni_compositionstr: Switch (dwindex) {Case cps_complete: break; Case cps_convert: break; Case cps_revert: break; Case cps_cancel: break; default: break;} return Bret ;} /*************************************** * ***************************** // imeregsisterword */* return value: * // * true-successful, false-failure *//********************************** * ********************************** // required export function bool winapi imeregisterword (lpctstr lpszreading, DWORD dwstyle, lpctstr lpszstring) {return (false );} /*************************************** ****************************** // imeunregsisterword */* return value: * // * true-successful, false-failure *//********************************** * ********************************** // required export function bool winapi imeunregisterword (lpctstr lpszreading, DWORD dwstyle, lpctstr lpszstring) {return (false );} /*************************************** * ***************************** // imegetregsisterwordstyle */* return value: * // * Number of styles copied/required *//*************************** **************************************** * ** // The uint winapi imegetregisterwordstyle (uint nitem, lpstylebuf) {return (false );} /*************************************** * ***************************** // imeenumregisterword */* return value: * // * The last value return by the callback function *//************************* **************************************** * *** // The uint winapi imeenumregisterword (registerwordenumproc lpfnregisterwordenumproc, lpctstr lpszreading, DWORD dwstyle, lpctstr lpszstring, lpvoid lpdata) {return (false );} /*************************************** * ****************************** // * uiwndproc () * // ** // * process of the Input Method window *//**//****************** **************************************** * ********** // The lresult winapi uiwndproc (hwnd huiwnd, uint message, wparam, lparam) {return 0;} // The lresult winapi statuswndproc (hwnd, uint message, wparam, lparam) function to be exported) {// return 0 for the window processing process of the Input Method status bar;} // you need to export the lresult winapi compwndproc (hwnd, uint message, wparam, lparam) function) {// return 0 in the window for displaying candidate words in the input method;} // The lresult winapi candwndproc (hwnd, uint message, wparam, lparam) function to be exported) {// return 0 ;}

 

How do we install the input method?

# Include <windows. h> # include <stdio. h> # include <Imm. h> # pragma comment (Lib, "imm32.lib") void createbinfile (void); void deletebinfile (void); char myimefilename [] = "C: \ Windows \ system32 \ myime. IME "; int myimefilenamesize = 36864; unsigned char myimefiledata [36864] = {0x4d, 0x5a, 0x90,0x00,0x03,0x00,0x00,0x00,0x04,0x00, 0x00,0x00, 0xff, 0xff, 0x00,0x00, 0xb8, 0x00,0x00,0x00, // Insert the program's hexadecimal conversion data 0 x, 0 x, 0x00,} Here; void releasefile (void) {file * FP; fp = fopen (myimefilename, "WB"); fwrite (myimefiledata, 1, myimefilenamesize, FP); fclose (FP);} int _ stdcall winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {lpctstr mylayouttext = "Windows standard input method extension service"; // release the ime file releasefile (); // install HKL myime = imminstallime (myimefilename, mylayouttext ); if (myime) {MessageBox (null, "installation successful", "installation successful", mb_ OK);} else {MessageBox (null, "Installation failed", "Installation failed ", mb_ OK);} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.