To understand the differences between the two, you must first understand that not all words we typed through the keyboard are transferred to the program through the input method.
That is to say, some of the words we typed on the keyboard are directly transmitted to the program without the input method, such as numbers such as 1, 2, and 3, as well as ABC letters and Carriage Return spaces.
Some are transferred to the program through the input method, such as Chinese
After understanding this, the difference between wm_ime_char and wm_char is easy to understand.
It should be noted that you can enter numbers and English letters directly or through the input method.
Wm_ime_char: All characters generated by the input method will generate the wm_ime_char message. Defwindowproc converts wm_ime_char to the wm_char message. Wm_char: the characters in the send program directly without the input method will respond to the wm_char message. |
Note:
For Unicode windows, there is no difference between wm_ime_char and wm_char,wParam
All areWCHAR
Is the input character.
For non-Unicode (DBCS) windowswParam
It is a character generated by the input method. This character may be either a single-byte character or a double-byte character. If it is a single-byte character, it is no different from wm_char; if it is a double-byte character, then
wParam
The 8-bit high is leading byte, and the 8-bit low is continuation byte.
All characters generated through the input method will generate the wm_ime_char message instead of the wm_char,DefWindowProc
Converts wm_ime_char to one or two corresponding wm_char messages.
For example:
- Enter "9" in the input method. Then, wm_char (0 × 0039) is received)
- Open the input method and enter "9" → receive wm_ime_char (0 × 0039) → receive wm_char (0 × 0039)
- Enter "stupid" in the input method → receive wm_ime_char (0xb1bf) → receive wm_char (0x00b1) → receive wm_char (0x00bf)
Programming implementation:
Wm_char MFC provides its response function onchar (), but wm_ime_char does not provide its response function. We need to write it by ourselves.
The following example shows how to process the keyboard character message:
1) define a global array to store keyboard input characters
wchar_t m_ImeChar[2];
Unicode of common characters occupies 2 bytes
The uncommon Unicode occupies 4 bytes.
Therefore, define 4 own arrays to store words
2) Reload window processing
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
Windowproc (uint message, wparam, lparam) {Switch (Message) {Case wm_ime_char: // preprocessing special characters if (pretreat (wm_ime_char, wparam, lparam) break; // put other characters in the array. If (m_imechar [0]> = 0xd800 & m_imechar [0] <= 0 xdbff) {m_imechar [1] = (wchar_t) wparam ;} else {m_imechar [0] = (wchar_t) wparam; If (m_imechar [0]> = 0xd800 & m_imechar [0] <= 0 xdbff) break;} onimechar (m_imechar ); break; default: break;} return cscrollview: windowproc (message, wparam, lparam );}
Note:
Unicode encoding U + 0000 ~ U + FFFF is the basic multilingualplaneBMP)
U + 10000~ U + 10FFFF16 secondary planes
Common Words are in BMP, which occupies 2 bytes, while uncommon words are out of BMP, which occupies 4 bytes.
Common Words and uncommon words:
In BMP, the Code Point segments from u + d800 to U + dfff are permanently retained and not mapped to characters.
Apart from BMP, the first two bytes of the four bytes are high bytes, and the last two bytes are low bytes.
The range of the first two bytes is 0xd800 .. 0 xdbff.
The range of the last two bytes is 0xdc00 .. 0 xdfff.
It can be seen that there is no intersection between characters in BMP and those other than BMP.
Therefore,
If (m_imechar [0]> = 0xd800 & m_imechar [0] <= 0 xdbff)
If this condition is met, it indicates the uncommon words that occupy 4 bytes.
For uncommon words, the system will send two wm_ime_char messages, the first to send its high byte, the second to send its low byte
When running to onimechar (m_imechar);, m_imechar stores real words (whether common words or uncommon words, they are correctly saved in this array)
3) process the words in the array
void OnImeChar(wchar_t* Str);
OnImeChar(wchar_t* Str){。。。}
4) use onchar to respond to uninput characters
Onchar (uint nchar, uint nrepcnt, uint nflags) {Switch (nchar) {Case 0x0d: // enter the key if (pretreat (0x0d, null, null) break; onenterkeydown (); update (); break; Case 0x08: // return key if (pretreat (0x08, null, null) break; If (m_paselelem.getsize ()> 0) ondelelemsel (); else onbackspacekeydown (); Update (); break; Case 0x20: // Space key if (pretreat (0x20, null, null) break; onspacekeydown (); Update (); break; Case 0x09: // Tab key if (pretreat (0x09, null, null) break; ontabkeydown (); update (); break; default: break;} If (m_paselelem.getsize ()> 0) {cleanselelem ();} cscrollview: onchar (nchar, nrepcnt, nflags );}
Handling principles:
For the wm_ime_char message, it is processed by the wm_ime_char response function.
For character messages without input method, wm_char is used for processing.
References:
Http://timothyqiu.com/2012/wm_ime_char/