Link to home
Start Free TrialLog in
Avatar of techsuppoprt
techsuppoprt

asked on

Simulating ALT-TAB aka switching between open programs

I've seen it done but not sure how to do it.
I need to simulate what ALT-TAB does without actually pressing/simulating ALT-TAB key press.

Here is what I've seen done and what I would like to do:
- I have to different programs running on my computer
- I start my application and it somehow captures and renames those windows to "Window1" and "Window2". I can physically see those windows names change down in a Windows system tray from whatever they're program names to Window1 and 2.
- From there I want my application to to switch back and forth between my Window1 or 2 any time I want to do work.

So at the end it's almost like simulating ALT-TAB except that I want to have more control and to know exactly which program/window to activate.

I know it's possible as I've seen it done. I just had to run those applications as an administrator.
Thank you.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

You could use the SetForegroundWindow() API:
http://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx
Avatar of techsuppoprt
techsuppoprt

ASKER

Could you expand on what you said a bit ? I can't seem to figure it out.

Thank you.
Is YOUR app going to start the two other apps?

If not, do they have fixed window captions?
If not, are they do have different EXE names?

Do you already have the code to rename them Window1 and Window2?

It's hard to tell from your post how far into this you have already gotten...which parts do you have done?

...or is the whole thing what you're trying to achieve?
SOrry, I was out of town.
I have not even started on this. I'm just trying to get an idea if this can be done and whats would be the best way to go about it.

Thank you/
It's certainly possible (and not that hard).

Will there only be ONE instance of each program running?
Two instances of the same program.
I want to be able to access either window at any time.
Without simulating ALT-TAB I want to decide whenever I want something to be sent to Window A or Window B.

Hope it makes sense :)
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you but this code I believe will simply select and set focus on the Notepad window.
My question really is what if I have two instances of Notepad open.

How can I rename one to Notepad1 and another one to Notepad2 so that I can quick toggle between the two with the logic you provided above. That's the problem here.

Thank you.
The code in the button1_Click() handler will get Process instances for ALL the running instances of Notepad.

The example is merely working with the first instance by using "ps[0]".

If you wanted to deal with a different instance then replace "0" with whatever index you want.  For example, if there were two instances then you could access the first instance with "ps[0]" and the second instance with "ps[1]".

The window title is irrelevant in my opinion as you can access them by Index (as shown above).  If you really need to access the window text than I also gave an example of that with "ps[0].MainWindowTitle".

If you really need to change the window title then look at the SetWindowText() API...but, again, I think this is un-necessary.
ok, understood. Thank you!