处理WM_IME_COMPOSITION消息

处理 WM_IME_COMPOSITION 消息的 IME 感知应用程序测试 lParam 参数中的位,并调用 ImmGetCompositionString 函数来检索指示的字符串或数据。 以下示例检查结果字符串,为字符串分配足够的内存,并从 IME 检索结果字符串。

HIMC hIMC;
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 terminating null character,  
        //   maybe it is in UNICODE 
        dwSize += sizeof(WCHAR);

        hstr = GlobalAlloc(GHND,dwSize);
        if (hstr == NULL)
             MyError(ERROR_GLOBALALLOC);

        lpstr = (LPSTR)GlobalLock(hstr);
        if (lpstr == NULL)
             MyError(ERROR_GLOBALLOCK);

        // Get the result strings that is generated by IME into lpstr. 
        ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);
        ImmReleaseContext(hWnd, hIMC);

        // add this string into text buffer of application 

        GlobalUnlock(hstr);
        GlobalFree(hstr);
    }

使用输入法管理器