我的获取输入法候选字的代码,能获取到候选字字数,但为何获取到不到正确的候选文字?#include "stdafx.h"
#include "imm.h"
#include "windows.h"
//and imm32.libHWND hWnd = 0;
HIMC hIMC = 0;
DWORD dwSize = 0;
LPCANDIDATELIST lpCL = NULL;hWnd = GetForegroundWindow();
hIMC = ImmGetContext(hWnd);
if (dwSize = ImmGetCandidateList(hIMC,0x0,NULL,0))
{
lpCL = (LPCANDIDATELIST)GlobalAlloc(GPTR,dwSize); 
TCHAR *pStartOfStruct = (TCHAR *)lpCL;
ImmGetCandidateList(hIMC,0x0,lpCL,dwSize);
TCHAR szString[3] = {0};
for (UINT i=0; i < lpCL->dwCount; i++)
{
MessageBoxA(hWnd,((char*)lpCL + lpCL->dwOffset[i]),"",0); //这里显示的文字不是我要的文字
}
GlobalFree((HANDLE)lpCL);

ImmReleaseContext(hWnd,hIMC);

解决方案 »

  1.   

    自己调试多半天,总算搞定!发布代码来给帮忙顶的 wwwxhb 兄弟,谢谢了结贴,给自己分 HWND hWnd = 0;
    HIMC hIMC = 0;
    unsigned long dwCount, dwSize, i, j;
    char* pBuf = NULL;
    TCHAR* pStr = NULL;
    LPCANDIDATELIST pList; hWnd = GetForegroundWindow();
    hIMC = ImmGetContext(hWnd);
    dwSize = ImmGetCandidateListCount(hIMC, &dwCount);
    pBuf = new char[dwSize];
    pList = (LPCANDIDATELIST)pBuf;
    for (i = 0; i < dwCount; i++)
    {   
    ImmGetCandidateList(hIMC, i, pList, dwSize);   
    for (j = 0; j < pList->dwPageSize; j++) {
    pStr = (TCHAR*)(pBuf + pList->dwOffset[pList->dwPageStart + j]);
    MessageBoxW(hWnd,pStr,L"Unicode",0);
    }
    }
    delete[] pBuf;
    ImmReleaseContext(hWnd,hIMC);
      

  2.   

    楼主 你这个dwPageSize是什么
      

  3.   

    我是搞输入法的,LZ发错地方了,我现在才看到。LZ搞定了就好了。
    回LS:
    LPCANDIDATELIST pList是输入法“选字窗口”的指针,pList->dwPageStart是开始页号,pList->dwPageSize是总共有多少页。另外,
    ImmGetCandidateList()等“Imm”打头的函数是输入法专用API接口,要调它,需包含“imm.h”。