Windows Hwnd句柄选择文件

时间:2013-06-27 16:10:40

标签: vb.net hwnd

我有一个成功访问网页并点击“上传文件”按钮的网络应用。

我的应用程序还通过监视和挂钩来成功处理弹出窗口。在大多数情况下,只需单击“确定”或“取消”按钮即可。按钮很简单。

我需要帮助的是Choose-File对话框。我把它挂好了,但是它上面有很多控件,我需要一些方向。

Choose File Dialog Example

这些是儿童控件:

DUIViewWndClassName,DirectUIHWND,CtrlNotifySink,NamespaceTreeControl,Static,SysTreeView32,CtrlNotifySink,Shell Preview Extension Host,CtrlNotifySink,SHELLDLL_DefView,DirectUIHWND,CtrlNotifySink,ScrollBar,CtrlNotifySink,ScrollBar,Static,Static,Static,ListBox,Static,Static,ComboBoxEx32,ComboBox,Edit,Static,ComboBox,Button,Button,Button,ScrollBar,WorkerW,ReBarWindow32,TravelBand,ToolbarWindow32,Address Band Root,msctls_progress32,Breadcrumb Parent,ToolbarWindow32,ToolbarWindow32,UniversalSearchBand,Search Box,SearchEditBoxWrapperClass,DirectUIHWND

我很乐意将精确的路径/文件粘贴到文件名文本框/组合框中,然后单击“打开”。按钮部分很简单,但我不知道如何在窗口中选择文件,和/或如何将我的路径放入文件名输入字段。

现在我有这样的事情:

<DllImport("user32.dll")> _
Private Shared Function GetClassName(ByVal hWnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Int32) As Int32
End Function

<DllImport("user32.dll")> _
Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal text As StringBuilder, ByVal maxLength As Int32) As Int32
End Function

<DllImport("user32.dll")> _
Private Shared Function GetDlgCtrlID(ByVal hwndCtl As IntPtr) As Integer
End Function

....

Private Shared Function hwndHandler() As Int32
    Dim ptrButtonhwnd As IntPtr

    For Each pChild As IntPtr In Interop.ChildWindows(pPopup.hwnd)
        Dim sbControl As New StringBuilder(255)
        GetClassName(pChild, sbControl, sbControl.Capacity)
        If "Button".Equals(sbControl.ToString()) Then
            Dim sbText As New StringBuilder(255)
            GetWindowText(pChildOfDialog, sbText, sbText.Capacity)
            If "&Open".Equals(sbText.ToString()) Then
                ptrButtonHwnd = pChild
            End If
        End If
    Next

    If ptrButtonHwnd <> IntPtr.Zero Then
        Dim ctrlId As Int32 = GetDlgCtrlID(ptrButtonHwnd)
        SendMessage(pPopup.hwnd, WM_COMMAND, New IntPtr(ctrlId), ptrButtonHwnd)
        Return 1
    End If

Return 0
End Function

这很好用,但是我需要添加一些内容来选择要打开的文件,方法是将其输入到text / combo字段中,或者在窗口中选择它。

1 个答案:

答案 0 :(得分:1)

我发现答案是寻找带有&#34;编辑&#34;文本的控件,这是我原始列表中列出的控件之一。

因此,根据我上面发布的代码,我创建了一个新指针ptrEdit,并将控件分配给"Edit".Equals(sbControl.ToString())

然后操纵它,我使用了一个DLL:

If ptrEdit <> IntPtr.Zero Then
    SetWindowText(pEditHwnd, strFilePath)
    If ptrButtonHwnd <> IntPtr.Zero Then
        Dim ctrlId As Int32 = GetDlgCtrlID(ptrButtonHwnd)
        SendMessage(cwp.hwnd, WM_COMMAND, New IntPtr(ctrlId), ptrButtonHwnd)
        Return 1
    End If
End If

所以我能够控制&#34;选择要上传的文件&#34;对话框。

相关问题