HSPポータル
サイトマップ お問い合わせ


HSPTV!掲示板


未解決 解決 停止 削除要請

2008
0408
begriffIME操作で文字化け6解決


begriff

リンク

2008/4/8(Tue) 21:18:54|NO.15002

ただいまIMEを操作するモジュールを製作していますが、
IMEでの入力文字数(未決定文字数)が64字あたりを越えると文字化け (のような
ことが起きます。str型でのメモリの宣言不足かと思い (自動で拡張しますよね
256^2で初期化したのですが問題は解決せず。。。

#module #uselib "imm32.dll" #cfunc global ImmCreateContext "ImmCreateContext" #cfunc global ImmAssociateContext "ImmAssociateContext" int, int #func global ImmSetCompositionWindow "ImmSetCompositionWindow" int,int,int #func global ImmGetCompositionString "ImmGetCompositionStringA" int,int,var,int #func global ImmSetCompositionFont "ImmSetCompositionFontA" int,int #deffunc ime_creat //ウィンドウにIMEを割り当て hwnd_ime=ImmCreateContext() k=ImmAssociateContext(hwnd,hwnd_ime) return hwnd_ime #deffunc ime_pos int hwnd2_ime,int com_posx, int com_posy //IMEの出現場所指定 標準0,0 compositionform = 0x0002 ,com_posx ,com_posy ImmSetCompositionWindow hwnd2_ime,varptr(compositionform) return #deffunc ime_getstr int hwnd2_ime,var buf,var str_size //IMEに入力されている文字をリアルタイムに取得 str_size=0 ImmGetCompositionString hwnd2_ime,8,buf,str_size str_size=stat sdim buf,str_size ImmGetCompositionString hwnd2_ime,8,buf,str_size return #deffunc ime_font int hwnd4_ime,str font_name,int font_size //IMEフォント指定 dim logfont,15 lfFaceName =font_name logfont(0)=font_size poke logfont,23,1 poke logfont, 28, lfFaceName ImmSetCompositionFont hwnd4_ime,varptr(logfont) return #global ime_creat//IME割り当て(既存する場合は上書き) hwnd_ime=stat//ハンドル格納 str1_="" sdim buf,256*256 sdim str1_,256*256//初期化がちゃんとできていないのが原因かと思ったが違うようだ font_name="MS UI Gothic":font_size=13 ime_font hwnd_ime,font_name,font_size//IMEとmesでのフォントを合わせる font font_name,font_size//mesでのフォントも指定 ime_pos hwnd_ime,0,40//IME出現場所 repeat ime_getstr hwnd_ime,buf,s//IMEに入力されている文字と長さを取得 if s!0:str1_=buf color 255,255,255:boxf color pos 0,0:mes "全角モードにしてください。" pos 0,20:mes buf//リアルタイム取得 title "buf=\""+buf+"\" length="+s redraw 1 wait 3 redraw 0 loop



この記事に返信する


begriff

リンク

2008/4/8(Tue) 21:41:08|NO.15003

あとフォントも思ったように変わってくれません。。。



As

リンク

2008/4/8(Tue) 22:26:36|NO.15006


#deffunc ime_getstr int hwnd2_ime,var buf,var str_size //IMEに入力されている文字をリアルタイムに取得 str_size=0 ImmGetCompositionString hwnd2_ime,8,buf,str_size str_size=stat sdim buf,str_size ImmGetCompositionString hwnd2_ime,8,buf,str_size return

str_sizeでバイト数を取得し、sdimでbufを初期化しているようですが、
どうやらNUL文字分が考慮されていないようです。

64バイト以降で発生するというのは、sdimで初期化できる最低が64バイトだから
だと思います。

つまり、64バイト以上だとNUL文字が考慮されない1文字少ないサイズで初期化されて
しまうために、"\0”がなくなってしまい文字化け部分が発生したんだと思います。


つまり

sdim buf,str_size+1

これで十分でしょう。



begriff

リンク

2008/4/8(Tue) 22:29:06|NO.15007

ありがとうございます。理解できました。m(_ _)m



begriff

リンク

2008/4/8(Tue) 23:38:08|NO.15009

フォントはPCによって正しかったり違ったりしているようです。。
unicode shift-jisとかなのか・・・



begriff

リンク

2008/4/9(Wed) 01:17:32|NO.15011

Asさんのおかげで解決することができました。
フォントの異常は、既存するIMEがあった場合削除しないとうまく動作しないらしい。

#module #uselib "imm32.dll" #cfunc ImmCreateContext "ImmCreateContext" #cfunc ImmAssociateContext "ImmAssociateContext" int, int #func ImmSetCompositionWindow "ImmSetCompositionWindow" int,int,int #func ImmGetCompositionString "ImmGetCompositionStringA" int,int,var,int #func ImmSetCompositionFont "ImmSetCompositionFontA" int,int #func ImmReleaseContext "ImmReleaseContext" int,int #func ImmDestroyContext "ImmDestroyContext" int #cfunc ImmGetContext "ImmGetContext" int #deffunc ime_dest //ウィンドウに関係づけられたIMEを削除 defime=ImmGetContext(hwnd) if defime!0{ ImmReleaseContext hwnd,defime ImmDestroyContext defime } return #deffunc ime_hwnd //ウィンドウに関係づけられたIMEのハンドルを取得 defime=ImmGetContext(hwnd) return defime #deffunc ime_creat //ウィンドウにIMEを割り当て hwnd_ime=ImmCreateContext() k=ImmAssociateContext(hwnd,hwnd_ime) return hwnd_ime #deffunc ime_pos int hwnd2_ime,int com_posx, int com_posy //IMEの出現場所指定 標準0,0 compositionform = 0x0002 ,com_posx ,com_posy ImmSetCompositionWindow hwnd2_ime,varptr(compositionform) return #deffunc ime_getstr int hwnd2_ime,var buf,var str_size //IMEに入力されている文字をリアルタイムに取得 str_size=0 ImmGetCompositionString hwnd2_ime,8,buf,str_size str_size=stat sdim buf,str_size+1 ImmGetCompositionString hwnd2_ime,8,buf,str_size return #deffunc ime_font int hwnd4_ime,str font_name,int font_size //IMEフォント指定 dim logfont,20 lfFaceName =font_name logfont(0)=font_size poke logfont,23,1 memcpy logfont, lfFaceName, strlen(lfFaceName), 28, 0 ImmSetCompositionFont hwnd4_ime,varptr(logfont) return #global title "h" screen 5 ime_hwnd hwnd_ime=stat if hwnd_ime=0{ ime_creat//IME割り当て hwnd_ime=stat//ハンドル格納 } str1_="" sdim buf,256*256 sdim str1_,256*256//初期化がちゃんとできていないのが原因かと思ったが違うようだ font_name="MS P明朝":font_size=13 ime_font hwnd_ime,font_name,font_size//IMEとmesでのフォントを合わせる font font_name,font_size//mesでのフォントも指定 ime_pos hwnd_ime,0,40//IME出現場所 repeat ime_getstr hwnd_ime,buf,s//IMEに入力されている文字と長さを取得 if s!0:str1_=buf color 255,255,255:boxf color pos 0,0:mes "全角モードにしてください。" pos 0,20:mes buf//リアルタイム取得 title "buf=\""+buf+"\" length="+s redraw 1 wait 3 redraw 0 loop



begriff

リンク

2008/4/9(Wed) 01:18:38|NO.15012

解決チェック



ONION software Copyright 1997-2023(c) All rights reserved.