delphi有關獲取其他程序的窗口及對窗口內控件的操作


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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM