单击资源管理器上的保存按钮下载弹出窗口 [英] click save button on explorer download popup

查看:46
本文介绍了单击资源管理器上的保存按钮下载弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动从网页获取文件.它将打开保存下载"窗口,其中包含3个常规选项(打开",保存"和取消").我试图单击保存"按钮,但无法正常工作.我什至试图从Spy ++获取& Save 句柄,并在执行 ClickButton 之前插入句柄指针,以确保我拥有正确的句柄指针,但仍然一无所有./p>

SendMessage(WindowHandle,BM_CLICK,0,IntPtr.Zero)

之外,其他所有方法都正常运行

  Sub ClickButton(ByVal WindowHandle为IntPtr)'SendMessageW(WindowHandle,WM_ACTIVATE,New IntPtr(WA_ACTIVE),IntPtr.Zero)我尝试了这种ClickButton的几种口味,但没有效果!'SendMessageW(WindowHandle,BM_CLICK,IntPtr.Zero,IntPtr.Zero)SendMessage(WindowHandle,BM_CLICK,0,IntPtr.Zero)结束子子LookForAndCloseIEPopup()'获取与主窗体关联的任何弹出窗口的句柄(弹出窗口也是如此)'由网络浏览器控件显示)...昏暗的ptrDialogWindow为IntPtr = GetWindow(xAsIntPtr,GW_ENABLEDPOPUP)暗淡的text2 = GetWindowText(xAsIntPtr)如果text2.Contains("File Download")然后'xAsIntPtr = hWnd'LookForAndCloseIEPopup()'(GetWindowText(hwnd,text,Int16.MaxValue))万一Debug.Print(GetWindowText(ptrDialogWindow)&"TEXT:="& Text2& vbCrLf)'如果弹出窗口是浏览器显示的一个,则将关闭消息发送到该窗口...如果text2.Contains("File Download")然后ClosePopup(ptrDialogWindow)结束子子ClosePopup(ByVal WindowHandle作为IntPtr)昏暗的clsChildHandles为ArrayList = GetChildWindowHandles(WindowHandle)昏暗的teststr作为字符串昏暗的prthandleint'在窗口的所有子句柄中查找确定"按钮(此方法'还可以用于收集有关对话框本身的更具体的信息,例如'正在显示的消息)...对于每个ptrHandle作为clsChildHandles中的IntPtr'如果找到确定按钮,请单击它...teststr = GetWindowText(ptrHandle)prthandleint = ptrHandle'ptrHandle =& H002A0B7E !!!我什至尝试放置从Spy ++获得的& Save句柄,结果相同!Debug.Print(GetWindowText(ptrHandle)&"=子文本:len ="& teststr.Length&"handle ="& prthandleint& vbCrLf)如果teststr.Contains(& Save")然后ClickButton(ptrHandle)'这部分工作正常,因为我到了这里退出别的Debug.Print(GetWindowText(ptrHandle)&"=子文本:len ="& vbCrLf)万一下一个结束子常量WM_GETTEXT只要=& HD常量WM_GETTEXTLENGTH为Long =& HEconst GW_ENABLEDPOPUP只要Long = 6const BM_CLICK只要Long =& HF5&常量GW_CHILD只要= 5常量GW_HWNDNEXT只要Long = 2常量WM_ACTIVATE为整数=& H6常量WA_ACTIVE为整数=& H1检索与表单关联的弹出窗口以及查找弹出窗口的子窗口的功能...私有声明自动功能GetWindow库"user32.dll"(ByVal hWnd作为IntPtr,ByVal uCmd作为整数)作为IntPtr< DllImport("user32.dll",EntryPoint:="SendMessageW")>私有共享函数SendMessageW(ByVal hWnd作为IntPtr,ByVal Msg作为UInteger,ByVal wParam作为IntPtr,ByVal lParam作为IntPtr)作为整数结束功能'sendmessage超载,用于将消息发送到对话框窗口上的按钮...私有声明自动功能SendMessage库"user32.dll"别名"SendMessage"(ByVal hWnd作为IntPtr,ByVal Msg作为整数,ByVal wParam作为整数,ByRef lParam作为IntPtr)作为IntPtr'sendmessage重载,用于检索窗口文本...私有声明自动功能SendMessageA库"user32.dll"别名"SendMessageA"(ByVal hWnd作为IntPtr,ByVal Msg作为整数,ByVal wParam作为IntPtr,ByRef lParam作为IntPtr)作为IntPtr< DllImport("User32.dll",CharSet:= CharSet.Auto,EntryPoint:="SendMessage")>公共共享函数SendMessageString(ByVal hwnd as IntPtr,ByVal wMsg作为整数,ByVal wparam作为整数,ByVal lparam作为System.Text.StringBuilder)作为IntPtr结束功能函数GetChildWindowHandles(ByVal ParentWindowHandle As IntPtr)作为ArrayListDim b为布尔昏暗的ptrChild作为IntPtr昏暗的clsRet作为新的ArrayList'ParentWindowHandle = IntPtr(xAsIntPtr)'得到第一个孩子的手...ptrChild = GetChildWindowHandle(xAsIntPtr)做直到ptrChild.Equals(IntPtr.Zero)'添加到句柄集合中...clsRet.Add(ptrChild)'生下一个孩子...ptrChild = GetNextWindowHandle(ptrChild)环形'返回...返回clsRet结束功能函数GetChildWindowHandle(ByVal ParentWindowHandle As IntPtr)作为IntPtr返回GetWindow(ParentWindowHandle,GW_CHILD)结束功能函数GetNextWindowHandle(ByVal CurrentWindowhandle As IntPtr)作为IntPtr返回GetWindow(CurrentWindowhandle,GW_HWNDNEXT)结束功能'此函数返回窗口的文本,以便我们可以确认我们拥有正确的对话框窗口...函数GetWindowText(ByVal WindowHandle As IntPtr)作为字符串作为IntPtr的Dim ptrRet昏暗的ptrLength作为IntPtr'获取缓冲区的长度...ptrLength = SendMessageA(WindowHandle,WM_GETTEXTLENGTH,IntPtr.Zero,IntPtr.Zero)'为返回值创建缓冲区...昏暗的人作为新的System.Text.StringBuilder(ptrLength.ToInt32 + 1)'获取窗口文字...ptrRet = SendMessageString(WindowHandle,WM_GETTEXT,ptrLength.ToInt32 + 1,sb)'获取返回值...返回sb.ToString结束功能 

在调试输出窗口中,我看到捕获了& Save 的值,并且代码按预期工作,直到 ClickButton 尝试以非执行方式执行的部分为止响应.

解决方案

我自己找到了解决方案!

需要将setactiveWindow放在第一位!

  SetActiveWindow(xAsIntPtr)SendMessageW(WindowHandle,WM_ACTIVATE,New IntPtr(WA_ACTIVE),IntPtr.Zero)SendMessageW(WindowHandle,BM_CLICK,IntPtr.Zero,IntPtr.Zero) 

Pete

I'm trying to automate getting a file from a web page. It opens the Save Download window with the 3 usual choices (Open, Save, and Cancel). I'm trying to click on the Save button but can't get it to work. I even tried to get the &Save handle from Spy++ and inserting the handle pointer just before executing ClickButton just to be sure I had the right handle pointer and still nothing.

Everything works fine except for SendMessage(WindowHandle, BM_CLICK, 0, IntPtr.Zero)

Sub ClickButton(ByVal WindowHandle As IntPtr)
    'SendMessageW(WindowHandle, WM_ACTIVATE, New IntPtr(WA_ACTIVE), IntPtr.Zero) I've tried several flavors of this ClickButton and no effect! 
    'SendMessageW(WindowHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero)
    SendMessage(WindowHandle, BM_CLICK, 0, IntPtr.Zero)
End Sub


Sub LookForAndCloseIEPopup()
    'get a handle to any popup window associated with the main form (as is a popup window
    'displayed by the Web browser control)...
    Dim ptrDialogWindow As IntPtr = GetWindow(xAsIntPtr, GW_ENABLEDPOPUP)
    Dim text2 = GetWindowText(xAsIntPtr)
    If text2.Contains("File Download") Then
        'xAsIntPtr = hWnd
        'LookForAndCloseIEPopup() '(GetWindowText(hwnd, text, Int16.MaxValue))
    End If

    Debug.Print(GetWindowText(ptrDialogWindow) & "TEXT:=" & Text2 & vbCrLf)
    'if the popup window is one displayed by the browser, then send the close message to the window...
    If text2.Contains("File Download") Then ClosePopup(ptrDialogWindow)

End Sub

