各位DX,如何在程序中控制另外一个软件的操作???是不是要利用Spy++来获得软件窗口中的某个控件的ID,然后通过发送消息的方法来操纵该控件???具体能给个简单的小例子吗??该方法是不是一定有效???

解决方案 »

  1.   

    是的:有的控件不能通过消息操纵,只有句柄才好控制,但也有例外的如toolbutton
    总之 枚举找句柄最好!
      

  2.   

    偶是说 找到句柄就可以发送消息了!那天怎么没贴上来 ,奇怪???类似这样吧:unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
     
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        function mygettext(hwnd:THandle):string;
      end;
     
    var
      Form1: TForm1;
    //  function EnumWindowsProc(Hwnd:THandle;lParam:LParam):boolean;Stdcall;
      function EnumChildProc(Hwnd:THandle;lParam:LParam):boolean;Stdcall;
    implementation
     
    {$R *.dfm}
    function TForm1.mygettext(hwnd:THandle):string;
    var
      buf:pchar;
      len:Longint;
    begin
     Len:=Longint(SendMessage(hWnd,WM_GETTEXTLENGTH ,0,0));
     if len>0 then
      begin//1
       try
        getmem(buf,len);
        SendMessage(hWnd,WM_GETTEXT,Len+1,Int64(buf));
        Result:=strpas(buf);
       finally
        freemem(buf);
       end;
      end;//1
    end;
     
    function EnumChildProc(Hwnd:THandle;lParam:LParam):boolean;
    var
     WindowCaption,WindowClass:array[0..254] of Char;
    begin
     GetClassName(Hwnd,WindowClass,255);
     if Pos('BUTTON',UpperCase(StrPas(WindowClass))) > 0 then
     begin
      // if Form1.mygettext(Hwnd)='Button1' then
      // begin
       sendmessage(hwnd,WM_LBUTTONDOWN,0,1);
       sendmessage(hwnd, WM_LBUTTONUP,0,1);
     //  end;
     end;
     Result := True;
    end;
     
    procedure TForm1.Button1Click(Sender: TObject);
    var
    h:hwnd;
    begin
      h := FindWindow(nil, 'MyForm');
      if h<>0 then
          begin
        //  sendmessage(h,WM_close,0,1);
           EnumChildWindows(H,@EnumChildProc,0);
          end
      else
          showmessage('No Form');
    end;
     
    end.
      

  3.   

    www.xmflyfish.com/awind/qqmsg.rar控制QQ自动发送消息的,原理同上面兄弟,看一下;有个叫spy 的软件不错,可以看Handle、Caption、Class等,去网上找一下