How to get what application is connected to the topmost window? - vb.net

My VB.NET application is suposed to monitor what application currently is running in the topmost window. I have tried the following approach using a timer:
Declare Function GetActiveWindow Lib "user32" () As System.IntPtr
Declare Function GetForegroundWindow Lib "user32" () As System.IntPtr
Public Declare Auto Function GetWindowText Lib "user32" _
(ByVal hWnd As System.IntPtr, _
ByVal lpString As System.Text.StringBuilder, _
ByVal cch As Integer) As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Caption As New System.Text.StringBuilder(256)
Dim hWnd As IntPtr = GetForegroundWindow()
GetWindowText(hWnd, Caption, Caption.Capacity)
'Caption now holds the title of the topmost window
End Sub
By this I can see that for example Outlook or Internet Explorer is the topmost window as the name is in Window's title bar. However, if the user crates a new mail in Outlook the title of the window is "Untitled message" givning no hint of what application is running in the the window.
How do I get what application is connected to the topmost window?p>
Help is appreciated!

You need to pinvoke GetWindowThreadProcessId(). That gets you the ID of the process that owns the window. Back to managed code, Process.GetProcessById() gives you details of the process.

Related

Accessing buttons on the Internet Explorer 11 View downloads form

I have a piece of code in vb.net to open a web link for an application and to download a file from the page. The web browser loads the page and the program auto clicks the link on the page to download the file which then makes the "View Downloads - Internet Explorer" window in IE 11 to pop up as shown below:
Internet Explorer 11- VIew Downloads window
I managed to activate the window but not able to click the Save button to automatically download the file without user intervention. The code that I tried to activate and click the button is as below
Private Declare Auto Function FindWindow Lib "user32" (ByVal ClassName As String, ByVal WindowTitle As String) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32" (ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal ClassName As String, ByVal WindowTitle As String) As IntPtr
Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim ParentWndHandle As IntPtr
ParentWndHandle = FindWindow("#32770", "View Downloads - Internet Explorer")
If ParentWndHandle <> IntPtr.Zero Then
Dim ChildWndHandle As IntPtr
ChildWndHandle = FindWindowEx(ParentWndHandle, vbNullString, "DirectUIHWND", vbNullString)
Dim btn1 As IntPtr = FindWindowEx(ChildWndHandle, IntPtr.Zero, "Button", "&Save")
SendMessage(ChildWndHandle, WM_ACTIVATE, WA_ACTIVE, 0)
SendMessage(ChildWndHandle, WM_COMMAND, VK_RETURN, IntPtr.Zero)
End If
End Sub
I think your issue might be the same as this thread: You need to use SetActiveWindow before SendMessage.
Or can you extract the file url? If so, you can refer to this doc to use the simple DownloadFile method to download files:
My.Computer.Network.DownloadFile(
"http://www.example.com/downloads/abc.txt",
"C:\Documents and Settings\All Users\Documents\abc.txt")

Trouble bringin window to focus in VB.NET

I am having trouble bringing a window to focus. All examples show using FindWindow function and calling SetForegroundWindow, but this did not work.
Here is what that code looked like
thandle = FindWindow(Nothing, "title of window")
SetForegroundWindow(thandle)
I then tried the ShowWindow functions. The below code worked if the window was minimized, and if the window wasn't maximized but it refused to bring focus to the window if it was already maximized.
If IsIconic(thandle) Then
ShowWindow(thandle, 9)
Else
ShowWindow(thandle, 3)
So then I came up with the work around that if the window isn't minimized, minimize it and then maximize it.
If IsIconic(thandle) Then
ShowWindow(thandle, 9)
Else
ShowWindow(thandle, 7)
ShowWindow(thandle, 9)
End If
I would really like to know why SetForegroundWindow does not bring the window to the foreground
Here is my code:
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long
Private Declare Auto Function FindWindow Lib "USER32.DLL" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim thandle = FindWindow(Nothing, "Calculator")
SetForegroundWindow(thandle)
End Sub

Set focus to the taskbar [duplicate]