Sub ClosePopup(ByVal WindowHandle As IntPtr)

    Dim clsChildHandles As ArrayList = GetChildWindowHandles(WindowHandle)
    Dim teststr As String
    Dim prthandleint As Long
    'look through all of the child handles of the window for an "OK" button (this method 
    'can also be used to gather more specific information about the dialog itself, such as 
    'the message being displayed)...
    For Each ptrHandle As IntPtr In clsChildHandles
        'if the OK button is found, click it...
        teststr = GetWindowText(ptrHandle)
        prthandleint = ptrHandle
        'ptrHandle = &H002A0B7E    !!! I even tried putting the &Save handle which I got from Spy++ with the same result! 
        Debug.Print(GetWindowText(ptrHandle) & " = Child TEXT:   len=" & teststr.Length & "handle = " & prthandleint & vbCrLf)

        If teststr.Contains("&Save") Then
            ClickButton(ptrHandle)  'This part works fine as I get here
            Exit For
        Else
            Debug.Print(GetWindowText(ptrHandle) & " = Child TEXT:   len=" & vbCrLf)
        End If
    Next

End Sub

Const WM_GETTEXT As Long = &HD
Const WM_GETTEXTLENGTH As Long = &HE
Const GW_ENABLEDPOPUP As Long = 6
Const BM_CLICK As Long = &HF5&
Const GW_CHILD As Long = 5
Const GW_HWNDNEXT As Long = 2
Const WM_ACTIVATE As Integer = &H6
Const WA_ACTIVE As Integer = &H1
'function to retrieve the popup window associated with the form, as well as to find the child windows of the popup...
Private Declare Auto Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As Integer) As IntPtr
<DllImport("user32.dll", EntryPoint:="SendMessageW")>
Private Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
'sendmessage overload that is used to send messages to the button on the dialog window...
Private Declare Auto Function SendMessage Lib "user32.dll" Alias "SendMessage" (ByVal hWnd As IntPtr, ByVal Msg As Integer,
ByVal wParam As Integer, ByRef lParam As IntPtr) As IntPtr

'sendmessage overloads used to retrieve the window text...
Private Declare Auto Function SendMessageA Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer,
ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
<DllImport("User32.dll", CharSet:=CharSet.Auto, EntryPoint:="SendMessage")> Public Shared Function SendMessageString(ByVal hwnd As IntPtr,
ByVal wMsg As Integer, ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As IntPtr
End Function
Function GetChildWindowHandles(ByVal ParentWindowHandle As IntPtr) As ArrayList

    Dim b As Boolean
    Dim ptrChild As IntPtr
    Dim clsRet As New ArrayList
    'ParentWindowHandle = IntPtr(xAsIntPtr)
    'get first child handle...
    ptrChild = GetChildWindowHandle(xAsIntPtr)

    Do Until ptrChild.Equals(IntPtr.Zero)
        'add to collection of handles...
        clsRet.Add(ptrChild)
        'get next child...
        ptrChild = GetNextWindowHandle(ptrChild)

    Loop

    'return...
    Return clsRet

End Function

Function GetChildWindowHandle(ByVal ParentWindowHandle As IntPtr) As IntPtr
    Return GetWindow(ParentWindowHandle, GW_CHILD)
End Function

Function GetNextWindowHandle(ByVal CurrentWindowhandle As IntPtr) As IntPtr
    Return GetWindow(CurrentWindowhandle, GW_HWNDNEXT)
End Function

'this function returns the text of the window, used so that we can confirm that we have the right dialog window...
Function GetWindowText(ByVal WindowHandle As IntPtr) As String

    Dim ptrRet As IntPtr
    Dim ptrLength As IntPtr

    'get length for buffer...
    ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)

    'create buffer for return value...
    Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)

    'get window text...
    ptrRet = SendMessageString(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sb)

    'get return value...
    Return sb.ToString

End Function

In the debug output window I see the value of &Save get captured and the code works as expected up to the part where ClickButton tries to execute with a non response.

解决方案

Found the solution myself!

Needed to put setactiveWindow in first!

    SetActiveWindow(xAsIntPtr)
    SendMessageW(WindowHandle, WM_ACTIVATE, New IntPtr(WA_ACTIVE), IntPtr.Zero)
    SendMessageW(WindowHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero)

Pete

这篇关于单击资源管理器上的保存按钮下载弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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