我自学delphi不久,很菜,请各位大虾们帮下忙!
用一个软件,因为要频繁的点击一按钮,所以想用句柄加个timer让它自动点击!
我的代码是这样的,我也是在网上找些代码自己修改的,不过总是不行!我先贴出我的代码,如果那里错了,请你们指正出来,把正确的代码补上来!有个完整的代码再加上注释那再好不过了!
小弟先在这里谢谢了!
满意结贴!
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    Timer1: TTimer;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject);
var
hButton,Handle:THandle;
begin
    Handle :=FindWindowEx(GetForegroundWindow,hButton,'软件caption',nil);
    hButton := FindWindowEx(Handle, THandle(nil),nil,'操作'); //这个操作按钮handle会变,每次打开都不一样
    SendMessage(hButton, WM_LBUTTONDOWN, 0, 0);
    SendMessage(hButton, WM_LBUTTONUP, 0, 0);
    sleep(StrToInt(form1.Edit1.text));  //间隔点击
end;procedure TForm1.Button1Click(Sender: TObject);
begin
timer1.Enabled:=true;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
timer1.Enabled:=false;
end;end.

解决方案 »

  1.   

    抓到窗口后需要用回调函数,就可以操作了,有时候你在窗口上看到的按钮名并不一定能准确抓到那个按钮,建议下一个进程间谍之类的软件(事实上这些软件也用了类似方法),先把那个窗口上的句柄和按钮句柄、类名记下,然后在去抓。回调函数:
    function EnumWindowsProc(AhWnd:LongInt;lParam:LongInt):boolean;stdcall;
    TFNWndEnumProc其实就是指针类型。其中的lpEnumFunc就是回调函数的入口地址了。function EnumWindows(lpEnumFunc: TFNWndEnumProc; lParam: LPARAM): BOOL; stdcall;
    EnumWindows函数的功能是:枚举屏幕上所有程序中的顶层窗口。可以把程序放在后台,或者用HBO...这个就有点像流氓软件了给你个简单例子吧
    interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls;type TForm1 = class(TForm) 
    Memo1: TMemo; file://用来显示找到的控件 Label1: 
    TLabel;  
    Edit1: TEdit;  
    file://输入标题. 
    Button1: TButton; procedure 
    Button1Click(Sender: TObject); 
    private { Private declarations } 
    public { Public declarations } 
    end;var Form1: TForm1;function EnumChildWndProc(AhWnd:LongInt; AlParam:lParam):boolean;stdcall;implementation
    {$R *.dfm} 
    function EnumChildWndProc(AhWnd:LongInt; AlParam:lParam):boolean;stdcall; 
    var WndClassName: array[0..254] of Char; WndCaption: array[0..254] of Char; 
    begin 
    GetClassName(AhWnd,wndClassName,254); 
    GetWindowText(aHwnd,WndCaption,254); 
    with form1.memo1 do begin lines.add(string(wndClassName)); 
    lines.add( string(wndCaption)); 
    lines.add('-------'); 
    end; result:=true; 
    end;
    procedure TForm1.Button1Click(Sender: TObject); var hWnd:LongInt; begin 
    memo1.Lines.Clear; Memo1.Lines.Add(Edit1.Text+' 有如下控件类名称'); 
    hWnd:=FindWindow(nil,pchar(Edit1.Text)); if hWnd<>0 then begin 
    EnumChildWindows(hWnd,@EnumChildWndProc,0); end else 
    MessageBox(self.handle,'没找到该窗口句柄','提示',0); end;end.
    //修改数据或点击,只要改第二个参数就可以了
    SendMessage(hWnd,WM_SETTEXT,0,LongInt(Pchar('哈哈!')));
      

  2.   

    [Quote=引用 2 楼 cdesigns 的回复:]
    谢谢你的回答,不过期待更好的回复
      

  3.   

      SendMessage(句柄,BM_CLICK,0,0);
    这样就OK了