|
-
Jul 20th, 2010, 02:07 PM
#1
Thread Starter
Member
Finding the window that has focus on it
is there any way to find which window has focus on it?
-
Jul 20th, 2010, 06:58 PM
#2
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.
-
Jul 21st, 2010, 07:21 AM
#3
Thread Starter
Member
Re: Finding the window that has focus on it
...
and is it possible to get that programs executable path?
-
Jul 21st, 2010, 07:48 AM
#4
Re: Finding the window that has focus on it
There may well be a way but I'm not aware of it.
-
Jul 21st, 2010, 12:49 PM
#5
Junior Member
Re: Finding the window that has focus on it
 Originally Posted by sepehr
...
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
-
Jul 21st, 2010, 01:19 PM
#6
Thread Starter
Member
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?
-
Jul 21st, 2010, 01:43 PM
#7
Junior Member
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
-
Jul 21st, 2010, 03:54 PM
#8
Thread Starter
Member
Re: Finding the window that has focus on it
ok it works. from where did you get the information about declares in that code?
-
Jul 21st, 2010, 09:11 PM
#9
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
-
Jul 22nd, 2010, 12:13 PM
#10
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|