|
-
Sep 11th, 2000, 06:43 PM
#1
Thread Starter
Hyperactive Member
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!!
-
Sep 11th, 2000, 06:48 PM
#2
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)
-
Sep 11th, 2000, 08:20 PM
#3
Thread Starter
Hyperactive Member
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]
-
Sep 11th, 2000, 08:25 PM
#4
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
-
Sep 11th, 2000, 09:05 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|