1

我从这里得到了以下代码并对其进行了一些更改,并且我也对原始问题进行了一些更改。

计时器间隔设置为 5000。
以下 3 个事件发生后,“Events OnTimer”程序将开始工作。

  • 1.WebBrowser1.Navigate('任何网页');
  • 2.等待加载
  • 3.以编程方式按下下载文件按钮

现在的问题是我找不到属于“另存为”对话框(或者是其子级)的“编辑”(类名)句柄。在下面的代码中,“编辑”的句柄为“0”,但如果我使用鼠标指针和以下代码:

HND:= WindowFromPoint(PNT);
Label1.Caption:= IntToStr(HND);

句柄给出一个结果。一旦我有了手柄,我就可以使用:

SetWindowText(EditHandle, 'test text'); 

更改“编辑”(类名)中的文本。

procedure TForm1.Timer1Timer(Sender: TObject);
Var
WHandle : HWND ;
ParentHandle : DWORD ;
P : Array[0..256] Of Char ;
ProcessIdActif : DWORD ;

begin
ProcessIdActif := 0 ;
GetWindowThreadProcessId (handle,@ProcessIdActif);
WHandle := FindWindow( Nil, Nil);
While (WHandle <> 0) Do
  begin 
    P[0] := #0;
    GetWindowText(WHandle, P, 255);
    if P[0] <> #0 then
      begin
        GetWindowThreadProcessId (WHandle,@ParentHandle);
        if ProcessIdActif = ParentHandle then
          begin
          if CompareText(p,'File Download') = 0 then
            begin
              ButtonHandle := FindWindowEx(WHandle, 0, 'Button', '&Save');
              if (ButtonHandle > 0) then
               PostMessage(ButtonHandle, BM_CLICK, 0, 0);       
            end
          else if CompareText(p,'Save As') = 0 then
            begin
              EditHandle := FindWindowEx(WHandle, 0, 'Edit',NIL);
              if (EditHandle > 0) then
                SetWindowText(EditHandle, 'test text');
            end;                
          end;
      end;
      WHandle := GetWindow(WHandle, GW_HWNDNEXT);
  end;
end;

我一直试图理解这里的一切,但我错过了一些东西。

我可以通过移动鼠标并以编程方式按下鼠标来按下任何 Windows 对话框按钮,但我想弄清楚如何以更简洁的方式按下这些按钮。

4

1 回答 1

1

免责声明:以下代码不以任何方式使用,如果环境不同(此处为 W7),它将无法正常工作。

无论如何,将以下内容放入您的计时器处理程序中,并尝试使用问题中的代码来锻炼差异(如果它有希望的话..):

var
  WHandle, ButtonHandle, EditHandle: HWND;
begin
  WHandle := FindWindow('#32770', 'File Download');
  if WHandle <> 0 then begin
    SetForegroundWindow(WHandle);
    ButtonHandle := GetDlgItem(WHandle, $114B);
    if ButtonHandle <> 0 then begin  // click the button
      // the dialog/button is kind of deaf.. 
      while IsWindowEnabled(WHandle) do begin
        SendMessage(ButtonHandle, BM_CLICK, 0, 0);
        Sleep(100);
      end;
      WHandle := 0;
      while WHandle = 0 do begin     // wait for the save as dialog
        WHandle := FindWindow('#32770', 'Save As');
        Sleep(100);
      end;
      while not IsWindowVisible(WHandle) do
        Sleep(100);
      // get through the edit handle
      WHandle := FindWindowEx(WHandle, 0, 'DUIViewWndClassName', nil);
      EditHandle := GetWindow(GetWindow(GetWindow(GetWindow
                    (WHandle, GW_CHILD), GW_CHILD), GW_CHILD), GW_CHILD);
      SetWindowText(EditHandle, 'test text');
    end;
  end;
于 2012-11-21T15:19:22.640 回答