On the programming of IME in Windows

Source: Internet
Author: User

On the programming of IME in Windows
Author: Guangdong Nanhai City Real Faith Technology Co., Ltd.-Li Jianguo

As you know, Delphi many controls have IME properties. So good things VC can not bring their own, how to do? In fact, through the registry, the API is implemented. The following is my study of the IME results, and provide examples of projects for your reference:

Download Sample Project 10.6K


This sample program runs the results as

1. API to be used


RegOpenKey: Open registry One key
RegQueryValue: Querying a key value
RegQueryValueEx: Ibid.
RegCloseKey: Turn off the Open key

Loadkeyboardlayout: Loading Input method
Activatekeyboardlayout: Activating Input Method

2. The location of the IME information in the registry

In HKEY_USERS: ". DEFAULT\Keyboard Layout\Preload "Put the installed input method, there are several sub-keys in the name of the number, the content is the Input method designator (keyboard layout), such as" e0040804 ", where the Left 4 bits is the device code (devices identifier), the right 4-bit is the language code (language identifier). For example above: The left e004 refers to the intelligent ABC, the right 0804 refers to the Chinese mainland. (see MSDN)
In HKEY_LOCAL_MACHINE: "System\currentcontrolset\control\keyboard layouts\" puts the registered input method. His subkey is named IME code (keyboard layout), which is the IME file, name and other information of the input method.

3. Main ideas

We can first put the installed input method (from the registry), the user select one, and then activate the input method.
Two key functions:
HKL loadkeyboardlayout (LPCTSTR pwszklid,uint Flags);
The first parameter is the IME code to be opened, such as "e0040804" (intelligent ABC);
The second parameter is a flag bit, such as klf_activate (active).

HKL activatekeyboardlayout (HKL hkl,uint Flags);
The first argument is the open input method handle (returned by loadkeyboardlayout);
The second parameter is a flag bit, such as klf_setforprocess. (see MSDN)

4. Example procedure

Create a new dialog-based project, add a ComboBox control and add control variable M_CB1. Add an edit control to add the control variable M_EDT1. To the ComboBox, increase the mapping function to Cbn_selchange OnSelchangeCombo1 (). For edit1, increase the SetfocusEdit1 () to En_setfocuos. In the dialog box class header file, add the array declaration under public: CString LST[10];
Add the following code before the return of OnInitDialog ():
HKEY Hk,hk1;  Long cp=16;  Char lp[15];  Char a[2];  a[0]= ' 1 ';  a[1]= ' + ';  CString str,str1; Str= ". Default\\keyboard layout\\preload\\ ";//Installed Input method.  Note: Win2000 differs str+=a;  int i=0; while (:: RegOpenKey (HKEY_USERS,STR,&HK) ==error_success)//Open Key {a[0]++;//Next sub-key str= ".         Default\\keyboard layout\\preload\\ ";          Str+=a;         if (:: RegQueryValue (HK,NULL,LP,&CP)!=error_success)//Installed Input Method MessageBox ("ERROR"); :: RegEnumKeyEx str1= "System\\currentcontrolset\\control\\keyboard layouts\\";//keyboardlayoutlayout str1+=         Lp         LST[I++]=LP; Open the corresponding keyboardlayoutlayout if (RegOpenKey (HKEY_LOCAL_MACHINE,STR1,&HK1) ==error_success) {LPBY TE Lpd=new Byte[80];//datavalue DWORD lpt=reg_sz;//datatype DWORD Lps=80; DataSize if (RegQueryValueEx (HK1, "Layout text", Null,&lpt,lpd,&lps)!=error_success) MessageBox ("Qu         ery error ", (LPCTSTR) LpD); M_CB1. AddString ((LPCTSTR) LpD);         Delete LpD;         } else MessageBox ("open error");  RegCloseKey (HK1);  }:: RegCloseKey (HK); M_CB1. SetCurSel (0);
Add the following code to ONSETFOCUSEDIT1 ():
       CString temp;         TEMP=M_CB1. GetCurSel ()!=cb_err?lst[m_cb1. GetCurSel ()]: "00000409";         HKL HKL;         Hkl=loadkeyboardlayout (temp,klf_activate);//Load Input Method         if (hkl==null) OnOK ();         Activatekeyboardlayout (hkl,klf_setforprocess);//Activating Input Method
Add the following code to ONSELCHANGECOMBO1 ():
      M_edt1. SetFocus ();
Can be compiled and run.
5. Note:Win2000 under different conditions. Registry hkey_users: ". DEFAULT\Keyboard Layout\Preload No subkey has only a number-name entry, and the value is the Input method designator (keyboard layout). In the sample code not only provides the code under win 9X, but also provides the corresponding code snippet under 2000, please refer to the example project.




A preliminary study of citation sources programming under Windows IME-Author: Li Jianguo

http://blog.163.com/wan_gn/blog/static/39515821200881062754773/

On the programming of IME in Windows

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.