很关键的98与2K汉字输入问题!(100分)

  • 主题发起人 主题发起人 NNNNNN
  • 开始时间 开始时间
N

NNNNNN

Unregistered / Unconfirmed
GUEST, unregistred user!
该程序向当前光标处插入一串字符,在98下运行正常,当在2K下若这串字符是双字节的则<br>不能正常运行,为什么?有谁能够回答?<br><br>procedure SendKeys(sSend: String);<br>var<br>&nbsp; &nbsp; I: Integer;<br>&nbsp; &nbsp; Focushld,Windowhld: Hwnd;<br>&nbsp; &nbsp; Threadld: Dword;<br>&nbsp; &nbsp; CH: byte;<br>begin<br>&nbsp; Windowhld := GetForegroundWindow;<br>&nbsp; Threadld &nbsp;:= GetWindowThreadProcessId(Windowhld,nil);<br>&nbsp; AttachThreadInput(GetCurrentThreadId,Threadld,True);<br>&nbsp; Focushld := GetFocus;<br>&nbsp; AttachThreadInput(GetCurrentThreadId,Threadld,False);<br><br>&nbsp; if Focushld = 0 then Exit;<br>&nbsp; I := 1;<br>&nbsp; while I &lt;= Length(sSend) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; CH := Byte(sSend);<br>&nbsp; &nbsp; &nbsp; if Windows.IsDBCSLeadByte(CH) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendMessage(Focushld, WM_IME_CHAR, MakeWord(Byte(sSend), CH), 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; SendMessage(Focushld, WM_IME_CHAR, Word(CH), 0);<br>&nbsp; &nbsp; &nbsp; Inc(I);<br>&nbsp; &nbsp; end;<br>end;<br><br>
 
哎哟,哥们,我们真是同病相连呀!我下面这段代码,是向一个窗口中传送字母或汉字,<br>经测试在2k下一切正常,但在98下,向单层窗口(如记事本)能正常显示传送的汉字,<br>但在多层窗口,如word中,必须将自己打开中文输入法才能正常显示汉字,否则显示为<br>乱码!你可以参考一下,也帮我查一下错,互相帮助拉!<br><br><br>///////////////////////////////////<br>// 向当前窗口输出内容<br>///////////////////////////////////<br>function input_process(var str : array of char ; outlen : integer ; Frm_H : THandle) : boolean ; stdcall;<br>var<br>&nbsp; h_Wnd : HWND;<br>&nbsp; num : array[0..3] of char ;<br>&nbsp; dwThreadID, dwCurrentThreadID : DWORD;<br>&nbsp; pt: TPoint;<br>&nbsp; CR_flag : BYTE;<br>&nbsp; old_hkl ,myhkl: HKL;<br>&nbsp; setflag : integer;<br>&nbsp; i : integer;<br>&nbsp; main_handle : THandle;<br>begin<br>&nbsp; main_handle := Frm_H;<br>&nbsp; CR_flag := $0d;<br>&nbsp; setflag := 0;<br>&nbsp; &nbsp; //检查哪个窗口掌握光标<br>&nbsp; &nbsp; GetCursorPos(pt);<br>&nbsp; &nbsp; h_Wnd := WindowFromPoint(pt);<br>&nbsp; &nbsp; //获得光标所有者的线程ID<br>&nbsp; &nbsp; dwThreadID := GetWindowThreadProcessId(h_Wnd,nil);<br>&nbsp; &nbsp; //获得当前线程的ID<br>&nbsp; &nbsp; dwCurrentThreadID := GetCurrentThreadId();<br>&nbsp; &nbsp; old_hkl := GetKeyBoardLayOut(0); &nbsp;//保存旧的HKL<br>&nbsp; &nbsp; if (dwCurrentThreadID &lt;&gt; dwThreadID) then &nbsp;//如果输入焦点在别的线程<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if AttachThreadInput(dwThreadID,dwCurrentThreadID,True) then //连接线程<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; h_Wnd := GetFocus();<br>&nbsp; &nbsp; &nbsp; &nbsp; if outlen = 1 then //如串口内容为单个字符<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num[0] := str[0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ord(num[0]) = $08 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keybd_event( VK_BACK,VK_BACK,KEYEVENTF_EXTENDEDKEY,0 );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keybd_event( VK_BACK,VK_BACK,KEYEVENTF_KEYUP,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end else if ord(num[0]) = $0A then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keybd_event( VK_RETURN,VK_RETURN,KEYEVENTF_EXTENDEDKEY,0 );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keybd_event( VK_RETURN,VK_RETURN,KEYEVENTF_KEYUP,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendMessage(h_Wnd,WM_IME_CHAR,ord(num[0]),0);<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else if outlen = 2 then &nbsp;//如串口内容为双字节,打开汉字输入法<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if screen.Imes.Count = 0 then &nbsp; //screen.Imes.Count 得到系统中文输入法个数<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showMessage(addIME);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not ImmIsIME(GetKeyBoardLayOut(0)) then //判断是否在中文状态,若不是<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myhkl:=hkl(screen.Imes.objects[screen.Imes.Count-1]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //得到objects[screen.Imes.Count-1]对应的HKL句柄<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activatekeyboardlayout(myhkl,KLF_ACTIVATE);//让得到的HKL句柄起作用<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setflag :=1 ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i:=0 to 1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PostMessage(h_Wnd,WM_CHAR,WPARAM(str),1); &nbsp; //使用WM_CHAR信号<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if setflag = 1 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//如打开了新的中文输入法,则关闭它<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activatekeyboardlayout(old_hkl,KLF_ACTIVATE);//让旧的HKL句柄起作用。<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setflag := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AttachThreadInput(dwThreadID,dwCurrentThreadID,False); //解除线程连接<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>end;
 
http://www.2ccc.com/article.asp?articleid=942
 
后退
顶部