See what window has focus

Jimmythegreat

Member
Joined
Jul 19, 2006
Messages
17
Programming Experience
3-5
Why was my last post of this deleted?

I need to be able to see what window has focus. If i have two windows I need to be able to figure out which one has focus.

Focus being "grayed out" in windows taskbar.

Thanks
Jimmy
 
You'll need to use an windows api then. I think. There might be a .Net way to do it.

Maybe GetForegroundWindow. You'll have to P/Invoke it.
 
You should be getting a handle to the window, an IntPtr.

Loop through all the processes and see if that handle matches the processes MainWindowHandle.

Private Function GetProcessByHandler(ByVal handle As IntPtr) As String
For Each p As Process In Process.GetProcesses
If handle.Equals(p.MainWindowHandle) Then
Return p.ProcessName
End If
Next
End Function
 
I did something a little bit easyer:

VB.NET:
    Declare Function GetWindowThreadProcessId Lib "user32.dll" ( _
    ByVal hwnd As Int32, _
    ByRef lpdwProcessId As Int32) As Int32

-----
        Dim processID As Int32
        GetWindowThreadProcessId(GetForegroundWindow(), processID)
        TextBox1.Text = Process.GetProcessById(processID).MainWindowTitle
 
Back
Top