The system sends IME window messages to the window that has focus when certain events occur. For example, the system sends the WM_IME_SETCONTEXT message when a window is activated. IME-unaware windows pass these messages to the DefWindowProc function, which sends them to the corresponding default IME window. IME-aware windows either process these messages or forward them to their own IME windows.
To have an IME window carry out an action, use the WM_IME_CONTROL message. To have the IME notify an application about changes to the composition string, use the WM_IME_COMPOSITION message. Use the WM_IME_NOTIFY message for general changes to the status of IME windows.
To process the WM_IME_COMPOSITION message, applications test the bits in the lParam parameter and call the ImmGetCompositionString function to retrieve the indicated string or data. The following code example checks for the result string, allocates sufficient memory for the string, and retrieves the result string from the IME.
HIMC hIMC;
HWND hWnd;
DWORD dwSize;
HGLOBAL hstr;
LPSTR lpstr;
case WM_IME_COMPOSITION:
if (lParam & GCS_RESULTSTR)
{
hIMC = ImmGetContext(hWnd);
If (!hIMC)
MyError(ERROR_NULLCONTEXT);
// Get the size of the result string.
dwSize = ImmGetCompositionString(hIMC, GCS_RESULTSTR, NULL, 0);
// Increase buffer size for NULL terminator;
// maybe it is in unicode.
dwSize += sizeof(WCHAR);
lpstr = (LPSTR) LocalAlloc(LPTR, dwSize);
if (lpstr == NULL)
MyError(ERROR_GLOBALLOCK);
// Get the result string that is generated by IME into lpstr.
ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);
ImmReleaseContext(hWnd, hIMC);
// Add this string into the text buffer of the application.
LocalFree(lpstr);
}