I have a web app that successfully gets to a page and clicks an "Upload File" button.
My app also successfully handles pop-up windows by monitoring and hooking them. For the most part, it's just to click "OK" or "Cancel" buttons. Buttons are easy.
What I need help with is the Choose-File dialog. I'm hooking it fine, but there are a lot of controls on it and I need some direction.
These are the child controls on it:
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
I would be happy with sticking an exact path/file into the File-Name textbox/combobox and clicking "Open". The button part is easy, but I don't know either how to select files in the window, and/or how to put my path into the File-Name entry field.
Right now I have something like this:
<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
This works fine, but I need to add something to select a file to open either by inputting it into the text/combo field, or by selecting it in the window.
I found the answer was to look for a control with the text of "Edit", which was one of the controls listed in my original list.
So according to my code posted above, I made a new pointer ptrEdit
, and assigned it the control where "Edit".Equals(sbControl.ToString())
.
Then to manipulate it, I used one of the DLLs:
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
And so I was able to control the "Choose File To Upload" Dialog Box.