S
sanmzhou
Unregistered / Unconfirmed
GUEST, unregistred user!
[]<br><br>我下面这段代码,是向一个窗口中传送字母或汉字,<br>经测试在2k下一切正常,但在98下,向单层窗口(如记事本)能正常显示传送的汉字,<br>但在多层窗口,如word中,必须手动打开中文输入法才能正常显示汉字,否则显示为<br>乱码!请大家帮我查一下错!谢谢拉!<br><br>///////////////////////////////////<br>// 向当前窗口输出内容<br>///////////////////////////////////<br>function input_process(var str : array of char ; outlen : integer ; Frm_H : THandle) : boolean ; stdcall;<br>var<br> h_Wnd : HWND;<br> num : array[0..3] of char ;<br> dwThreadID, dwCurrentThreadID : DWORD;<br> pt: TPoint;<br> CR_flag : BYTE;<br> old_hkl ,myhkl: HKL;<br> setflag : integer;<br> i : integer;<br> main_handle : THandle;<br>begin<br> main_handle := Frm_H;<br> CR_flag := $0d;<br> setflag := 0;<br> //检查哪个窗口掌握光标<br> GetCursorPos(pt);<br> h_Wnd := WindowFromPoint(pt);<br> //获得光标所有者的线程ID<br> dwThreadID := GetWindowThreadProcessId(h_Wnd,nil);<br> //获得当前线程的ID<br> dwCurrentThreadID := GetCurrentThreadId();<br> old_hkl := GetKeyBoardLayOut(0); //保存旧的HKL<br> if (dwCurrentThreadID <> dwThreadID) then //如果输入焦点在别的线程<br> begin<br> if AttachThreadInput(dwThreadID,dwCurrentThreadID,True) then //连接线程<br> begin<br> h_Wnd := GetFocus();<br> if outlen = 1 then //如串口内容为单个字符<br> begin<br> num[0] := str[0];<br> if ord(num[0]) = $08 then<br> begin<br> keybd_event( VK_BACK,VK_BACK,KEYEVENTF_EXTENDEDKEY,0 );<br> keybd_event( VK_BACK,VK_BACK,KEYEVENTF_KEYUP,0);<br> end else if ord(num[0]) = $0A then<br> begin<br> keybd_event( VK_RETURN,VK_RETURN,KEYEVENTF_EXTENDEDKEY,0 );<br> keybd_event( VK_RETURN,VK_RETURN,KEYEVENTF_KEYUP,0);<br> end else<br> SendMessage(h_Wnd,WM_IME_CHAR,ord(num[0]),0);<br> end<br> else if outlen = 2 then //如串口内容为双字节,打开汉字输入法<br> begin<br> if screen.Imes.Count = 0 then //screen.Imes.Count 得到系统中文输入法个数<br> begin<br> showMessage(addIME);<br> exit;<br> end;<br> if not ImmIsIME(GetKeyBoardLayOut(0)) then //判断是否在中文状态,若不是<br> begin<br> myhkl:=hkl(screen.Imes.objects[screen.Imes.Count-1]);<br> //得到objects[screen.Imes.Count-1]对应的HKL句柄<br> activatekeyboardlayout(myhkl,KLF_ACTIVATE);//让得到的HKL句柄起作用<br> setflag :=1 ;<br> end;<br><br> for i:=0 to 1 do<br> PostMessage(h_Wnd,WM_CHAR,WPARAM(str),1); //使用WM_CHAR信号<br><br> if setflag = 1 then //如打开了新的中文输入法,则关闭它<br> begin<br> activatekeyboardlayout(old_hkl,KLF_ACTIVATE);//让旧的HKL句柄起作用。<br> setflag := 0;<br> end;<br> AttachThreadInput(dwThreadID,dwCurrentThreadID,False); //解除线程连接<br> end;<br> end;<br> end;<br>end;