一个窗体form1,form1上面有2个panel,panel都没有名称,第二个panle上面有个按钮button1,怎样找到button1的句柄呢?
h1:=FindWindow(NIL,'Form1); 
h1:=FindWindowEx(h1,0,nil,'');//这边没有名称  这边要怎么改呢?
h_button1:=FindWindowEx(h1,0,nil,'button1'); 

解决方案 »

  1.   

    那就只能盲目的全遍历,找出Button1
      

  2.   

      这里向楼主请教,你的两个panel都没名称是指的name属性都是空的吗?还是指它们的caption属性为空?
      如果是name属性为空的话,那么是否你的窗口上面还有别的panel存在,而且name不为空?
      我问这个其实有点离题,主要是因为,一个form上的任一类型控件必须要有一个name属性不为空才可能编译通过,否则会报错的。所以做了一个猜测。
      至于你的问题,我滴回答是:可以获取。
      下面是一个通过获取鼠标位置,获取该控件句柄的代码。你可以借鉴一下。
      
    procedure TForm1.Timer1Timer(Sender: TObject);
    var
    Pos: TPoint;
    Handle: HWND;
    ScreenDC: HDC;
    Buf: array[0..1024] of Char;
    ScreenColor: COLORREF;
    begin
      GetCursorPos(Pos); 
      Handle := WindowFromPoint(Pos);                     // 返回当前位置的句柄
      HandleText.Caption := IntToStr(Handle);
      GetClassName(Handle, Buf, 1024);                    // 得到类名
      ClassNameText.Caption := Buf;
      SendMessage(Handle, WM_GETTEXT, 33, Integer(@Buf)); // 得到标题
      TitleText.Caption := Buf;
    end;记得放timer控件,嘿嘿。
      

  3.   

    h1:=FindWindowEx(h1,0,nil,'');改为h1:=FindWindowEx(h1,0,'Button',nil);

    h1:=FindWindowEx(h1,0,'TButton',nil);没有名称可以按类名来查。如果上述代码不行,建议你先用spy++查看一下按扭的类名,把类名填上去。
      

  4.   

    没有名称,系统也会默认给一个名称的,你用Spy4Win先获取名称,然后查找。
      

  5.   

    这样只能找到第一个panel啊,按钮却在第二个panel上面
    var
        h1: Windows.HWND;
    h_button1: Windows.HWND;
    begin
    h1:=FindWindow(NIL,'标题');  
    h1:=FindWindowEx(h1,0,'TPanel',nil);
    h_button1:=FindWindowEx(h1,0,nil,'Button1');
    if  h_button1<>0 then
    ShowMessage('找到');
    end;
      

  6.   

    为什么我循环只能找到第二级呢?  不能到第三级var   hMain:HWND;
    hChild:HWND;
    buffer:array[1..200]   of   char;
    begin
      hMain:=FindWindow(nil, '标题');
      hChild:=FindWindowEx(hMain,0,nil,nil);
      while   hChild<>0   do
      begin
        GetWIndowText(hChild,@buffer,200);
        ListBox1.Items.Add(StrPas(@Buffer));
        hChild:=FindWindowEx(hMain,hChild,nil,nil);//look   for   next   control
      end;
    end;
      

  7.   


    是cation属性为空
    spy++好像不能看到name属性
      

  8.   

    handle := (Form1.FindComponent('button1') as TButton).Handle;
      

  9.   


      for i := 0 to self.ScrollBoxInfo.ComponentCount - 1 do
      begin
        if self.ScrollBoxInfo.Components[i] is TCheckBox then
        begin
          if TCheckBox(self.ScrollBoxInfo.Components[i]).Checked then
          begin
            MoveUnit.strInfo.Add(TCheckBox(self.ScrollBoxInfo.Components[i]).Caption);
            if TCheckBox(self.ScrollBoxInfo.Components[i]).Color = clTeal then
               MoveUnit.strInfoEnd.Add(TCheckBox(self.ScrollBoxInfo.Components[i]).Caption)
            else
               MoveUnit.strInfoEnd.Add('gb');
          end
          else
          begin
             MoveUnit.strInfo.Add('null');
             MoveUnit.strInfoEnd.Add('gb');
          end;    end;
      end;自己原来的代码 没修改
      

  10.   

    为什么都用FindWindow呢?用EnumWindow和EnumChildWindow更好吧,然后判断下所有父句柄中是否有panel的句柄就行了。
      

  11.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      i, k: Integer;
      Panel: TPanel;
    begin
      for i := 0 to self.ControlCount - 1 do
      begin
        if Self.Controls[i] is TPanel then
        begin
          Panel := TPanel(Self.Controls[i]);
          for k := 0 to Panel.ControlCount - 1 do
          begin
            if Panel.Controls[i] is TButton then
            begin
              if TButton(Panel.Controls[k]).Name = 'Button1' then
              begin
                //找到了
                TButton(Panel.Controls[k]).Caption := '找到了';
              end;
            end;
          end;
        end;
      end;
    end;
      

  12.   

    这个兄弟的方法可以实现···EnumWindow和EnumChildWindow 可以遍历所有的控件,直到找到你要的为止,具体用法可以网上搜下,还是有很多的