Focus another application

K

Kbalz

Hello - I've tried to search this, but the word Focus in searching
always returns lots of control focusing.

I would like my C# Win App to minimize, and focus a program that is
already running, then send mouse clicks / keyboard interaction.. and
when complete, restore my app, and focus it of course.

My troubles are finding how to focus the other program. Not looking
for code solutions, just maybe the MSDN location of the namespace, or
an article example, thanks!
 
P

Peter Duniho

Hello - I've tried to search this, but the word Focus in searching
always returns lots of control focusing.

Yup. You're really asking more about windows and activating, while
controls are all about focusing. :)
I would like my C# Win App to minimize, and focus a program that is
already running, then send mouse clicks / keyboard interaction.. and
when complete, restore my app, and focus it of course.

My troubles are finding how to focus the other program. Not looking
for code solutions, just maybe the MSDN location of the namespace, or
an article example, thanks!

AFAIK, this wasn't available under .NET 3.0, which includes the Windows
class (in the Windows Presentation Foundation). You can use
Windows.Activate(). However, the docs mention that you need (in addition
to .NET 3.0) the UnmanagedCode permission; this may or may not be a
problem for you.

Pre-3.0, I think you may need to use p/invoke and call the native Windows
SetForegroundWindow() function directly.

Keep in mind that you will want to set the other application's window as
the foreground window *before* you minimize yourself. Minimizing may
cause your application to no longer be the foreground application, which
will prevent it from successfully bringing the other application to the
foreground.

Pete
 
K

Kbalz

Yup. You're really asking more about windows and activating, while
controls are all about focusing. :)



AFAIK, this wasn't available under .NET 3.0, which includes the Windows
class (in the Windows Presentation Foundation). You can use
Windows.Activate(). However, the docs mention that you need (in addition
to .NET 3.0) the UnmanagedCode permission; this may or may not be a
problem for you.

Pre-3.0, I think you may need to use p/invoke and call the native Windows
SetForegroundWindow() function directly.

Keep in mind that you will want to set the other application's window as
the foreground window *before* you minimize yourself. Minimizing may
cause your application to no longer be the foreground application, which
will prevent it from successfully bringing the other application to the
foreground.

Pete

I probably will be developing this later this year / early next year,
so it should be using 3.0 or 3.5 (whichever is the latest &
greatest).. I was just looking ahead doing some research, currently
I'm using .NET 2.0 with VS 2005 Pro sp1.

Good point about the order of events.

Are you familiar with a program called Automate? This program does
exactly what I'm after, focusing windows or processes, sending mouse/
key clicks to it... I think it uses VB 6 in the version I have at
work.

I'll run more searches using "Activate" instead of "Focus" for
keywords. Also look at what you've suggested for method calls, thanks
a bunch.
 
P

Peter Duniho

I probably will be developing this later this year / early next year,
so it should be using 3.0 or 3.5 (whichever is the latest &
greatest).. I was just looking ahead doing some research, currently
I'm using .NET 2.0 with VS 2005 Pro sp1.

Please note that when I wrote "this wasn't available under .NET 3.0" what
I meant to write was actually "this wasn't available until .NET 3.0".
Looks like you figured that out, but I should clarify anyway. :)

Pete
 

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