Results 1 to 3 of 3

Thread: Is it posible to fing the hWnd mof a window from its Caption???

  1. #1

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    43

    Question Is it possible to find thw hWnd of a window with just its caption???

    Okay this is just a quick question which will probably need a long answer, but is it possible to find the hwnd of a window from its capition, okay the situation is, im tryiny to make a program which when somone goes onto a certain website, ie yahoo, this programs uses the yahoo homepage caption to get the hwnd of the ie window so i can log how many times that webpage has been visited. also i need to know if there are any texts on getting the hwnds/captions of all windows, preferable all visable windows.

    if u can help with either of these please let me know
    Thanx
    Cease

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Try picking this apart...

    This code closes all windows with a partial caption
    so just pull out what you need
    It finds windows base on partial existance of its name
    'Remove the close part and add your code.

    Good Luck.


    Part of title:
    This code uses EnumWindows to retrieve the title of all
    windows in the system, compares it with the partial name,
    and stops when a match is made. Code modified from a sample
    available at http://www.thescarms.com
    Code taken from a Q&A On Experts Exchange Posted By: Erick37


    '~~~~~~~MODULE CODE~~~~~~~~~~~~

    Option Explicit

    Public Const WM_CLOSE = &H10
    Public Const MAX_PATH = 260

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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
    Declare Function EnumWindows Lib "user32" _
    (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

    Private sAppTitle As String
    Public glHwnd As Long

    Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long
    Dim lResult As Long
    Dim sWndName As String

    fEnumWindowsCallBack = 1
    sWndName = Space$(MAX_PATH)

    lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
    sWndName = Left$(sWndName, lResult)
    'Search Title for our string
    If (InStr(1, sWndName, sAppTitle, vbTextCompare) > 0) Then
    Debug.Print sWndName
    glHwnd = hwnd
    fEnumWindowsCallBack = 0
    End If
    End Function

    Public Function SearchWindows(sApp As String, hwnd As Long) As Long
    sAppTitle = sApp
    glHwnd = 0
    Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
    SearchWindows = glHwnd
    End Function

    '~~~~~~~FORM CODE~~~~~~~~~~~~~~

    Option Explicit

    Private Sub Command1_Click()
    Dim sApp As String
    'Find notepad with partial name
    sApp = "notepa"
    glHwnd = SearchWindows(sApp, Me.hwnd)
    'End application if found
    If glHwnd > 0 Then
    PostMessage glHwnd, WM_CLOSE, 0&, 0&
    End If
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    43

    Talking Thanx

    Thanx a lot man this has REALLY helped :o)
    Thanx again
    Cease

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