delphi有关获取其他程序的窗口及对窗口内控件的操作
转http://blog.sina.com.cn/s/blog_576b7081010130tx.html
1.获取当前所有窗口
procedure TForm1.Button1Click(Sender: TObject); var szText: array[0..254] of char; hCurrentWindow :hwnd; begin hCurrentWindow := GetWindow(Handle,GW_HWNDFIRST); while hCurrentWindow <> 0 do begin if GetWindowText(hCurrentWindow ,@szText,255) > 0 then memo1.lines.Add(StrPas(@szText)); hCurrentWindow := GetWindow(hCurrentWindow,GW_HWNDNEXT); end; end;
2.获取某一指定窗口
procedure TForm1.Button1Click(Sender: TObject); var szText: array[0..254] of char; hCurrentWindow :hwnd; begin hCurrentWindow := GetWindow(Handle,GW_HWNDFIRST); while hCurrentWindow <> 0 do begin if GetWindowText(hCurrentWindow ,@szText,255) > 0 then if pos('RTX 会话', StrPas(@szText))<>0 then //这里指定某一窗口,可能有多个 memo1.lines.Add(StrPas(@szText)); hCurrentWindow := GetWindow(hCurrentWindow,GW_HWNDNEXT); end; end;
3.获取某一窗口内的所有控件及其内容
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; memo1: TMemo; procedure Button1Click(Sender: TObject); procedure get_actrlh(h:hwnd); private { Private declarations } public { Public declarations } end; var Form1: TForm1; hi:integer; implementation {$R *.dfm} function gettext(h:hwnd):string; var name:string; txtlen:integer; begin TxtLen:=sendmessage(h ,wm_gettextlength,0,0)+1; setlength(name,TxtLen); sendmessage(h ,wm_gettext,txtlen,LongInt(@name[1])); result:=name; end; procedure tform1.get_actrlh(h:hwnd); var s: Array[0..255] of char; begin h:=GetWindow(h,GW_child); while h>0 do begin GetClassName(h, s, 256); begin memo1.lines.Add(inttostr(hi)+':'+s+':'+trim(gettext(h)) ); end ; hi:=hi+1; get_actrlh(h); h:=GetWindow(h,GW_HWNDNEXT); end; end; procedure TForm1.Button1Click(Sender: TObject); var szText: array[0..254] of char; hCurrentWindow :hwnd; begin hCurrentWindow := GetWindow(Handle,GW_HWNDFIRST); while hCurrentWindow <> 0 do begin if GetWindowText(hCurrentWindow ,@szText,255) > 0 then if pos('RTX 会话', StrPas(@szText))<>0 then //这里指定某一窗口,可能有多个 begin memo1.lines.Add(StrPas(@szText)); hi:=0; get_actrlh(hCurrentWindow);//获取窗口中的所有控件 end; hCurrentWindow := GetWindow(hCurrentWindow,GW_HWNDNEXT); end; end; end.
4.获取窗口中的某个控件,并对其操作
首先用上面3中的程序,根据窗口中控件的特性,找到你要操作的控件的序号,
即memo1中左边的数字,用变量hnum保存这一序号,再把上面3中的程序稍作修改如下,
以下其实是一个完整的、功能简单的、没有经过优化的腾讯通RTX自动回复消息程序:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; memo1: TMemo; procedure Button1Click(Sender: TObject); procedure get_actrlh(h:hwnd); private { Private declarations } public { Public declarations } end; var Form1: TForm1; hi,hnum:integer; implementation {$R *.dfm} function gettext(h:hwnd):string; var name:string; txtlen:integer; begin TxtLen:=sendmessage(h ,wm_gettextlength,0,0)+1; setlength(name,TxtLen); sendmessage(h ,wm_gettext,txtlen,LongInt(@name[1])); result:=name; end; procedure tform1.get_actrlh(h:hwnd); var s: Array[0..255] of char; begin h:=GetWindow(h,GW_child); while h>0 do begin GetClassName(h, s, 256); begin memo1.lines.Add(inttostr(hi)+':'+s+':'+trim(gettext(h)) ); if hi=hnum then begin memo1.lines.Add('上面为找到的控件'); SendMessage(h,WM_SETTEXT,0,Integer(pchar('*此为RichEdit20W所赋的文本'))); end; end ; hi:=hi+1; get_actrlh(h); h:=GetWindow(h,GW_HWNDNEXT); end; end; procedure TForm1.Button1Click(Sender: TObject); var szText: array[0..254] of char; hCurrentWindow :hwnd; begin hCurrentWindow := GetWindow(Handle,GW_HWNDFIRST); memo1.Clear; while hCurrentWindow <> 0 do begin if GetWindowText(hCurrentWindow ,@szText,255) > 0 then if pos('RTX 会话', StrPas(@szText))<>0 then //这里指定某一窗口,可能有多个 begin memo1.lines.Add(StrPas(@szText)); hi:=0; hnum:=3; //此为所要赋值的RichEdit20W控件在窗口中的序号 get_actrlh(hCurrentWindow);//获取窗口中的所有控件 //以下向窗口发送ALT_S 组合键,实现腾讯通RTX发送消息 SetForegroundWindow(hCurrentWindow);//设置窗口为当前窗口 keybd_event(VK_menu,MapVirtualKey(VK_menu,0),0,0); sleep(1000); keybd_event(83,MapVirtualKey(83,0),0,0); sleep(1000); keybd_event(83,MapVirtualKey(83,0),KEYEVENTF_KEYUP,0); keybd_event(VK_menu,MapVirtualKey(VK_menu,0),KEYEVENTF_KEYUP,0); end; hCurrentWindow := GetWindow(hCurrentWindow,GW_HWNDNEXT); end; end; end.

浙公网安备 33010602011771号