Results 1 to 12 of 12

Thread: GetForegroundWindow Question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Posts
    18

    Arrow GetForegroundWindow Question

    Im trying to find the foreground window, using GetForegroundWindow, and then getting its Title using GetWindowText.

    The problem is that GetForegroundWindow returns a Long, like 3989, for example, but I need to specify the hWnd of the window in the GetWindowText function to get its title.

    How do I get the hWnd of the window using the number?

    DocUK

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    The GetForegroundWindow function returns the handle of the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.

  3. #3
    Matthew Gates
    Guest
    Try this code to get the title of the active window.


    VB Code:
    1. Private Declare Function GetForegroundWindow _
    2. Lib "user32" () As Long
    3.  
    4. Private Declare Function GetWindowTextLength _
    5. Lib "user32" Alias "GetWindowTextLengthA" (ByVal _
    6. hwnd As Long) As Long
    7.  
    8. Private Declare Function GetWindowText Lib "user32" _
    9. Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal _
    10. lpString As String, ByVal cch As Long) As Long
    11.  
    12. Private Declare Function GetParent Lib "user32" (ByVal _
    13. hwnd As Long) As Long
    14.  
    15.  
    16. Private Function GetActiveWindowTitle(ByVal ReturnParent As Boolean) As String
    17.    Dim i As Long
    18.    Dim j As Long
    19.  
    20.    i = GetForegroundWindow
    21.  
    22.  
    23.    If ReturnParent Then
    24.       Do While i <> 0
    25.          j = i
    26.          i = GetParent(i)
    27.       Loop
    28.  
    29.       i = j
    30.    End If
    31.  
    32.    GetActiveWindowTitle = GetWindowTitle(i)
    33. End Function
    34.  
    35.  
    36. Private Function GetWindowTitle(ByVal hwnd As Long) As String
    37.    Dim l As Long
    38.    Dim s As String
    39.  
    40.    l = GetWindowTextLength(hwnd)
    41.    s = Space$(l + 1)
    42.  
    43.    GetWindowText hwnd, s, l + 1
    44.  
    45.    GetWindowTitle = Left$(s, l)
    46. End Function

  4. #4
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075

    Re: GetForegroundWindow Question

    Originally posted by DocUK
    Im trying to find the foreground window, using GetForegroundWindow, and then getting its Title using GetWindowText.

    The problem is that GetForegroundWindow returns a Long, like 3989, for example, but I need to specify the hWnd of the window in the GetWindowText function to get its title.

    How do I get the hWnd of the window using the number?

    DocUK
    hWnd is nothing more than a long integer.

    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

  5. #5
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    That was my point, greg

  6. #6
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    Originally posted by chrisjk
    That was my point, greg
    I guess, but he seemed to be stuggling with the basic concept and the quote seemed overly technical to explain that to someone less familiar with the Windows API.

    I just wanted to put a finer point on it.

    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Posts
    18

    Talking Yes...

    Yea, I am a little unfamiliar with the API and with window handles... Thanks for the help everyone...

    DocUK

  8. #8
    Matthew Gates
    Guest
    Window handles (hWnd) are used to identify a window. Every window has a handle or hWnd and with the hWnd, you can do just about anything with the window that API and VB are capable of allowing you to do.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Posts
    18

    Hrmmm

    I see... How would i be able to get the title and handles of all open windows, and put them into separate arrays?

    DocUK

  10. #10
    Matthew Gates
    Guest
    Use the EnumWindows API function.


    VB Code:
    1. 'Author: Megatron
    2. 'Origin: [url]http://www.vbforums.com[/url]
    3. 'Purpose: Use EnumWindows to get a list of all Windows.
    4. 'Version: VB5+
    5.  
    6.  
    7. Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    8. Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    9. Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    10.  
    11. Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    12.     Dim Length As Long
    13.     Dim sName As String
    14.     Dim Temp As String
    15.     Static iCount As Integer
    16.    
    17.     iCount = iCount + 1
    18.     Length = GetWindowTextLength(hwnd) + 1
    19.    
    20.     If Length > 1 Then
    21.       sName = Space(Length)
    22.       GetWindowText hwnd, sName, Length
    23.       Debug.Print Left(sName, Length - 1)
    24.     End If
    25.    
    26.     EnumWindowsProc = 1
    27. End Function
    28.  
    29.  
    30. '[b][u]Usage[/u][/b]
    31.  
    32. Private Sub Command1_Click()
    33.     EnumWindows AddressOf EnumWindowsProc, 0
    34. End Sub

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Posts
    18

    Thumbs up Wow

    I have yet to try it, as I am in the middle of the process of moving premises, but thanks a lot for all the responses!

    Matt, in which variables are the names and handles of the windows stored?

    Thanks!

    DocUK

  12. #12
    Matthew Gates
    Guest
    sName is the variable that holds it all.

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