is there any way to find which window has focus on it?
Printable View
is there any way to find which window 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.
...
and is it possible to get that programs executable path?
There may well be a way but I'm not aware of it.
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
and is there any way to get the process name that has the focus on it?
use the processName in the code i posted earlierCode:Dim processId As Integer
GetWindowThreadProcessId(GetForegroundWindow(), processId)
Dim processName as string = process.GetProcessById(processId).processName
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
ok it works. from where did you get the information about declares in that code?
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
my app can not access the 64 bit programs...
is there any 64 bit version of visual studio?!