C# and IME - getting the current input text

谁都会走 提交于 2019-11-29 07:33:04

You can use this to get the current composition. This will work in any composition state, and for Japanese, Chinese, and Korean. I've only tested it on Windows 7, so not sure if it'll work on other versions of Windows.

As for things being the same, well, things are actually horribly different between the three.

using System.Text;
using System;
using System.Runtime.InteropServices;

namespace Whatever {
    public class GetComposition {
        [DllImport("imm32.dll")]
        public static extern IntPtr ImmGetContext(IntPtr hWnd);
        [DllImport("Imm32.dll")]
        public static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);
        [DllImport("Imm32.dll", CharSet = CharSet.Unicode)]
        private static extern int ImmGetCompositionStringW(IntPtr hIMC, int dwIndex, byte[] lpBuf, int dwBufLen);

        private const int GCS_COMPSTR = 8;

        /// IntPtr handle is the handle to the textbox
        public string CurrentCompStr(IntPtr handle) {
            int readType = GCS_COMPSTR;

            IntPtr hIMC = ImmGetContext(handle);
            try {
                int strLen = ImmGetCompositionStringW(hIMC, readType, null, 0);

                if (strLen > 0) {
                    byte[] buffer = new byte[strLen];

                    ImmGetCompositionStringW(hIMC, readType, buffer, strLen);

                    return Encoding.Unicode.GetString(buffer);

                } else {
                    return string.Empty;
                }
            } finally {
                ImmReleaseContext(handle, hIMC);
            }
        }
    }
}

Other implementations I've seen used a StringBuilder, but it is much better to use a byte array, because the SB will usually end up with some rubbish in it as well. The byte array is encoded in UTF16.

And usually, you would want to call GetComposition whenever you receive a "WM_IME_COMPOSITION" message as Dian said.

It is very important to call ImmReleaseContext after you call ImmGetContext, which is why it is in a finally block.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!