PDA

Click to See Complete Forum and Search --> : GetForegroundWindow Question


DocUK
Aug 20th, 2001, 04:31 PM
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

chrisjk
Aug 20th, 2001, 06:11 PM
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.

Matthew Gates
Aug 20th, 2001, 07:02 PM
Try this code to get the title of the active window.


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


Private 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


Private 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

gdebacker
Aug 20th, 2001, 08:46 PM
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

chrisjk
Aug 20th, 2001, 08:59 PM
That was my point, greg ;)

gdebacker
Aug 20th, 2001, 09:25 PM
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

DocUK
Aug 21st, 2001, 06:32 AM
Yea, I am a little unfamiliar with the API and with window handles... Thanks for the help everyone...

DocUK

Matthew Gates
Aug 21st, 2001, 01:26 PM
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.

DocUK
Aug 21st, 2001, 02:11 PM
I see... How would i be able to get the title and handles of all open windows, and put them into separate arrays?

DocUK

Matthew Gates
Aug 21st, 2001, 02:48 PM
Use the EnumWindows API function.


'Author: Megatron
'Origin: http://www.vbforums.com
'Purpose: Use EnumWindows to get a list of all Windows.
'Version: VB5+


Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Length As Long
Dim sName As String
Dim Temp As String
Static iCount As Integer

iCount = iCount + 1
Length = GetWindowTextLength(hwnd) + 1

If Length > 1 Then
sName = Space(Length)
GetWindowText hwnd, sName, Length
Debug.Print Left(sName, Length - 1)
End If

EnumWindowsProc = 1
End Function


'Usage

Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, 0
End Sub

DocUK
Aug 25th, 2001, 03:16 PM
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

Matthew Gates
Aug 25th, 2001, 07:14 PM
sName is the variable that holds it all.