CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2010
    Posts
    5

    Problem with webbrowser cotrol

    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!

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Problem with webbrowser cotrol

    Something like :
    Code:
    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

  3. #3
    Join Date
    Mar 2010
    Posts
    5

    Re: Problem with webbrowser cotrol

    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)

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Problem with webbrowser cotrol

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured