Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get caption for active window

Status
Not open for further replies.

Niphyr

Programmer
Jul 21, 2003
85
AU
Is there a way i can get the caption for the current window when i want it, eg. when a timer event occurs?

thanks ahead.

-Niphyr

------------------------------
------------------------------
 
There's a way, but I haven't done it in a few years...
You have to know the length of the text, so you'll need getwindowtextlength along with getwindowtext. You'll need the handle of the window in question as well. Sorry, I don't have time to work out the exact solution... look into these:

Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long


[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!

Ever feel like you're banging your head against a tree? I did, so I cut down the tree.
 
if you use the getactivewindow api, is that the ByVal hwnd value needed for the other above API's?

------------------------------
------------------------------
 
If you want to get the title of the active window in your own application, then you should consider the ActiveForm property of the Screen Object.
___
[tt]
Private Sub Timer1_Timer()
On Error Resume Next
Debug.Print Screen.ActiveForm.Caption
End Sub[/tt]
___

You can also get this title using the GetWindowText and GetActiveWindow API functions as mentioned by TheTuna.
___
[tt]
Option Explicit
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetActiveWindow Lib "user32" () 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 Sub Timer1_Timer()
Debug.Print GetCaption(GetActiveWindow)
End Sub

Function GetCaption(ByVal hWnd As Long) As String
Dim S As String * 1024
GetCaption = Left$(S, GetWindowText(hWnd, S, Len(S)))
End Function[/tt]
___

On the other hand if you want to retrieve the title of the foreground window on which the user is working and has got the keyboard focus (regardless if it belongs to your application or not), then you should consider the GetForegroundWindow function.
___
[tt]
Private Sub Timer1_Timer()
Debug.Print GetCaption(GetForegroundWindow)
End Sub[/tt]
___

The rest of the code (GetCaption function) remains same.
 
Thanks, ill try it and give some feedback, looks very good though Hypetia. You've solved one of my problems yet again. Thanks tuna for the ideas too.

-Niphyr

------------------------------
------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top