Resuming focus in VB.NET?

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
166
Location
Chicago
Programming Experience
10+
Is there an 'easy' way to find out the currently focused application on the users screen, and set focus back to it once you have done something to take focus off?

To elaborate a bit, I am using sendkeys to send strings and keyboard commands to a process in the background. This inevitably sets focus to that window for a second, so i would like to automatically have the previously focused window set back to focus, once the string is sent. (The sendkeys stuff is already taken care of).

I remember having to use findwindow/getforegroundwindow/setforegroundwindow api's in classic vb, but knowing .NET was designed to make life a bit easier, was wondering (hoping) for a class or something that could do this now?
Thanks in advance! :)
 
Thanks for the replies guys. :)

But actually I need to set the focus to the previous app with focus. Not necessarily my own app.

Example: The user may have Firefox as the app they were currently using, so I want to set it back to Firefox once my own app sends it's keystrokes to a different app (not Firefox).
 
Is there an 'easy' way to find out the currently focused application on the users screen, and set focus back to it once you have done something to take focus off?

To elaborate a bit, I am using sendkeys
Ugh.. You can probably do it with window messaging instead, then you dont have to have focus

To elaborate a bit, every window in windows, has a message queue and a loop that processes them. When you put clicks or keys intoa window, they are posted into the queue as messages to be processed. In time, they will be handed to the controls for processing. If you post a WM_SETTEXT message in, providing the control handle, and the message to set, then when that message reaches the top it will be handed to the relevant control, and that control (if it supports set text) will set its internal text to whatever you provided

SendKeys = evil, lame and a host of other derogotary adjectives.. Avoid it at all times
 
Hi

Thanks again. I was looking into using the sendmessage api, but again, like the last thread, I am sending it to a video game which I assume is directx. I have found no way to get the handle to the textbox to supply to the api.
 
AppleBag, you still have to use the Win32 APIs, .Net Framework doesn't have functionality for this. (except the AppActivate function)
 
That's what I was afraid of, lol.

I thought MS said .NET was the 'future'? They didn't even make Sendkeys more powerful by allowing the strings/key presses to be sent to a background process without forcing focus, and no possibility to find windows natively, lol.

ah well. Thanks for the reply John. :)
 
Back
Top