Results 1 to 8 of 8

Thread: Get browser tab captions & close tabs

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    5

    Get browser tab captions & close tabs

    NOTE: I have also posted this thread on XtremeVBtalk.com.

    I'm barely above an entry-level programmer, and have been trying to accomplish this task for a few days now. Perhaps some of you smarter folks can assist me.

    The program I am coding (in VB6) closes windows which have a caption that matches a keyword list. The program works as intended on all windows except browsers. When multiple tabs are opened, my code will close the entire browser rather than only the matching tab. I've tried some code that searches for any child windows, but apparently Firefox and Chrome don't utilize child windows in this aspect.

    Basically, I'd like to know how to extract the captions from open tabs (regardless of focus) and subsequently close tabs. Provided those two snippets, I can script the rest. However, I just can't seem to figure out how to accomplish this. The internet has been scoured and my hands are still empty. Any help?

    NOTE: If your reply includes a reference to a required class/module, please specify the name or provide a link. Thanks!
    Last edited by enoctis; Sep 18th, 2012 at 11:05 AM. Reason: Additional details

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Get browser tab captions & close tabs

    Current code attempt?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    5

    Re: Get browser tab captions & close tabs

    I have everything broken down into separate subs, which would make it very difficult to compile into a reply. However, here's what I'm doing:

    Enumerating window handles to a list. Checking each handle for child windows. Grabbing captions from child windows (if they exist) and comparing to the keyword list. If no child exists, then the parent caption is compared. Closing any handles {PostMessage [HANDLE], WM_CLOSE, CLng(0), CLng(0)} that match.

    Is this adequate information?
    Last edited by enoctis; Sep 18th, 2012 at 10:21 AM. Reason: Additional details

  4. #4
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Get browser tab captions & close tabs

    I dont know if this can help you but you could modify code to be able to do what you want... Here is link

  5. #5
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Get browser tab captions & close tabs

    It is tricky to close or control tabs but if you find a way with api i wanna see

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Get browser tab captions & close tabs

    The program I am coding (in VB6) closes windows which have a caption that matches a keyword list. The program works as intended on all windows except browsers. When multiple tabs are opened, my code will close the entire browser rather than only the matching tab. I've tried some code that searches for any child windows, but apparently Firefox and Chrome don't utilize child windows in this aspect.
    as you do not show the code that is producing this result it is hard to make suggestions to modify the undesired behaviour,

    it is very easy to do this with internet explorer, but other programs that utilise tabs work differently and may require the sending of a different widows message to that which you are sending
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    5

    Re: Get browser tab captions & close tabs

    Code:
    'DECLARATIONS:
    
    Public Declare Function BeepAPI Lib "kernel32" Alias "Beep" (ByVal dwFrequency As Long, ByVal dwMilliseconds As Long) As Long
    Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Public Declare Function GetActiveWindow Lib "user32" () As Long
    Public Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Public Const GW_HWNDNEXT = 2
    Public Const WM_CLOSE = &H10

    Code:
    Private Sub timeBlock_Timer()
    
    ' lstChildHWND.List is where the handles of children are loaded
    ' lstKW.List is where the keywords to compare with window captions are stored
    ' lstHWND.List is where the handles are loaded from the EnumWindows function
    ' wbSave is a sub that saves relevant information to an external INI
    
    ' [EXPLANATION OF EVENTS] 
    ' Top-level window handles are loaded into lstHWND.List
    ' Each handle is checked for Child windows
    ' If Child windows exist, get captions and check against keywords (lstKW.List)
    ' If no Child windows, get top-level captions and check against keywords
    ' If matches are found, close via SendMessage/WM_Close
    
    lstHWND.Clear
    EnumWindows AddressOf EnumWindowsProc, ByVal 0&
    
    Dim handCnt, kwCnt, opt As Integer
    Dim childCnt As Integer
    
    For handCnt = 0 To lstHWND.ListCount - 1
        For kwCnt = 0 To lstKW.ListCount - 1
        Call GetChildHandles(lstChildHWND, lstHWND.List(handCnt))
            If lstChildHWND.ListCount > 0 Then
                For childCnt = 0 To lstChildHWND.ListCount - 1
                    If InStr(LCase(CaptionGetFromHandle(lstChildHWND.List(childCnt))), lstKW.List(kwCnt)) > 0 Then
                        '---BEGIN EXEMPTIONS--------
                        If Trim(LCase(CaptionGetFromHandle(lstChildHWND.List(childCnt)))) Like Trim(LCase(Me.Caption)) Then GoTo SkipChild
                        If Trim(LCase(CaptionGetFromHandle(lstChildHWND.List(childCnt)))) Like Trim(LCase(frmSecurity.Caption)) Then GoTo SkipChild
                        If Trim(LCase(CaptionGetFromHandle(lstChildHWND.List(childCnt)))) Like "registry editor" Then GoTo SkipChild
                        If Trim(LCase(CaptionGetFromHandle(lstChildHWND.List(childCnt)))) Like "windows task manager" Then GoTo SkipChild
                        '---END OF EXEMPTIONS-------
                        While CaptionGetFromHandle(lstChildHWND.List(childCnt)) <> ""
                            PostMessage lstChildHWND.List(childCnt), WM_CLOSE, CLng(0), CLng(0)
                        Wend
                        lblBlockedCnt = Val(lblBlockedCnt) + 1
                        Call wbSave
                        For opt = 0 To 2
                            If optSound(opt).Value = True Then
                                Select Case opt
                                    Case 0: 'Silent
                                    Case 1: BeepAPI 700, 75
                                    Case 2: Call MediaWAVplay(txtSound)
                                End Select
                            End If
                        Next opt
                    End If
    SkipChild:
                Next childCnt
            Else
                If InStr(LCase(CaptionGetFromHandle(lstHWND.List(handCnt))), lstKW.List(kwCnt)) > 0 Then
                    '---BEGIN EXEMPTIONS--------
                    If Trim(LCase(CaptionGetFromHandle(lstHWND.List(handCnt)))) Like Trim(LCase(Me.Caption)) Then GoTo SkipTop
                    If Trim(LCase(CaptionGetFromHandle(lstHWND.List(handCnt)))) Like Trim(LCase(frmSecurity.Caption)) Then GoTo SkipTop
                    If Trim(LCase(CaptionGetFromHandle(lstHWND.List(handCnt)))) Like "registry editor" Then GoTo SkipTop
                    If Trim(LCase(CaptionGetFromHandle(lstHWND.List(handCnt)))) Like "windows task manager" Then GoTo SkipTop
                    '---END OF EXEMPTIONS-------
                    While CaptionGetFromHandle(lstHWND.List(handCnt)) <> ""
                        PostMessage lstHWND.List(handCnt), WM_CLOSE, CLng(0), CLng(0)
                    Wend
                    lblBlockedCnt = Val(lblBlockedCnt) + 1
                    Call wbSave
                    For opt = 0 To 2
                        If optSound(opt).Value = True Then
                            Select Case opt
                                Case 0: 'Silent
                                Case 1: BeepAPI 700, 75
                                Case 2: Call MediaWAVplay(txtSound)
                            End Select
                        End If
                    Next opt
                End If
            End If
        Next kwCnt
    SkipTop:
    Next handCnt
    
    End Sub

    Code:
    Public Function HandleGetFromCaption(strAppTitle As String) As Long
    
    Dim hwnd As Long, hWndStop As Long, hWndNext As Long, iLen As Long
    Dim strTitle As String * 64, p As Integer
    
    hwnd = GetActiveWindow()
    hWndStop = hwnd
    
    p = 0
    Do
    hWndNext = GetNextWindow(hwnd, GW_HWNDNEXT)
    iLen = GetWindowText(hWndNext, strTitle, Len(strTitle))
    
    If iLen Then
        If InStr(LCase(strTitle), LCase(strAppTitle)) > 0 Then
            HandleGetFromCaption = hWndNext
            Exit Do
        End If
    End If
    
    hwnd = hWndNext
    p = p + 1
    Loop Until p = 256
    
    End Function

    Code:
    Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
        Dim sSave As String, Ret As Long, cls As Long, sText As String
        sText = Space(255)
            cls = GetClassName(hwnd, sText, 255)
            sText = Left$(sText, cls)
            Ret = GetWindowTextLength(hwnd)
            sSave = Space(Ret)
            GetWindowText hwnd, sSave, Ret + 1
            If sSave <> "" Then
                If hwnd < 1000 Then
                    If Trim(Str$(hwnd)) <> frmMain.hwnd Then
                        frmMain.lstHWND.AddItem Trim(Str$(hwnd)) ' + "   " + sSave
                    End If
                Else
                    If Trim(Str$(hwnd)) <> frmMain.hwnd Then
                        frmMain.lstHWND.AddItem Trim(Str$(hwnd)) ' + " " + sSave
                    End If
                End If
            End If
            'continue enumeration
            EnumWindowsProc = True
        'End If
    End Function
    Last edited by enoctis; Sep 20th, 2012 at 09:41 AM. Reason: Mistakenly excluded a function

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    5

    Re: Get browser tab captions & close tabs

    The Declarations and EnumWindowsProc are located in a module. frmMain is the form where the controls being used are located.

Tags for this Thread

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