In winform, we often encounter problems such as disabling input methods in text input boxes or changing the full-width input methods. After reading the relevant information, the following is a summary:
(1) control. imemode attribute: gets or sets the control's Input Method Editor (IME) mode.
(1) because of the inheritance relationship between form attributes, if most of your winform forms need to be set to the valid state of the input method, you can set the imemode attribute of form to on.
(2) Use imemode. On
(3) If you need to set all input methods for the entire form to halfwidth, you can use the following Code :
# Region correction of full/half-width input methods
// Declare some API functions
[Dllimport ("imm32.dll")]
Public static extern intptr immgetcontext (intptr hwnd );
[Dllimport ("imm32.dll")]
Public static extern bool immgetopenstatus (intptr himc );
[Dllimport ("imm32.dll")]
Public static extern bool immsetopenstatus (intptr himc, bool B );
[Dllimport ("imm32.dll")]
Public static extern bool immgetconversionstatus (intptr himc, ref int lpdw, ref int lpdw2 );
[Dllimport ("imm32.dll")]
Public static extern int immsimulatehotkey (intptr hwnd, int lnghotkey );
Private const int ime_cmode_fullshape = 0x8;
Private const int ime_chotkey_shape_toggle = 0x11;
Protected override void onactivated (eventargs E)
{
Base. onactivated (E );
Intptr hime = immgetcontext (this. Handle );
// If the input method is enabled
If (immgetopenstatus (hime ))
{
Int imode = 0;
Int isentence = 0;
// Retrieve input method information
Bool bsuccess = immgetconversionstatus (hime, ref imode, ref isentence );
If (bsuccess)
{
// If it is a full angle, convert it to a half angle
If (imode & ime_cmode_fullshape)> 0)
Immsimulatehotkey (this. Handle, ime_chotkey_shape_toggle );
}
}
}
# Endregion correction of full/half-width input methods
Note: The following namespace must be referenced: using system. runtime. interopservices;
(4) For textbox controls that are only used to input numeric values, the imemode attribute is usually set to imemode. Off. For the Form class, the imemode attribute value is set to imemode. nocontrol.
(5) You cannot directly enter the method editor or most classes that are allowed to directly input from IME to set the value of this attribute to nocontrol.
Important:
Unless imemode has been set to imemode. Disable programmatically, you can change it interactively. In the previous case, you cannot use ime.
(2) meanings of imemode attributes:
Imemode enumeration: Specifies a value that is used to determine the status of the Input Method Editor (IME) of the selected object.
The following is Microsoft's explanation:
| |
Member name |
Description |
|
Alpha |
Single-byte characters (SBC ). This setting is only valid for Korean and Japanese ime. |
|
Alphafull |
Double-byte letters and numbers. This setting is only valid for Korean and Japanese ime. |
|
Close |
|
|
Disable |
IME is disabled. If you use this setting, you cannot open IME from the keyboard and the ime floating window is hidden. |
|
Hangul |
Korean SBC. This setting is only valid for Korean ime. |
|
Hangulfull |
Korean DBC. This setting is only valid for Korean ime. |
|
Hiragana |
Hirakana DBC. This setting is only valid for Japanese ime. |
|
Inherit |
Inherits the ime mode of the parent control. |
|
Katakana |
Katakana DBC. This setting is only valid for Japanese ime. |
|
Katakanahalf |
Katakana SBC. This setting is only valid for Japanese ime. |
|
Nocontrol |
None (default ). |
|
Off |
IME is disabled. This mode indicates that the IME is disabled, which means that the object is in the same way as the English input mode. This setting is only valid for Japanese, simplified Chinese, and traditional Chinese ime. |
|
On |
IME is enabled. This value indicates that the IME is enabled and can contain Chinese or Japanese characters. This setting is only valid for Japanese, simplified Chinese, and traditional Chinese ime. |
Note:
The IME editor allows you to enter and edit Chinese, Japanese, and Korean characters. IME is a basic component used to write scripts in Chinese, Japanese, and Korean. These writing systems have more characters than can be coded on a regular keyboard. IME in these languages uses a base character sequence that describes a single character or a group of characters to allow you to enter a wider set of characters. The base character can be a combination of letters, Chinese characters, and Chinese characters from the Korean syllable.
(3) If it is set to an appropriate input method?
Here is only an example. You can refer to the following example:
UsingSystem;
UsingSystem. drawing;
UsingSystem. collections;
UsingSystem. componentmodel;
UsingSystem. Windows. forms;
UsingSystem. Data;
Public Class Form1: system. Windows. Forms. Form
{
RichTextBox RTB = New RichTextBox ();
Public Form1 ()
{
This . Controls. Add (RTB );
RTB. Dock = dockstyle. Fill;
This. Inputlanguagechanged + =NewInputlanguagechangedeventhandler (languagechange );
}
Private Void Languagechange (Object sender, inputlanguagechangedeventargs E)
{
// If the input language is Japanese, Set the initial imemode to katakana.
If(E. inputlanguage. Culture. twoletterisolanguagename. Equals ("Ja"))
{
RTB. imemode = system. Windows. Forms. imemode. katakana;
}
}
Public Static Void Main ( String [] ARGs)
{
Application. Run ( New Form1 ());
}
}