4

I'm trying to check if javaw.exe has focus, then execute certain code if it does.

Previously I had code which would look for the process ID of javaw.exe, then compare it to the process which currently had focus, which worked for awhile, but then I noticed when I had more than one javaw.exe process running, it would only work on one of those processes, while I need it to work when any javaw.exe process has focus.

Is there any way to do this?

User5152
  • 131
  • 9
  • 1
    `System.Diagnostics.Process.GetCurrentProcess()` – Sam Axe Jan 26 '17 at 18:21
  • 1
    Whoops. You are correct. You'll need the `GetForgroundWindow` Win32 API function (which returns a window handle), then loop over the currently running processes, checking each for the javaw.exe process name and compare it's Main Window Handle to the foreground window handle. – Sam Axe Jan 26 '17 at 19:20
  • http://stackoverflow.com/questions/884256/how-to-determine-if-an-process-is-the-currently-active-foreground-application – Sam Axe Jan 26 '17 at 19:23
  • @SamAxe : No need to loop. You can use the [**`GetWindowThreadProcessId()`**](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx) function to get the id of the process owning the window. – Visual Vincent Jan 26 '17 at 20:47
  • Possible duplicate of [How to determine if an process is the currently active / foreground application](http://stackoverflow.com/questions/884256/how-to-determine-if-an-process-is-the-currently-active-foreground-application) – Sam Axe Jan 26 '17 at 21:06

1 Answers1

4

You can determine this quite easily using the GetForegroundWindow() and GetWindowThreadProcessId() WinAPI functions.

First call GetForegroundWindow to get the window handle of the currently focused window, then call GetWindowThreadProcessId in order to retrieve the process id of that window. Finally get it as a Process class instance by calling Process.GetProcessById()

Public NotInheritable Class ProcessHelper
    Private Sub New() 'Make no instances of this class.
    End Sub

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function GetForegroundWindow() As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As UInteger) As Integer
    End Function

    Public Shared Function GetActiveProcess() As Process
        Dim FocusedWindow As IntPtr = GetForegroundWindow()
        If FocusedWindow = IntPtr.Zero Then Return Nothing

        Dim FocusedWindowProcessId As UInteger = 0
        GetWindowThreadProcessId(FocusedWindow, FocusedWindowProcessId)

        If FocusedWindowProcessId = 0 Then Return Nothing
        Return Process.GetProcessById(CType(FocusedWindowProcessId, Integer))
    End Function
End Class

Usage example:

Dim ActiveProcess As Process = ProcessHelper.GetActiveProcess()

If ActiveProcess IsNot Nothing AndAlso _
    String.Equals(ActiveProcess.ProcessName, "javaw", StringComparison.OrdinalIgnoreCase) Then
    MessageBox.Show("A 'javaw.exe' process has focus!")
End If

Hope this helps!

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75