Link to home
Start Free TrialLog in
Avatar of macjonesnz
macjonesnz

asked on

How to get name of window with focus.

Hi,

Can somepne please give me a vb.net code sample to get the current active window.

i.e. 3 apps are running (calculator, Word, Excel), and calculator has the focus, I want my vb.net app to be able to get the window name and/or .exe name.

Regards
Mac
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of prakash_prk
prakash_prk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of iboutchkine
iboutchkine

Private Declare Function GetActiveWindow Lib "user32" () As Integer
 Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
 Private Declare Function GetForegroundWindow Lib "user32" () As Integer


 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim foreground_hwnd As Integer
        Dim txt As String
        Dim length As Long

        foreground_hwnd = GetForegroundWindow()

        txt = Space$(1024)
        length = GetWindowText(foreground_hwnd, txt, Len(txt))
        txt = txt.Substring(0, length)
        lb.Text = txt
        lb.Refresh()
    End Sub