Results 1 to 5 of 5

Thread: get title of active window

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    my program needs to get the title of the window that has the focus (the window that the user is currently working with)

    any ideas?


    thanks in advance!!
    ______________

  2. #2
    Guest
    Here you go:

    Code:
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias _
    "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    
    Public Function GetActiveWindowTitle(ByVal ReturnParent As Boolean) As String
       Dim i As Long
       Dim j As Long
    
       i = GetForegroundWindow
    
    
       If ReturnParent Then
          Do While i <> 0
             j = i
             i = GetParent(i)
          Loop
    
          i = j
       End If
    
       GetActiveWindowTitle = GetWindowTitle(i)
    End Function
    Public Function GetWindowTitle(ByVal hwnd As Long) As String
       Dim l As Long
       Dim s As String
    
       l = GetWindowTextLength(hwnd)
       s = Space(l + 1)
    
       GetWindowText hwnd, s, l + 1
    
       GetWindowTitle = Left$(s, l)
    End Function
    
    Usage:
    
    MsgBox GetActiveWindowTitle(True)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    thanks! it works great.

    i'm trying to get a msgbox to come up when my form loses focus

    i tried:

    Private Sub Form_LostFocus()
    msgbox GetActiveWindowTitle(True)
    End Sub

    but it doesnt work. any ideas on this one?

    (or am i not using LostFocus() correctly?)



    by the way, where do you get all these functions? are there some good books you recommend i should read or what?

    thanks!!

    [Edited by HAVocINCARNATE29 on 09-11-2000 at 09:22 PM]
    ______________

  4. #4
    Guest
    Subclass.

    Code:
    Option Explicit
    
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal ndx As Long, ByVal newValue As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    ' This is used with the SetWindowLong API function.
    Const GWL_WNDPROC = -4
    
    Public Const WM_KILLFOCUS = &H8
    
    Dim saveHWnd As Long        ' The handle of the subclassed window.
    Dim oldProcAddr As Long     ' The address of the original window procedure
    
    Sub StartSubclassing(ByVal hWnd As Long)
        saveHWnd = hWnd
        oldProcAddr = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
    End Sub
    
    Sub StopSubclassing()
        SetWindowLong saveHWnd, GWL_WNDPROC, oldProcAddr
    End Sub
    
    Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
        ByVal wParam As Long, ByVal lParam As Long) As Long
        ' Send the message to the original window procedure, and then
        ' return Windows the return value from the original procedure.
        WndProc = CallWindowProc(oldProcAddr, hWnd, uMsg, wParam, lParam)
        
    Select Case uMsg
        Case WM_KILLFOCUS
                MsgBox GetActiveWindowTitle(True)
        End Select
    End Function
    
    Private Sub Form_Load()
        StartSubclassing Me.hWnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        StopSubclassing
    End Sub

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    If Subclassing seems a little daunting for this task, try a Timer control and the GetForeGroundWindow() API, i.e.
    Code:
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    
    Private Sub Form_Load()
        Timer1.Interval = 1000
    End Sub
    
    Private Sub Timer1_Timer()
        If hWnd <> GetForegroundWindow() Then MsgBox "Don't ya like me no more?", vbQuestion + vbSystemModal + vbYesNo, "Don't Go!!!"
    End Sub

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