给一个程序输入内容(50分)

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

hujunyi

Unregistered / Unconfirmed
GUEST, unregistred user!
用D4写一个程序,给一个已经打开的写字板输入内容(中英文件字符)
 
我记得以前的贴子里有过,你要问问cAkk,我记得的一个是<br>&lt;a ref = http://www.delphibbs.com/delphibbs/dispq.asp?lid=208536&gt;
 
先用 FindWindow 找到其句柄 aHandle,然后:<br>SendMessage(aHandle, wm_SetText, 0, PCHAR('Hello world'));
 
楼上的老兄您试过了吗?<br>我用PostMessage(MyHWND, WM_CLOSE, 0, 0);是能关闭我打开的写字板程序的,<br>可是SendMessage(MyHWND, wm_SetText, 0, ord(Str));或<br>SendMessage(MyHWND, wm_SetText, ord(Str, 0));都无法向写<br>字板输入内容。
 
//唉,50分,我调试很才时间的<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp;function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>&nbsp; var<br>&nbsp; &nbsp; buffer: array[0..255] of Char;<br>&nbsp; begin<br>&nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; //得到目标窗口的控件<br>&nbsp; &nbsp; GetClassName(hwnd,buffer,256);<br>&nbsp; &nbsp; //找到发消息的目标窗口的目标控件<br>&nbsp; &nbsp; if StrPas(Buffer)='Edit' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; PInteger(lparam)^ := hwnd; //得到目标控件的Hwnd(句柄)<br>&nbsp; &nbsp; &nbsp; Result:=False; &nbsp;//终止循环<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>var<br>&nbsp; Handle: Integer;<br>&nbsp; buffer: Array[0..1023] of Char;<br>begin<br>&nbsp; Handle := FindWindow('NotePad',nil);<br>&nbsp; if Handle&lt;&gt;0 then<br>&nbsp; &nbsp; begin //<br>&nbsp; &nbsp; &nbsp; EnumChildWindows(Handle,@EnumChildWindowsProc,Integer(@Handle));<br>&nbsp; &nbsp; &nbsp; //下面这句会删去原写字板的内容,再写字符串;<br>&nbsp; &nbsp; &nbsp; //如不想删,我改为<br>&nbsp; &nbsp; &nbsp;// &nbsp; &nbsp;S:='字符串'<br>&nbsp; &nbsp; &nbsp;// &nbsp;SendMessage(Handle, WM_CHAR, Ord(s), 0); &nbsp;<br>&nbsp; &nbsp; &nbsp;//要用一个FOR啊<br>&nbsp;<br>&nbsp; &nbsp; &nbsp; SendMessage(Handle,WM_SETTEXT,0,Integer(pchar('Your String')));<br><br>&nbsp; &nbsp; end;<br>end;<br>
 
&nbsp;我想上面的方法都不错,但是好像都不能实现hujunyi所要求的<br>输入中文字符,我以前看过一篇有关用wm_settext输入中文字符的文章。可是<br>我试过了还是没有成功。不过我这倒有另外一种方法,实现中英文都能输入<br>我给出了一个完整的例子:<br>这个程序在运行之前必须先启动notepad(记事本)<br>,然后再拷贝一些东东到粘贴板去,点击button1即可粘贴到记事本.<br>程序中有详细的注释<br>unit notepad;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br><br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; jubing:Thandle;<br>function getchildText(hwnd:THandle;Param:Pointer):Boolean;stdcall;//自定义一个<br><br>// 回调调函数;<br>implementation<br><br>{$R *.DFM}<br>function getchildtext(hwnd:thandle;param:pointer):boolean;stdcall;<br>var buffer:array[0..255] of char;<br>begin<br>getclassname(hwnd,buffer,255); &nbsp;//得到类名,并存放在buffer中;<br>if string(buffer)='Edit' then<br>begin<br>jubing:=hwnd;<br>result:=false;<br>end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>sendmessage(jubing,wm_paste,0,0);//发出一个粘贴消息;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>jubing:=findwindow('notepad',nil);//通过类名发现记事本窗口的句柄;<br>//如果你要实现记事本操作可以用delphi自带winsight32查看一下他的类名。<br>if jubing&lt;&gt;0 then<br>EnumChildWindows(jubing,@getchildText,0);//列举该窗口所有的成员;<br>end;<br><br>end.<br><br>
 
//改一改,改为这样可以输入中文了吧。<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp;function EnumChildWindowsProc(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>&nbsp; var<br>&nbsp; &nbsp; buffer: array[0..255] of Char;<br>&nbsp; begin<br>&nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; //得到目标窗口的控件<br>&nbsp; &nbsp; GetClassName(hwnd,buffer,256);<br>&nbsp; &nbsp; //找到发消息的目标窗口的目标控件<br>&nbsp; &nbsp; if StrPas(Buffer)='Edit' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; PInteger(lparam)^ := hwnd; //得到目标控件的Hwnd(句柄)<br>&nbsp; &nbsp; &nbsp; Result:=False; &nbsp;//终止循环<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>var<br>&nbsp; Handle: Integer;<br>&nbsp; buffer: Array[0..1023] of Char;<br>&nbsp; i: integer;<br>&nbsp; s: string;<br>begin<br>&nbsp; Handle := FindWindow('NotePad',nil);<br>&nbsp; if Handle&lt;&gt;0 then<br>&nbsp; &nbsp; begin //<br>&nbsp; &nbsp; &nbsp; EnumChildWindows(Handle,@EnumChildWindowsProc,Integer(@Handle));<br>&nbsp; &nbsp; &nbsp; SendMessage(Handle, EM_SETSEL, 0, -1); &nbsp; //选取ALL<br>&nbsp; &nbsp; &nbsp; SendMessage(Handle, WM_CUT, 0, 0); &nbsp; &nbsp; &nbsp;//剪切到剪贴板<br>&nbsp; &nbsp; &nbsp; SendMessage(Handle,WM_SETTEXT,0,Integer(pchar('字符串')));<br>&nbsp; &nbsp; &nbsp; SendMessage(Handle, WM_PASTE, 0, 0); &nbsp; //从剪贴板贴回来<br>&nbsp; &nbsp; end;<br>end;
 
谢谢各位啦。
 
这个问题我还想进一步问一下,如果某个程序中有多个EDIT输入框,而且在输入完之后,<br>我还想单击一下某个按钮?<br>请问又该如何做呢?
 
后退
顶部