各位大虾:  我想从给定标题的窗口里,定位给定标题的一个标签按钮。如何实现,我的参考程序代码如下。针对我的程序代码,本人有点迷茫,请各位大虾指点迷途:procedure TForm1.Button2Click(Sender: TObject);
var
  win_cap,          //待查找的主窗体标题
  btn_cap: string;  //待查找的标签标题  hd,               //待查找的主窗体句柄
  hdl: hwnd;        //遍历主窗体各子控件句柄  ctl_text: array[0..254] of char; //各子控件标题
begin
  win_cap := 'flash 作品欣赏 - Microsoft Internet Explorer';
  btn_cap := '投我一票';  //获取主窗体句柄
  hd := FindWindow(nil,pchar(win_cap));  //获取成功则提取主窗体第一子控件窗口句柄
  if hd >0 then
    hdl := getWindow(hd,GW_CHILD+GW_HWNDFIRST);  //遍历主窗体各子控件
  while hdl > 0 do
  begin
    //获取子控件标题
    if GetWindowText(hdl, @ctl_text, 255)>0 then
    begin
      //与待定位按钮标题比较,一致则查找成功
      if pos(btn_cap,StrPas(@ctl_text))>0 then
        showmessage('哈哈,就是你...');
    end;
    hdl := getwindow(hdl,GW_HWNDNEXT);
  end;  showMessage('结束了...');
end;1、为何GetWindowText总是返回一个空字符串,无任何信息
2、如何正确定位到该控件,如果有懂的,一定要将程序代码调试无误,本人对分绝不吝啬!  谢谢大家!~

解决方案 »

  1.   

    http://expert.csdn.net/Expert/topic/2981/2981509.xml?temp=.8596002
      

  2.   

    function EnumChildProc(Wnd: hWnd; SL: TStrings): BOOL; stdcall; 
    var 
      szFull: array[0..MAX_PATH] of Char; //Buffer for window caption 
    begin 
      Result := Wnd <> 0; 
      if Result then  
      begin 
        GetWindowText(Wnd, szFull, SizeOf(szFull)); // put window text in buffer 
        if (Pos(SL[0], StrPas(szFull)) > 0) // Test for text 
          and (SL.IndexOfObject(TObject(Wnd)) < 0) // Test for duplicate handles 
          then SL.AddObject(StrPas(szFull), TObject(Wnd)); // Add item to list 
        EnumChildWindows(Wnd, @EnumChildProc, Longint(SL)); //Recurse into child windows 
      end; 
    end; function ClickButton(ParentWindow: Hwnd; ButtonCaption: string): Boolean; 
    var 
      SL: TStringList; 
      H:  hWnd; 
    begin 
      SL := TStringList.Create; 
      try 
        SL.AddObject(ButtonCaption, nil); // First item in list is text to find 
        EnumChildWindows(ParentWindow, @EnumChildProc, Longint(SL)); 
        H := 0; 
        case SL.Count of 
          1: ShowMessage('Window text not found.'); 
          2: H := hWnd(SL.Objects[1]); 
          else  
            ShowMessage('Ambiguous text detected.'); 
        end; 
      finally 
        SL.Free; 
      end; 
      Result := H <> 0; 
      if Result then PostMessage(H, BM_CLICK, 0, 0); 
    end; 
      

  3.   

    調用:
    ClickButton(hdl, btn_cap);
      

  4.   

    你可以找到有HANDLE的东西.不过TLabel没有HANDLE,所以用这个办法找不齐全吧??便利窗体上所有控件.如果这个控件是TWincontrol,就继续找它的子控件.你可以找到全部,包括TLabel.....
    for I := 0 to ComponentCount - 1 do
      begin
        if (Components[I] is TLabel) then
        begin
           TLabel(Components[I]].left:=0;
        end;
    /// ComonentCount 属性好象只有TWinControl 派生类有.
      

  5.   

    你的好象不行。算了,告诉我全部思路。1、打开IE,URL为http://flash.163.com/show_one.php?idno=759252、查找该窗体里的“投我一票”按钮,模拟鼠标点击它一次3、将电击后新打开的窗体关闭如何实现这种功能,最好有完整的测试代码,谢谢啦!~楼上的你的我测试过,怎么不行啊。对于这种标签按钮,好象不支持吧
      

  6.   

    早說啊, 根本不是一回事
    用 WebBrowser1 來控制procedure TForm1.Button1Click(Sender: TObject); 
    var  
      ovElements: OleVariant;  
      i: Integer;  
    begin  
      ovElements := WebBrowser1.OleObject.Document.forms.item(0).elements;  
      for i := 0 to (ovElements.Length - 1) do 
        if (ovElements.item(i).tagName = 'INPUT') and 
          (ovElements.item(i).type = 'SUBMIT') and 
          (ovElements.item(i).Value = 'Recent Charges') then 
          ovElements.item(i).Click;  
    end; 
      

  7.   

    //用一个TWebBrowser,装那个网页进去
    procedure TForm1.Button1Click(Sender: TObject);
    var
      ov : OleVariant;
    begin
      ov := wb.OleObject.Document.All.Item('btnVote', 0);   //找到那个按钮
      ov.OnClick;
    end;
      

  8.   

    改进楼上的,楼主是用来在网页灌水的吧 :P
      for i := 0 to (ovElements.Length - 1) do
      begin
        if (ovElements.Item(i).tagName = 'INPUT') and (ovElements.Item(i).type = 'radio') and
           (ovElements.Item(i).Value = 'c') then
        begin
          ovElements.item(i).Checked := true;
          wb.OleObject.Document.Forms.Item(0).Submit;
        end;
      

  9.   

    测试过,wb.OleObject.Document.Forms.Item(0).Submit;
    要换成ov := wb.OleObject.document.all.item('submsg', 0); {按名找到那个发送按钮}
          ov.Click;  {这样表单就submit了,用上面的.Submit好象不是想要的结果}