如何给其他程序的工具栏上的按钮发送点击消息用代码示之~~~谢谢!

解决方案 »

  1.   

    什么意思,你的意思是击工具栏中的按扭, 执行代码功能两次
    var
      i:integer;
    begin
      i:=1;
     if i<=2 then
       begin
        ShowMessage('信息提示');
        i:=i+1;
       end;
    end;
      

  2.   

    转自DelphiBBS:第一步:找到Button所在窗口句柄:
    var HWin: THandle;
    HWin := FindWindow(nil, '程序Caption');
    第二步:找到Button句柄:
    EnumChildWindows(HBtn, @EnumerateChildWindows, 0);
    第三步:发送消息:
    SendMessage(HBtn, WM, 0, 0);程序参考:
    //回调函数,列出所有子窗口句柄
    var
      HBtn: THandle;function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL;
    var
      WindowCaption: Array[0..254] of Char;
    begin
      GetWindowText(Hwnd, WindowCaption, 255);
      if WindowCaption = '按钮Caption' then 
        HBtn:= Hwnd;
      Result := true;
    end;procedure TFindForm.Button2Click(Sender: TObject);
    var
      H: HWnd;
      I: Integer;
    begin
      H := FindWindow(nil, '程序Caption');
      if H = 0 then
        Exit;
      EnumChildWindows(H, @EnumerateChildWindows, 0);
      SendMessage(HBtn, BM_CLICK, 0, 0);
    end;
      

  3.   

    gobiz(……) ,先谢谢您的恢复。我的意思从 delphi程序中调用 别的已经编译完的程序(无源码) 的工具栏上的按钮 。并且 用 spy++ 找不到 工具栏按钮的句柄 ,只有工具栏的句柄。
      

  4.   


    3#代码很完整啊,最後一句SendMessage就是给指定的按扭发点击的消息了
    只要知道程序窗口的名称和按扭的名称即可。不必关心按扭在那个位置的
      

  5.   

    procedure TForm5.btn2Click(Sender: TObject);
    var  han,h1,h2,h3:Integer;
     c1,c2,c3,c4:Integer;begin
      han:=FindWindow(0,PChar('Form4'));
      if IsWindow(han) then
      begin
          h1 := FindWindowEx(han, 0, 'TToolBar', nil);
          //SendMessage(h1,WM_SETTEXT,255,Integer(PChar('我来测度了1')));
          if h1<>0 then
          begin
            c1 := FindWindowEx(h1, 0, 'TToolButton', nil);
            if c1<>0 then
            begin
              c1 := FindWindowEx(h1, 0, 'TToolButton', nil);
              SendMessage(c1, BM_CLICK, 0, 0);
            end;
          end;
      end;
    end;
    我说的不是Button,是ToolButton。无法找到句柄
      

  6.   

    我说的是ToolButton,不是Button。Button 的句柄能找到,ToolButton 不能。