目的是这样的,一个已知程序(非我所写),我想自己写个程序去控制那个程序,发消息吧!
我想发个程序中一个按钮单击事件,首先好获得窗口的handle,然后得获得哪个按钮的handle,请问大虾们,怎么实现啊?
最好能贴出源代码,先谢过了!

解决方案 »

  1.   

    可以用FindWindow获得该程序的Handle,也可以通过WindowFromPos来获得
      

  2.   

    同意楼上。
    Wnd:Hwnd
    ExeTitle: pchar
    Wnd  :=  FindWindow(nil,  ExeTitle);
    button直接取Handle就可以。
    Wnd := button.handle;
      

  3.   

    findwindow(nil,'程序名称');//得到窗体句柄
    enumwindows(@函数名,0);//枚举所有窗体中的子窗体
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      h:hwnd;
    implementation{$R *.dfm}
    procedure pro(var h:hwnd;LPARAM:lParam);  //回调函数
    begin
      if h<>0 then
      form1.ListBox1.Items.Add(inttostr(h));//加入到LISTBOX1
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
     h:=findwindow(nil,'form1');
     EnumChildWindows(form1.Handle,@pro,0);//枚举窗口中的子窗体
    end;end.
      

  4.   

    如果就是想控制某一个按钮的话可以看这个例子
    var h:hwnd;
    h:=findwindow(nil,'form1');//找到
    sendmessage(h,WM_CLOSE,0,0);//关闭
      

  5.   

    var
      Handle, ChildHandle: HWND;
      clsName: pchar;
      text:array[0..255] of char;
    begin
      getmem(clsName, 255);
      Handle := FindWindow(pchar('SciCalc'), nil);
      if Handle <> 0 then
        begin
          ChildHandle := GetWindow(Handle, GW_Child);
          while ChildHandle <> 0 do
            begin
              GetClassName(ChildHandle, clsName, 255);
              //showmessage(clsName);
              if string(clsName) = 'Static' then
                begin
                getwindowtext(ChildHandle,text,255);
                showmessage(text);
                end;
              ChildHandle := GetWindow(ChildHandle, GW_HWNDNEXT);
            end;
        end;
      freemem(clsName, 255);
    end;
      

  6.   

    完全同意:
            findwindow(nil,'程序名称');//得到窗体句柄
            enumwindows(@函数名,0);//枚举所有窗体中的子窗体
      

  7.   

    不对吧楼主说要取得窗口中特定的控件的Handle。怎么可以使用EnumWindows呢。
    刚才写的:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        TreeView1: TTreeView;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure EnumChildWindows(Node:TTreeNode;hParent:hwnd);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    function _hasChild(handle:hwnd):boolean;
    begin
        Result:=FindWindowEx(handle,0,nil,nil)<>0;
    end;
    procedure TForm1.EnumChildWindows(Node:TTreeNode;hParent:hwnd);
    var hWindow:hwnd;s:PChar;child:TTreeNode;
    begin
        hWindow:=FindWindowEx(hParent,0,nil,nil);
        while hWindow>0 do
        begin
            GetMem(s,500);
            GetClassName(hWindow,s,500);
            child:=TreeView1.Items.AddChild(Node,Format('%x [%s]',[hWindow,String(s)]));
            freemem(s);
            if _hasChild(hWindow) then
                EnumChildWindows(child,hWindow);
            hWindow:=FindWindowEx(hParent,hWindow,nil,nil);
        end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var t:TTreeNode;hWindow:hwnd;s:String;
    begin
        TreeView1.Items.Clear ;
        hWindow:=FindWindow(nil,'Win32 SDK/API - Microsoft Internet Explorer');
        SetLength(s,200);
        GetClassName(hWindow,PChar(s),200);
        t:=TreeView1.Items.AddFirst(nil,s);
        EnumChildWindows(t,hWindow);
    end;end.
      

  8.   

    需要一个TreeView和一个Button。就可以将指定的窗体的所有控件(包括控件的子控件)全部列举出来,
      

  9.   

    呵呵,写错了,不过我的代码子中用的是
    EnumChildWindows(form1.Handle,@pro,0);//枚举窗口中的子窗体
      

  10.   

    FindWindow, FindWindowex 就可以了!
      

  11.   

    一个相关问题:
      Delphi中很多控件是没有handle,比如TLable就是一个典型。要对他发送消息肯定不行,楼上各位对于没有handle控件如何对他们进行操作
      

  12.   

    TLabel不是从TWinControl继承下来的阿。呵呵,只有从TWinControl继承下来的空间才有句柄(当然,我说VCL,如果你自己硬是要从TObject或其他类继承下来带句柄的我也没有话说)。TLabel是画出来的