Exemple #1
0
 public string[] GetReverseConversion(string AText)
 {
     string[] strList = null;
     if (AText.Length > 0)
     {
         IntPtr hIMC   = ImmGetContext(this.Handle);
         IntPtr hKL    = GetKeyboardLayout(0);
         int    dwSize = ImmGetConversionList(hKL, hIMC, AText, IntPtr.Zero, 0, GCL_REVERSECONVERSION);
         if (dwSize > 0)
         {
             IntPtr BufList = Marshal.AllocHGlobal(dwSize);
             try
             {
                 ImmGetConversionList(hKL, hIMC, AText, BufList, dwSize, GCL_REVERSECONVERSION);
                 CANDIDATELIST list = new CANDIDATELIST();
                 Marshal.PtrToStructure(BufList, list);
                 byte[] buf = new byte[dwSize];
                 Marshal.Copy(BufList, buf, 0, dwSize);
                 Marshal.FreeHGlobal(BufList);
                 int    os  = list.dwOffset;
                 string str = System.Text.Encoding.Default.GetString(buf, os, buf.Length - os - 3);
                 char[] par = "\0".ToCharArray();
                 strList = str.Split(par);
             }
             finally
             {
                 ImmReleaseContext(this.Handle, hIMC);
             }
         }
     }
     return(strList);
 }
Exemple #2
0
        public static string[] GetImeCandidateList(IntPtr window)
        {
            var imContext = ImmGetContext(window);
            var list      = new CANDIDATELIST();
            var dwSize    = ImmGetCandidateList(imContext, 0, IntPtr.Zero, 0);

            if (dwSize > 0)
            {
                var BufList = Marshal.AllocHGlobal(dwSize);
                ImmGetCandidateList(imContext, 0, BufList, dwSize);
                Marshal.PtrToStructure(BufList, list);
                byte[] buf = new byte[dwSize];
                Marshal.Copy(BufList, buf, 0, dwSize);
                Marshal.FreeHGlobal(BufList);
                int os  = list.dwOffset;
                var str = System.Text.Encoding.Default.GetString(buf, os, buf.Length - os);
                str = Regex.Replace(str, @"\0+$", "");
                var par = "\0".ToCharArray();
                ImmReleaseContext(window, imContext);
                return(str.Split(par));
            }
            else
            {
                ImmReleaseContext(window, imContext);
                return(new string[0]);
            }
        }
        private void getCandidateList(IntPtr context)
        {
            System.Diagnostics.Debug.WriteLine("getCandidateList");

            // Always returns zero on Windows 10
            IntPtr candidateListCount = new IntPtr();
            int    result             = ImmGetCandidateListCountW(context, candidateListCount);

            System.Diagnostics.Debug.WriteLine("candidate list count=" + candidateListCount + " result=" + result);

            int bytesNeeded = ImmGetCandidateListW(context, 0, IntPtr.Zero, 0);

            System.Diagnostics.Debug.WriteLine("bytesNeeded=" + bytesNeeded);

            if (0 == bytesNeeded)
            {
                immCandidatesBox.Text = "";
                return;
            }

            IntPtr candidateListBytes = Marshal.AllocHGlobal(bytesNeeded);
            int    bytesCopied        = ImmGetCandidateListW(context, 0, candidateListBytes, bytesNeeded);

            System.Diagnostics.Debug.WriteLine("bytesCopied=" + bytesCopied);

            CANDIDATELIST list = new CANDIDATELIST();

            Marshal.PtrToStructure(candidateListBytes, list);
            byte[] buf = new byte[bytesNeeded];
            Marshal.Copy(candidateListBytes, buf, 0, bytesNeeded);
            Marshal.FreeHGlobal(candidateListBytes);

            System.Diagnostics.Debug.WriteLine("list.dwSize=" + list.dwSize);
            System.Diagnostics.Debug.WriteLine("list.dwStyle=" + list.dwStyle);
            System.Diagnostics.Debug.WriteLine("list.dwCount=" + list.dwCount);
            System.Diagnostics.Debug.WriteLine("list.dwSelection=" + list.dwSelection);
            System.Diagnostics.Debug.WriteLine("list.dwPageStart=" + list.dwPageStart);
            System.Diagnostics.Debug.WriteLine("list.dwPageSize=" + list.dwPageSize);
            System.Diagnostics.Debug.WriteLine("list.dwOffset (first offset)=" + list.dwOffset);

            const int members = 6;
            string    output  = "";

            for (int i = 0; i < list.dwCount; i++)
            {
                uint ithOffset = BitConverter.ToUInt32(buf, (members + i) * (sizeof(uint)));
                int  strLen;
                if (i == list.dwCount - 1)
                {
                    strLen = (int)(list.dwSize - ithOffset - 2);
                }
                else
                {
                    uint jthOffset = BitConverter.ToUInt32(buf, (members + i + 1) * (sizeof(uint)));
                    strLen = (int)(jthOffset - ithOffset - 2);
                }
                string ithStr = Encoding.Unicode.GetString(buf, (int)ithOffset, strLen);
                string sLine  = "Offset " + i + ": " + ithStr;
                System.Diagnostics.Debug.WriteLine(sLine);
                output += sLine + "\n";
            }
            immCandidatesBox.Text = output;
        }