questo è il codice che ho trovato in rete:
codice:
Imports System.Runtime.InteropServices
Public Class DialogHandler
'API CONSTANTS
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
'FINDS CHILD WINDOWS
Private Declare Auto Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As Integer) As IntPtr
'SEND MESSAGES TO THE BUTTON
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
'GETS 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
Private Function GetChildWindowHandles(ByVal ParentWindowHandle As IntPtr) As ArrayList
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 clsRet
End Function
Private Function GetChildWindowHandle(ByVal ParentWindowHandle As IntPtr) As IntPtr
Return GetWindow(ParentWindowHandle, GW_CHILD)
End Function
Private Function GetNextWindowHandle(ByVal CurrentWindowhandle As IntPtr) As IntPtr
Return GetWindow(CurrentWindowhandle, GW_HWNDNEXT)
End Function
'RETURNS TEXT OF THE WINDOW FOR CONFIRMATION OF CORRECT DIALOG
Private Function GetWindowText(ByVal WindowHandle As IntPtr) As String
Dim ptrRet As IntPtr
Dim ptrLength As IntPtr
'LENGTH OF BUFFER
ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
'BUFFER NEEDED FOR RETURN VALUE
Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)
'WINDOW TEXT
ptrRet = SendMessageString(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sb)
Return sb.ToString
End Function
'SEND A 'CLICK' TO THE BUTTON ("WINDOW")
Private Sub PerformClick(ByVal WindowHandle As IntPtr)
SendMessage(WindowHandle, BM_CLICK, 0, IntPtr.Zero)
End Sub
Public Sub LookForAndCloseIEPopup(ByVal whichButton As String)
'GET HANDLE OF ANY POPUP WINDOW ASSOCIATED WITH MAIN FORM
Dim ptrDialogWindow As IntPtr = GetWindow(Process.GetCurrentProcess.MainWindowHandle, GW_ENABLEDPOPUP)
'IF IT'S A BROWSER POPUP, HANDLE IT
If GetWindowText(ptrDialogWindow) = "Microsoft Internet Explorer" Or GetWindowText(ptrDialogWindow) = "Message from webpage" Or GetWindowText(ptrDialogWindow) = "Windows Internet Explorer" Then
ClosePopup(ptrDialogWindow, whichButton)
End If
End Sub
Private Sub ClosePopup(ByVal WindowHandle As IntPtr, ByVal whichButton As String)
Dim clsChildHandles As ArrayList = GetChildWindowHandles(WindowHandle)
For Each ptrHandle As IntPtr In clsChildHandles
'IF IT FINDS A BUTTON WITH THE TEXT SPECIFIED, CLICK IT
If GetWindowText(ptrHandle).Contains(whichButton) Then PerformClick(ptrHandle) : Exit For
Next
End Sub
End Class
Solo che Io sto utilizzando visualbasic 2010 e mi da errore nelle seguenti righe:
api constants
= &HD
= &HE
= &HF5&
come li devo scrivere per non darmi l'errore?
Si capisce che sono un principiante quindi vi chiedo scusa per il disturbo e grazie per la pazienza.