如何向一个外部程序输入中文字符串,此外部程序有多个Edit。谁有经验? (100分)

  • 主题发起人 主题发起人 HookBoy
  • 开始时间 开始时间
H

HookBoy

Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
如何向一个外部程序输入中文字符串,此外部程序有多个Edit。只要向有光标的Edit中输入中文字符串。<br>我曾模拟按键输入了英文字符,可中文字符不好办了。<br>用SendMessage的话,获得该handle不太好办,<br>我在考虑 &nbsp;<br>GetCaretPos &nbsp; 得到光标位置<br>GetWindowsEx &nbsp; &nbsp;从光标处得到该Edit的句柄<br>SendMessage &nbsp; &nbsp; 发送消息<br>但不好办,谁有经验?<br>有没有采用Clipboard技术的?<br>
 
我看了许多贴子,都不合我意。<br>许多人是用模拟按键,但中文不好按的。<br>有些是通过枚举产生Edit的handle的,但是向所有Edit发送。<br>所以我期待答案。
 
我想把这个问题改成300分,怎么办?
 
只要向有光标的Edit中输入中文字符串。<br><br>我用的比较笨的方法,不过有效:<br>那就是通过剪贴板,在发送前把字符串存入剪贴板,发送时直接发送 Ctrl+V 的虚拟按键<br>就可以了:) <br><br>// uses Clipbrd; 记得引用 Clipbrd 单元<br><br>procedure SendPaste; // 发送 Ctrl + V 的虚拟按键进行粘贴<br>begin<br>&nbsp; keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0);<br>&nbsp; keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), 0, 0);<br>&nbsp; keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), KEYEVENTF_KEYUP, 0);<br>&nbsp; keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0);<br>end;<br><br>发送(我是在一个 Timer 里面):<br><br>&nbsp; Clipboard.AsText := '大富翁论坛';<br>&nbsp; SendPaste;<br><br>搞定。<br><br>
 
// 我想把这个问题改成300分,怎么办?<br>另外开个帖子再给 200 分 [:D]<br><br>
 
var<br>&nbsp; p:pchar;<br>begin<br>&nbsp; p:='中文';<br>&nbsp; sendmessage(edithandle,wm_settext,0,integer(pointer(p)));<br>&nbsp; // sendmessage(edit1.handle,wm_settext,0,integer(pointer(p)));//程序自身的测试<br>
 
beta的方法是有效的,只是这样做粗了点,先记上100分,看看还有没有更好的方法。<br><br>jsxjd,我就是在愁edithandle不好确定。
 
to HookBoy:不是所有控件都有 Handle 的,特别是在 IE 里面<br>&nbsp; 所以用 SendMessage 恐怕不行:(<br><br>
 
发送WM_IME_CHAR行不行呀? 这样一次可以发送一个中文字
 
Pearl:<br>&nbsp; 问题在于,不好找Handle.
 
function input_process(var str : array of char ; outlen : integer) : 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; old_hkl ,myhkl: HKL;<br>&nbsp; setflag : integer;<br>begin<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(dwCurrentThreadID,dwThreadID,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; 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; showMessage('not chinese!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myhkl:=hkl(screen.Imes.objects[screen.Imes.Count-2]);<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; &nbsp; sleep(1000);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setflag :=1 ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendMessage(h_Wnd,WM_IME_CHAR,ord(str[0]) shl 8 + ord(str[1]),0);<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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AttachThreadInput(dwCurrentThreadID,dwThreadID,False); //解除线程连接<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; end;
 
sanmzhou:<br>&nbsp; 你的意思是在这样调用<br>input_process( '我的文字',2);<br>是不是?<br>这个outlen有点烦,如果我要发送 '我的abc文字' ,outlen怎么定义?<br><br>还有我调试时出现了 Ambiguous overloaded call to 'GetWindow ThreadProcessId'<br>怎么回事?
 
除了IE中的Editbox 比较特殊外,其它应用程序的Edit应该都是有handle 的,<br>而对于IE中的Editbox应该采用 DHtml 对象模型进行处理!!!!!!
 
问题在于,不好找Handle.<br>使用GetFocus()函数获得光标所在处的Handle
 
sanmzhou的方法有点问题。<br>
 
可以使用自定义消息实现,在第一个程序中发送自定义消息给另一程序,第二个程序接收该消息!<br>实现过程有点复杂,在书《delphi5开发人员指南》中有例子实现了你所说的功能,例子挺好,建议你买那本书,<br>如果需要我可以发给你:xinlp@sino-bridge.com
 
readxlp:<br>可是我要的是对别人已经编好的程序进行消息处理。不是两个我编写的程序。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部