GFGetFocusWindowCaption/Mfrm.frm
VERSION 5.00
Begin VB.Form Mfrm
Caption = "Form1"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows‑Standard
Begin VB.Timer Timer1
Interval = 1000
Left = 60
Top = 60
End
End
Attribute VB_Name = "Mfrm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'(c)2001 by Louis. To be used in the NN99 System. Use especially for getting the description of a web page the target person is currently visiting (from browser title).
'NOTE: GFGetForeGroundWindowCaption maybe does not work on WinNT (GetFocus is not supported, maybe GetForegroundWindow is not, too).
'GFGetForeGroundWindowCaption
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Sub Timer1_Timer()
'on error resume next
Debug.Print GFGetForeGroundWindowCaption
End Sub
Private Function GFGetForeGroundWindowCaption()
On Error GoTo Error: 'important (WinNT); returns caption of window that has the keyboard focus
Dim FocusWindowHandle As Long
Dim WindowTitle As String
Dim WindowTitleLength As Long
Dim Temp As Long
'begin
FocusWindowHandle = GetForegroundWindow
If FocusWindowHandle = 0 Then
GFGetForeGroundWindowCaption = "[no window focused]"
Exit Function
Else
WindowTitleLength = 1024 'GetWindowTextLength(FocusWindowHandle)
WindowTitle = String$(WindowTitleLength, Chr$(0))
Temp = GetWindowText(FocusWindowHandle, WindowTitle, WindowTitleLength) 'Temp is length of title
If Not (Temp = 0) Then 'verify (Temp is WindowTitleLength)
GFGetForeGroundWindowCaption = Left$(WindowTitle, Temp)
Exit Function 'ok
Else
GFGetForeGroundWindowCaption = ""
Exit Function 'ok
End If
End If
Exit Function
Error:
GFGetForeGroundWindowCaption = "ERROR" 'error
Exit Function
End Function
[END OF FILE]