|
-
Aug 20th, 2001, 04:31 PM
#1
Thread Starter
Junior Member
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
-
Aug 20th, 2001, 06:11 PM
#2
PowerPoster
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.
-
Aug 20th, 2001, 07:02 PM
#3
Try this code to get the title of the active window.
VB 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
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
-
Aug 20th, 2001, 08:46 PM
#4
Frenzied Member
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.
-
Aug 20th, 2001, 08:59 PM
#5
PowerPoster
That was my point, greg
-
Aug 20th, 2001, 09:25 PM
#6
Frenzied Member
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.
-
Aug 21st, 2001, 06:32 AM
#7
Thread Starter
Junior Member
Yes...
Yea, I am a little unfamiliar with the API and with window handles... Thanks for the help everyone...
DocUK
-
Aug 21st, 2001, 01:26 PM
#8
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.
-
Aug 21st, 2001, 02:11 PM
#9
Thread Starter
Junior Member
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
-
Aug 21st, 2001, 02:48 PM
#10
Use the EnumWindows API function.
VB Code:
'Author: Megatron
'Origin: [url]http://www.vbforums.com[/url]
'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
'[b][u]Usage[/u][/b]
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, 0
End Sub
-
Aug 25th, 2001, 03:16 PM
#11
Thread Starter
Junior Member
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
-
Aug 25th, 2001, 07:14 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|