-
Apr 30th, 2012, 12:42 AM
#1
Thread Starter
Addicted Member
click a pop up OK dialog button.
I found this code online to click a pop up OK dialog button. It's a lot of code but it works. I tried putting this code in a module or another form so I can use it with other programs. I get an error every time.
This line is causing the problem.
Dim ptrDialogWindow As IntPtr = GetWindow(Me.Handle, GW_ENABLEDPOPUP)
Me.Handle doesn't work outside the form. I tried the name_of_form.Handle in another form. Syntax is okay but crashes during runtime. Any idea on putting this code in a module or another form?
Anyone else has a more simplier code to click a dialog button?
'----------------------------------------- click dialog button ---------------------------------
'api constant declarations...
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
'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 Long) As IntPtr
'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
'these next three functions are used to enumerate the Windows handles of all Windows sited on the specified parent...
Private Shared Function GetChildWindowHandles(ByVal ParentWindowHandle As IntPtr) As ArrayList
Dim b As Boolean
Dim ptrChild As IntPtr
Dim clsRet As New ArrayList
'get first child handle...
ptrChild = GetChildWindowHandle(ParentWindowHandle)
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
Private Shared Function GetChildWindowHandle(ByVal ParentWindowHandle As IntPtr) As IntPtr
Return GetWindow(ParentWindowHandle, GW_CHILD)
End Function
Private Shared 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...
Private 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
'this function sends a button click message to the specified window...
Private Shared Sub ClickButton(ByVal WindowHandle As IntPtr)
SendMessage(WindowHandle, BM_CLICK, 0, IntPtr.Zero)
End Sub
Private Sub LookForAndCloseIEPopup(ByVal title_text As String, ByVal button_text As String, ByVal delay As Integer)
'Add thread delay
utility_library_1.web_browser_delay(delay)
'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(Me.Handle, GW_ENABLEDPOPUP)
'if the popup window is one displayed by the browser, then send the close message to the window...
' title_text could be "Windows Internet Explorer"
If GetWindowText(ptrDialogWindow) = title_text Then ClosePopup(ptrDialogWindow, button_text)
End Sub
'Private Sub ClosePopup(ByVal WindowHandle As IntPtr)
Private Sub ClosePopup(ByVal WindowHandle As IntPtr, ByVal button_text As String)
Dim clsChildHandles As ArrayList = GetChildWindowHandles(WindowHandle)
'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 button_text (typically OK button) is found, click it...
If GetWindowText(ptrHandle) = button_text Then ClickButton(ptrHandle) : Exit For
Next
End Sub
-
Apr 30th, 2012, 01:34 AM
#2
Re: click a pop up OK dialog button.
Placing Dim ptrDialogWindow As IntPtr = GetWindow(Me.Handle, GW_ENABLEDPOPUP) into another file should cause an error as Me.Handle refers to the form you took the code from. Calling this code from a form you need to pass the form handle to the code.
-
Apr 30th, 2012, 09:45 AM
#3
Thread Starter
Addicted Member
Re: click a pop up OK dialog button.
I did pass the form handle to the code. For example, my main form is called Facebook_Posting and my library form is called utility_library. I tried using the Facebook_Posting.Handle in the utility_library form code. Syntax is okay but crashes during runtime.
-
Apr 30th, 2012, 11:25 AM
#4
Re: click a pop up OK dialog button.
Try GetForegroundWindow
Code:
Module Module1
<Runtime.InteropServices.DllImport("user32.dll")> _
Public Function GetForegroundWindow() As IntPtr
End Function
<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
Public Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
End Function
<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
Private Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function
Public Function GetText(ByVal hWnd As IntPtr) As String
Dim length As Integer
If hWnd.ToInt32 <= 0 Then
Return Nothing
End If
length = GetWindowTextLength(hWnd)
If length = 0 Then
Return Nothing
End If
Dim sb As New System.Text.StringBuilder("", length + 1)
GetWindowText(hWnd, sb, sb.Capacity)
Return sb.ToString()
End Function
Public Sub Demo()
Dim hwnd As IntPtr = GetForegroundWindow()
MessageBox.Show(GetText(hwnd))
End Sub
End Module
-
Apr 30th, 2012, 12:54 PM
#5
Thread Starter
Addicted Member
Re: click a pop up OK dialog button.
Thank you Kevin. I got it working. I'm amazed how much code needed to be written to click on a dialog button.
-
Apr 30th, 2012, 01:04 PM
#6
Re: click a pop up OK dialog button.
Originally Posted by holisticsam
Thank you Kevin. I got it working. I'm amazed how much code needed to be written to click on a dialog button.
Personally I tend not to think of how much code is needed when it comes to an operation such as this. Any ways good to hear you are working now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|