Click to See Complete Forum and Search --> : Problem with webbrowser cotrol


ganav
April 4th, 2010, 04:04 PM
Hello,
I use a webbrowser control in my program and sometimes I navigate to a site where I get a message box with "ok" and "cencel" buttons.
Is there a way to ignore this message? or maybe somehow send a command to exit that message box.

Thanks!

HanneSThEGreaT
April 6th, 2010, 07:51 AM
Something like :
Option Explicit On
Imports System
Imports System.Text
Imports System.Runtime.InteropServices

Public Class Form1

Private Delegate Function EnumWindowCallbackDelegate(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindowCallbackDelegate, ByVal lParam As IntPtr) As Boolean

Private Declare Auto Function GetWindowTextLength Lib "user32" (ByVal hWnd As IntPtr) As Integer
Private Declare Auto Function GetWindowText Lib "user32" (ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function GetParent Lib "user32" (ByVal hWnd As IntPtr) As IntPtr

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function GetAncestor Lib "user32" (ByVal hwnd As Long, ByVal gaFlags As UInteger) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, ByVal lpdwProcessId As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As IntPtr
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String) As IntPtr

Private Function EnumWindowsCallbackFunction(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

If IsWindowVisible(hWnd) And GetParent(hWnd) = IntPtr.Zero Then
Dim buffer As StringBuilder = New StringBuilder(GetWindowTextLength(hWnd) + 1)
GetWindowText(hWnd, buffer, buffer.Capacity)
Dim windowText As String = buffer.ToString()
If windowText.Contains("Windows Internet Explorer") Then
Dim wnd1 As IntPtr
Dim wnd2 As IntPtr
wnd2 = FindWindowEx(hWnd, 0, "Button", "OK")

Return False
End If
End If
Return True
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cb As New EnumWindowCallbackDelegate(AddressOf EnumWindowsCallbackFunction)
EnumWindows(cb, IntPtr.Zero)
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cb As New EnumWindowCallbackDelegate(AddressOf EnumWindowsCallbackFunction)
EnumWindows(cb, IntPtr.Zero)
End Sub
End Class

ganav
April 7th, 2010, 04:49 PM
It will be great if you could just explain what does this code do, and how can I use it.
(I know when this message pops up, after a certain event)

dglienna
April 7th, 2010, 09:18 PM
Open the app, after opening IE. If the OK box is there, it will click it, as soon as the Enum() function fires a callback, which happens when the main window of IE is open, and it finds the OK button.

Put a breakpoint in the code, and then start things. When it clicks the button, it will break