共用方式為


處理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);
    }

使用輸入法管理員