Set Focus to another programs window?

T

Tom

I am using VB.NET to control another program. I have the program's window
hwnd; however, I need to be able to set the focus to that other programs
window so I can send it some keystrokes. I thought I could just use the
SetFocus API, but I read somewhere that it will only work for programs
running under that programs thread. Is this true? If so, how can I set focus
to another program's window via VB.NET? Thanks.

Tom
 
G

Guest

Hi

First of all 'hwnd' is now 'Handle' in VB.NET

The easiest way would be to use 'AppActivate' if you know the window title:

AppActivate("Untitled - Notepad")

---

Another simple way is:

Dim intNotePad As Integer = Shell("C:\WINNT\Notepad.exe",
AppWinStyle.MinimizedNoFocus)

AppActivate(intNotePad)
 
T

Tom

Crouchie: Thanks, forgot about AppActivate.

One question, though: Is the process handle the same and the 'Handle' (i.e.
old hwnd)?

Tom
 
H

Herfried K. Wagner [MVP]

Tom said:
One question, though: Is the process handle the same and the 'Handle'
(i.e.
old hwnd)?

No. Process handles are different from window handles.
 
T

Tom

Herfried: Does this just bring the window to the front? Or does it also set
focus to the window?
 
H

Herfried K. Wagner [MVP]

Tom said:
Does this just bring the window to the front? Or does it also set
focus to the window?

MSDN on 'SetForegroundWindow':

"
The 'SetForegroundWindow' function puts the thread that created the
specified window into the foreground and activates the window. Keyboard
input is directed to the window, and various visual cues are changed for the
user.
"
 
T

Tom

Herfried: One more question related to this: Is it possible to set the focus
to another program's window and send it keystroks, YET still keep my VB
program in the foreground (i.e. showing on the screen)?
 
T

Tom

Herfried: One more question related to this: Is it possible to set the focus
to another program's window and send it keystroks, YET still keep my VB
program in the foreground (i.e. showing on the screen)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top