This question already has an answer here:
How to make a window active in vb.net
(1 answer)
Closed 5 years ago.
I'm writing a simple Visual Basic app to help me launch global hotkeys by the press of a taskbar button.
To do this, I basically set the app to minimize itself back to the taskbar. I then want to activate the taskbar itself (not the previously active program) so I can use SendKeys to do these keypresses. In my test, the keypresses get registered. Its just that I can't seem to find out how to actually activate the taskbar from my program.
If I use AppActivate, I need the processID or the window title.
So it seems best to use Windows API's to do it, but I'm not getting the result I'm after either and I must be doing something wrong.
Here's my code:
Public Class Form1
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Sub Form1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
Me.WindowState = FormWindowState.Minimized
Dim intReturn As Integer = FindWindow("Shell_traywnd", "")
AppActivate(intReturn)
SendKeys.Send("%1")
MsgBox("test")
End Sub
End Class
The error I'm getting is that there's no process by this ID running.
FindWindow will return a hwnd handle, not a processID. You will need to use the function SetForegroundWindow to activate it.
Your code becomes as follows:
Public Class Form1
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
Private Sub Form1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
Me.WindowState = FormWindowState.Minimized
Dim intReturn As Integer = FindWindow("Shell_traywnd", "")
SetForegroundWindow(intReturn)
SendKeys.Send("%1")
MsgBox("test")
End Sub
End Class

SensKeys on a window

i was programming an hack for a game in Visual basic, i want simulate a keypress with the code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
SendKeys.Send("a")
End Sub
In my window game didn't work, but in other windows such as chrome or a test textbox work, why?
if this is not a flash game , you can get the process of the game by:
Dim TheGame as Process = Process.GetProcessesByName("YourGame")(0)
get the handle of the game window:
Dim Handle As IntPtr = TheGame.MainWindowHandle
and then use sendmessage function to send the keys to the window:
SendMessage(Handle, 258, PUT KEY HERE, 0)
for example - instead of the "PUT KEY HERE" type:
&H41 (is "A")
you can find the keys list here:
http://www.pinvoke.net/default.aspx/Enums/VirtualKeys.html
the user32.dll sendmessage function:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
Edit:
if the code is not working change the 258 (wm_char) in the sendmessage to 256(wm_keydown)
note:sendkeys sends keys to the current active window, and you should try to use pinvoke functions first and if you don't have any choice then use sendkeys.
Recommendation:
explore the functions: postmessage,sendmessage,setforegroundwindow and also explore spy++ tool :).

VB.NET Send Tab key to another application window

I want to send "{TAB}" Key to another application window(send the key to the window not to textbox).
I tried:
SendMessage(hWnd, WM_SETHOTKEY, VK_TAB, 0)
Nothing happened.
my goal is:
send tab key to my application Or other application when the application window is not in focus.
(i know that sendkey is not professional in this case there is no choice(This is the first time that I'm using it).)
I made many attempts and I always returned to the same result:
Nothing happened.
Does anyone know the answer?
SendKeys requires the application that you are sending the Keys to, to be active.
From above Link:
Use SendKeys to send keystrokes and keystroke combinations to the active application.
I order to get around this limitation you will have to resort to using the WinApi Functions.
FindWindow pInvoke.net
FindWindowEx pInvoke.net
sendMessage pInvoke.net
See this MSDN Forum Post for an example
Here is a modified example from that Posting:
Public Class Form1
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd As IntPtr, ByVal hWndChildAfterA As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
Const WM_SETTEXT As Integer = &HC
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim destination As IntPtr = FindWindow(Nothing, "Untitled - Notepad")
Dim destControl As IntPtr = FindWindowEx(destination, IntPtr.Zero, "Edit", Nothing)
SendMessage(destControl, WM_SETTEXT, IntPtr.Zero, "Hello" & vbTab & "GoodBye" & vbCrLf)
End Sub
End Class
Added an Additional Example using WM_KEYDOWN I created another small application with the Window Title set to TestForm and overrode the WndProc Method to determine if the application got the TabKey.
Sending Form
Public Class Form1
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
Const WM_KEYDOWN As Integer = &H100
Const VK_TAB As Integer = &H9
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim destination As IntPtr = FindWindow(Nothing, "TestForm")
SendMessage(destination, WM_KEYDOWN, VK_TAB, 0)
End Sub
End Class
Test Form
Put a breakpoint on MyBase.WndProc(m) and look at m to see what has been sent.
Public Class Form1
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
End Sub
End Class
Having struggled with this type of this a few times before, i would suggest a couple of things to look at.
The 1st is autoit which includes a dll you can reference from vb.net, and is very simple you use, and well documented. I tend to use that whenever i need to control a 3rd party program.
The other is the ui automation classes
See this for an example:
http://blog.functionalfun.net/2009/06/introduction-to-ui-automation-with.html
you need make the other window active first. check Change focus to another window in VB.NET . then use send key.