Getting Window Text

Maaatt

Member
Joined
Sep 4, 2006
Messages
16
Programming Experience
Beginner
I was trying to make something totally different before, a compiler for code on the fly, and i did it and that was fun but now i'm gonna try something totally different. I've decided to make something completely different and i'm hoping somebody here can help me out with this speedbump.

I'd like to get the window title of the application in focus, whether it be Calculator, Pinball, Internet Explorer, PhotoShop, anything - and write the window title to a textbox on my VB.NET form.

I've been searching for it for a solution on a few hours now but it all seems very complicated and i'm no guru when it comes to API stuff... Has anybody got some tips or perhaps a quick, simple solution?
 
Since there is no functionality for this in the .Net framework (only indirectly for processes, but no indication which is foreground window), you have to use Win32 API methods GetForegroundWindow, GetWindowTextLength and GetWindowText. Example:

VB.NET:
Declare Function GetForegroundWindow Lib "user32.dll" () As Int32
 
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" ( _
ByVal hwnd As Int32, ByVal lpString As String, ByVal cch As Int32) As Int32
 
Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Int32) As Int32
 
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
  Dim hwnd As Integer = GetForegroundWindow
  Dim tlen As Integer = GetWindowTextLength(hwnd) + 1
  If tlen > 1 Then
    Dim str As New String(" ", tlen)
    tlen = GetWindowText(hwnd, str, tlen)
    MsgBox(str)
  Else
    MsgBox("no window title")
  End If
End Sub
You see the code is in button click, which consequently when clicking that button your own Window will be foreground, somewhat defeates the whole point don't it? ;) To make something happen in your own application when another application is active you either have to (a) make it respond to some external event, (b) set a timer to happen at a interval or (c) set up your application to use global system hotkeys (yet more Win32 API). There is Hotkey tips and code at this thread http://www.vbdotnetforums.com/showthread.php?t=7471 (5th post is complete code needed to set up a global hotkey)
 
Thanks for that, i'm yet to try it but thanks :p

I was thinking that maybe when the mouse is clicked, it'd check for the window name rather than the button lol. I' guess it'd be kinda tough but i'm up for a challenge, thanks for your help though, i would've had a lot of trouble doing what you did, i can do a lot of things but API work makes me shudder :eek:
 
Not exactly, even if the button itself also got a 'window handle', what is retrieved when clicking the example code button is the text of the foreground form window title. But it's more interesting that GetForegroundWindow always gives you the handle to what window is currently in 'front', so with a timer you can for instance analyze the usage ratio for different applications..
 
Back
Top