Results 1 to 10 of 10

Thread: Finding the window that has focus on it

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    49

    Question Finding the window that has focus on it

    is there any way to find which window has focus on it?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finding the window that has focus on it

    Do you mean within your app or from any application? If it's the latter then you would need to use the Windows API, specifically the GetForegroundWindow function. That will give you the window's handle, which you can then use in other API calls. Search the web and you'll certainly find examples.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    49

    Re: Finding the window that has focus on it

    ...
    and is it possible to get that programs executable path?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finding the window that has focus on it

    There may well be a way but I'm not aware of it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    22

    Re: Finding the window that has focus on it

    Quote Originally Posted by sepehr View Post
    ...
    and is it possible to get that programs executable path?
    if you figure out the name of the process you could do something like this:

    Code:
            Dim pList() As Process
            Dim str As String = "firefox"
            pList = Process.GetProcesses
            For Each p As Process In pList
    
                If p.ProcessName = str Then
                    MsgBox(p.MainModule.FileName) 'exe path
                End If
            Next

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    49

    Re: Finding the window that has focus on it

    and is there any way to get the process name that has the focus on it?

  7. #7
    Junior Member
    Join Date
    Oct 2009
    Posts
    22

    Re: Finding the window that has focus on it

    Code:
    Dim processId As Integer
    GetWindowThreadProcessId(GetForegroundWindow(), processId)
    Dim processName as string = process.GetProcessById(processId).processName
    use the processName in the code i posted earlier
    here are the functions:

    Code:
        Private Declare Function GetForegroundWindow Lib "user32.dll" () As Int32
        Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessId As IntPtr) As IntPtr

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    49

    Re: Finding the window that has focus on it

    ok it works. from where did you get the information about declares in that code?

  9. #9
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Finding the window that has focus on it

    GetGUIThreadInfo can get all focus related info, including the caret.

    Code:
        Const NULL As Int32 = 0
        Private Declare Function apiGetGUIThreadInfo Lib "user32" Alias "GetGUIThreadInfo" (ByVal dwthreadid As Int32, ByRef lpguithreadinfo As GUITHREADINFO) As Int32
        Private Declare Function apiGetWindowThreadId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hWnd As Int32, ByVal lpdwProcessId As Int32) As Int32
        Private Declare Function apiGetWindowProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hWnd As Int32, ByRef lpdwProcessId As Int32) As Int32
    
        Private Function GetForegroundInfo() As GUITHREADINFO
            GetForegroundInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(GetForegroundInfo)
            apiGetGUIThreadInfo(apiGetWindowThreadId(NULL, 0), GetForegroundInfo)
        End Function
        Private Structure GUITHREADINFO
            Public cbSize, flags As Int32
            Public hwndActive, hwndFocus, hwndCapture, hwndMenuOwner, hwndMoveSize, hwndCaret As IntPtr
            Public rcCaret As RECT
        End Structure
        Private Structure RECT
            Public rLeft, rTop, rRight, rBottom As Int32
        End Structure
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' System.Threading.Thread.Sleep(3000)'Give you a moment to put focus somewhere
            Dim g As GUITHREADINFO = GetForegroundInfo()
            MessageBox.Show("ActiveForeground:" & g.hwndActive.ToInt32.ToString & "  Focus:" & g.hwndFocus.ToInt32.ToString & "  Capture:" & g.hwndCapture.ToInt32.ToString & "  Caret:" & g.hwndCaret.ToInt32.ToString & "  MenuOwner:" & g.hwndMenuOwner.ToInt32.ToString & "  MoveSize:" & g.hwndMoveSize.ToInt32.ToString & "  CaretLeft:" & g.rcCaret.rLeft & "  CaretRight:" & g.rcCaret.rRight & "  CaretTop:" & g.rcCaret.rTop & "  CaretBottom:" & g.rcCaret.rBottom, "Window Focus information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    49

    Unhappy Re: Finding the window that has focus on it

    my app can not access the 64 bit programs...
    is there any 64 bit version of visual studio?!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width