☆フリガナの取得 その1
.NETで入力した時のフリガナを取得するには、どうしたらいいのかといろいろ探しましたが、結局Win32のDLLを利用する方法しか見つけられませんでした。
以下、GotDotNet Japan 掲示板 での kazuMorono2 さんのコメントを参考に作ったものです。
http://www.gdncom.jp/general/bbs/ShowPost.aspx?PostID=21415
ここでは、Delphi for .NETで作成しました。一応、動作しているようです。
以下、GotDotNet Japan 掲示板 での kazuMorono2 さんのコメントを参考に作ったものです。
http://www.gdncom.jp/general/bbs/ShowPost.aspx?PostID=21415
ここでは、Delphi for .NETで作成しました。一応、動作しているようです。
unit unit1; interface uses Windows, Messages, SysUtils, variants, classes, Graphics, Controls, Forms, Dialogs, System.Runtime.InteropServices, System.Text, Borland.Vcl.WinUtils, System.ComponentModel, Borland.Vcl.StdCtrls; type TCompositionStrEvent = procedure (Sender: TObject; Value: String) of Object; TEditEx = class(TEdit) private FOnCompositionStr: TCompositionStrEvent; protected procedure WndProc(var Message: TMessage); override; published property OnCompositionStr: TCompositionStrEvent read FOnCompositionStr write FOnCompositionStr; end; TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private Edit1: TEdit; EditEx1: TEditEx; procedure OnCompositionStr(Sender: TObject; Value: String); end; var Form1: TForm1; implementation {$R *.nfm} { Borland.Vcl.Imm の ImmGetCompositionStringを使うとWindows XPの場合、 戻り値が2倍になるため、Borland.Vcl.Immを使わず、ここで定義しておく。 たぶんCharSetの指定が原因だと思われる。} type HIMC = Integer; const GCS_RESULTREADSTR = $0200; [DllImport('Imm32.dll')] function ImmGetContext(hWnd: HWND): HIMC; external; [DllImport('Imm32.dll')] function ImmGetCompositionString(hImc: HIMC; dWord1: DWORD; lpBuf: StringBuilder; dwBufLen: DWORD): Longint; external; [DllImport('Imm32.dll')] function ImmReleaseContext(hWnd: HWND; hImc: HIMC): Boolean; external; procedure TEditEx.WndProc(var Message: TMessage); var Imc: HIMC; Len: Longint; Str: StringBuilder; Temp1, Temp2: array of Byte; begin if Assigned(FOnCompositionStr) and (Message.Msg = WM_IME_COMPOSITION) and ((Message.LParam and GCS_RESULTREADSTR) <> 0) then begin Imc := ImmGetContext(Self.Handle); Len := ImmGetCompositionString(Imc, GCS_RESULTREADSTR, nil, 0); Str := StringBuilder.Create(Len); ImmGetCompositionString(Imc, GCS_RESULTREADSTR, Str, Str.Capacity); ImmReleaseContext(Self.Handle, Imc); Temp1 := System.Text.Encoding.Default.GetBytes(Str.ToString); SetLength(Temp2, Len); System.Array.Copy(Temp1, 0, Temp2, 0, Len); FOnCompositionStr(Self, System.Text.Encoding.Default.GetString(Temp2)); end; inherited WndProc(Message); end; procedure TForm1.FormCreate(Sender: TObject); begin { EditEx1の生成 } EditEx1 := TEditEx.Create(Self); EditEx1.Parent := Self; EditEx1.Top := 10; EditEx1.Left := 10; EditEx1.Text := 'ここに入力して!'; EditEx1.OnCompositionStr := OnCompositionStr; { Edit1の生成 } Edit1 := TEdit.Create(Self); Edit1.Parent := Self; Edit1.Top := 40; Edit1.Left := 10; Edit1.Text := ''; end; procedure TForm1.OnCompositionStr(Sender: TObject; Value: String); begin Edit1.Text := Edit1.Text + Value; end; end.
| 固定リンク