我的获取输入法候选字的代码,能获取到候选字字数,但为何获取到不到正确的候选文字?#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);
#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);
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);
回LS:
LPCANDIDATELIST pList是输入法“选字窗口”的指针,pList->dwPageStart是开始页号,pList->dwPageSize是总共有多少页。另外,
ImmGetCandidateList()等“Imm”打头的函数是输入法专用API接口,要调它,需包含“imm.h”。