您的位置:首页 > 其它

windows客户端开发--如何修复输入法提示框位置不正确

2016-03-05 22:30 1271 查看
应该也有人会遇到这样的困扰:就是在edit或是richedit控件中输入时,输入法的提示框位置不正确。

何为HIMC

什么是IME (Input Method Editors)?广义上讲,IME是微软提供的Windows平台的一套输入法编程规范,依照这套规范(框架),你不需要处理太多输入法特性相关的操作(光标跟随,输入捕获,字码转换后输出到应用程序等),你只需要使用IME规范里面提供的工具函数(imm32库),实现规范所指定必须导出的接口即可。实际上你要做的就是写一个导出函数包含IME规范规定的接口的dll,所以,狭义上讲IME就是你写的这个dll。

HIMC实际上是个DWORD类型,一般在启动或关闭输入法时用到。

头文件和库:

[code]#include   <imm.h> 
#pragma   comment(lib, "imm32.lib ")


如何获取HIMC

通过函数:ImmGetContext

Returns the input context associated with the specified window.

An application should routinely use this function to retrieve the current input context before attempting to access information in the context.

The application must call ImmReleaseContext when it is finished with the input context.

重要的是看返回值:

Returns the handle to the input context.

结构体COMPOSITIONFORM 是干什么的?

作用:

Contains style and position information for a composition window.

原型:

[code]typedef struct tagCOMPOSITIONFORM {
  DWORD dwStyle;
  POINT ptCurrentPos;
  RECT  rcArea;
} COMPOSITIONFORM, *PCOMPOSITIONFORM;


GetCaretPos函数就不需要再解释了:

该函数将插入标记的位置(按客户区坐标)信息拷贝到指定的POINT结构中

ImmSetCompositionWindow函数怎样设置的?

作用:

Sets the position of the composition window.

原型:

[code]BOOL ImmSetCompositionWindow(
  _In_ HIMC              hIMC,
  _In_ LPCOMPOSITIONFORM lpCompForm
);


等等,我们还没有完。

就像获得设备描述表一样,有获取,就要有释放.

ImmReleaseContext函数来释放:

作用:

此函数释放输入上下文和解锁相关联的内存。应用程序必须调用ImmReleaseContext为对应每一个ImmGetContext。

原型:

[code]BOOL ImmReleaseContext(
HWND hWnd,
HIMC hIMC
);


有了以上的储备,工作变得简单。

[code]if (uMsg == WM_IME_COMPOSITION)
{
        HIMC hIMC = ImmGetContext(m_hWnd);//获取
        if (hIMC)
        {
            // Set composition window position near caret position
            POINT point;
            GetCaretPos(&point);//获取输入光标位置,存入point结构体

            COMPOSITIONFORM Composition;
            Composition.dwStyle = CFS_POINT;
            Composition.ptCurrentPos.x = point.x;
            Composition.ptCurrentPos.y = point.y;
            ImmSetCompositionWindow(hIMC, &Composition);//设置

            ImmReleaseContext(m_hWnd, hIMC);//释放
        }
}


good job!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: