按下Windows MSdn对话框按钮并在Delphi中更改``另存为''对话框中的``编辑'' [英] Pressing windows msdn dialog box buttons and changing the 'Edit' in ''Save as' dialog box in Delphi

查看:71
本文介绍了按下Windows MSdn对话框按钮并在Delphi中更改``另存为''对话框中的``编辑''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此处获取了以下代码,并且已更改有点,我也改变了原来的问题.

I got the following code from here and have changed it a little and I have changed the original question a little also.

计时器间隔设置为5000.
在发生以下3个事件之后,事件OnTimer"过程将开始工作.

The timer interval is set to 5000.
After the following 3 events occur the 'Events OnTimer' procedure will start to work.

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

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

The problem now is I can't find the 'Edit'(Class Name) handle that belongs to(or is the child of) the 'Save as' dialog box. The handle for the 'Edit' comes to '0' in the code below, but if I use my mouse pointer and the following code:

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

句柄给出结果.有了手柄后,就可以使用:

the handle gives a result. Once I have the handle, I can use:

SetWindowText(EditHandle, 'test text'); 

更改编辑"(类名)中的文本.

to change the text in 'Edit'(Class Name).

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;

我一直试图了解所有内容这里,但我丢失了一些东西.

I've been trying to understand everything here but I'm missing something.

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

I'm able to press any windows dialog button by moving the mouse and pressing the mouse programatically, but I'd like to figure out how to press these buttons in a cleaner way.

推荐答案

免责声明:以下代码无意以 any 的方式使用,如果环境不同,它将无法正常工作无论如何(这里是W7).

Disclaimer: the below code is not meant to be used in any way, it won't work correctly if the environment is different anyway (W7 here).

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

Anyway, put the below in your timer handler and try to workout the differences (if it works hopefully..) with the code in the question:

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;

这篇关于按下Windows MSdn对话框按钮并在Delphi中更改``另存为''对话框中的``编辑''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