• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

delphi有关获取其他程序的窗口及对窗口内控件的操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab函数:residue和residuez的用法发布时间:2022-07-18
下一篇:
用MATLAB做矩阵运算发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap