首页  编辑  

获取输入法编码

Tags: /超级猛料/IME.Charset.输入法和中文处理/   Date Created:

获取输入法编码

function GetIMMCode(CHS: string; ImmName: string): string;

{

  返回CHS对应指定输入法的编码,例如返回"中国"的拼音编码,目前仅适用于Win9x

}

 function QueryCompStr(hKB: HKL; const sChinese: AnsiString): string;

 var

   dwGCL: DWORD;

   szBuffer: PChar;

   iMaxKey, iStart, i: integer;

   Imc: HIMC;

 begin

   Result := '';

   Imc := ImmGetContext(GetActiveWindow);

   iMaxKey := ImmEscape(hKB, Imc, IME_ESC_MAX_KEY, nil); { Get Single IMM Max Code Length }

   if iMaxKey <= 0 then exit;

   dwGCL := ImmGetConversionList(hKB, Imc, pchar(sChinese), nil, 0, GCL_REVERSECONVERSION);

   if dwGCL <= 0 then Exit; { Check if the IMM support Conversion }

   GetMem(szBuffer, dwGCL);

   dwGCL := ImmGetConversionList(hKB, Imc, pchar(sChinese), @szBuffer, dwGCL, GCL_REVERSECONVERSION);

   if dwGCL > 0 then

   begin

     iStart := Byte(szBuffer[24]);

     for i := iStart to iStart + iMaxKey * 2 do

       Result := Result + szBuffer[i];

   end;

   ImmReleaseContext(GetActiveWindow, Imc);

 end;

const

 MAX_HKL = 255;

var

 iHandleCount: integer;

 szImeName: array[0..254] of char;

 pList: array[0..MAX_HKL - 1] of HKL;

 i: integer;

begin

 iHandleCount := GetKeyboardLayoutList(MAX_HKL, pList);

 for i := 0 to iHandleCount - 1 do

   if ImmEscape(pList[i], 0, IME_ESC_IME_NAME, @szImeName) > 0 then

     if Pos(ImmName, szImeName) > 0 then

     begin

       Result := QueryCompStr(pList[i], CHS);

       Exit;

     end;

end;