IME の取得や 操作あれこれ







IME の取得や 操作あれこれ

uses Imm;



function TForm1.IsImeOpened(const winctr: TWinControl): boolean;
var Imc:HIMC;
begin
  winctr.SetFocus;
  Imc := ImmGetContext(winctr.Handle);
  result:= ImmGetOpenStatus(imc);
  ImmReleaseContext(winctr.Handle, Imc);
end;

//IME オンオフ判定
procedure TForm1.IsIMEButtonClick(Sender: TObject);
begin
   if IsImeOpened(RIchEdit1) then
      Edit4.Text := 'ON'
      else
      Edit4.Text := 'OFF';
end;

///////////////////////////////////////
//IMEのモード番号を取得する
function  GetInputCharMode(const winctr :TWinControl):DWORD;
var Imc:HIMC;
  dwConversion : DWORD;
  dwSentence   : DWORD;
begin
  winctr.SetFocus;
  Imc := ImmGetContext(winctr.Handle);
  ImmGetConversionStatus(Imc,dwConversion,dwSentence);
    Result:= dwConversion;
  ImmReleaseContext(winctr.Handle, Imc);
end;

procedure TForm1.IME_ModeButton1Click(Sender: TObject);
begin
   Edit3.Text := IntToStr(GetInputCharMode(RichEdit1));
end;

//WindowsXP SP1

//IME日本語入力  Microsoft Natural Input 2002 ver. 8.1
//25 Hiragana 
//27 Zen katakana
//24 Zen Eisu
//19 Han Katakana
//16 Han Eisu

//IME韓国語入力  Korean Input System(IME 2002) XPに付属しているもの
//0 straight
//1 Hangul


///////////////////////////////////////

//IME on off セット
procedure ImeOnOff(const winctr :TWinControl; const status:boolean);
 var Imc:HIMC;
begin
 winctr.SetFocus;

  Imc := ImmGetContext(winctr.Handle);
 if status then
   ImmSetOpenStatus(imc,true)
  else
   ImmSetOpenStatus(imc,false);
 ImmReleaseContext(winctr.Handle, Imc);

end;

procedure TForm1.IMEonButtonClick(Sender: TObject);
begin
   ImeOnOff(RichEdit1,True);
end;

procedure TForm1.ImeOffButtonClick(Sender: TObject);
begin
   ImeOnOff(RichEdit1,False);
end;

//ひらがな入力などをセットする
procedure SetInputCharMode(const winctr :TWinControl; ImeCharMode:DWORD);
var
  Imc          : HIMC;
  dwConversion : DWORD;
  dwSentence   : DWORD;
begin
  winctr.SetFocus;

  Imc := ImmGetContext(winctr.Handle);
  ImmGetConversionStatus(Imc,dwConversion,dwSentence);

  ImmSetConversionStatus(Imc,ImeCharMode,dwSentence);
  ImmReleaseContext(winctr.Handle, Imc);
end;

//IME ひらがな
procedure TForm1.ImeHiraButtonClick(Sender: TObject);
begin
   ImeOnOff(RichEdit1,True);
   SetInputCharMode(RichEdit1,25);
end;

//IME 英数
procedure TForm1.ImeEisuButtonClick(Sender: TObject);
begin
  ImeOnOff(RichEdit1,True);
   SetInputCharMode(RichEdit1,16);
end;

///////////////////////////////////////
//キーボードレイアウトが日本語かどうか
function GetLanguage:String;
var
 p:array[0..100] of char;
begin
 GetKeyboardlayoutname(p);
 if StrPas(p) = 'E0010411'  then
    Result := 'Japanese'
    else
    Result := 'Other';
end;

//同じくキーボードレイアウトが日本語かどうか
function KeylayoutJapanese:Boolean;
var
 p:array[0..100] of char;
begin
 GetKeyboardlayoutname(p);
 if StrPas(p) = 'E0010411'  then
    Result := True
    else
    Result := False;
end;


//WIndowsXPは「E0010411」Windows98SEでは「e0030411」なので
//下4桁の判定がベストだろう(05/11/19)
function KeylayoutJapanese:Boolean;
var
 p:array[0..100] of char;
 s : String;
 i : Integer;
begin
 GetKeyboardlayoutname(p);
  s := StrPas(p);
  i := Length(s);
  s := Copy(s, (i-3), 4);
   if s = '0411'  then
    Result := True
    else
    Result := False;
end;

procedure TForm1.GetKeyBoardLayoutButtonClick(Sender: TObject);
begin
   Edit5.Text := GetLanguage;
end;

procedure TForm1.KeyJapanButtonClick(Sender: TObject);
begin
  if KeylayoutJapanese then
     Edit5.Text := 'Japanese On'
     else
     Edit5.Text := 'Other';
end;










Back