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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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_h wnd, txt, Len(txt))
txt = txt.Substring(0, length)
lb.Text = txt
lb.Refresh()
End Sub
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_h
txt = txt.Substring(0, length)
lb.Text = txt
lb.Refresh()
End Sub
https://www.experts-exchange.com/questions/20535377/vb-Net-tracking-processes.html