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.
LVL 1
techsuppoprtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You could use the SetForegroundWindow() API:
http://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx
0
techsuppoprtAuthor Commented:
Could you expand on what you said a bit ? I can't seem to figure it out.

Thank you.
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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?
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

techsuppoprtAuthor Commented:
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/
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It's certainly possible (and not that hard).

Will there only be ONE instance of each program running?
0
techsuppoprtAuthor Commented:
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 :)
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Here's an example that should get you started...the array "ps" will have all the running instances of notepad in it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public const int SW_RESTORE = 9;

        [DllImport("user32.dll")]
        public static extern bool IsIconic(IntPtr handle);

        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr handle, int nCmdShow);

        [DllImport("user32.dll")]
        public static extern int SetForegroundWindow(IntPtr handle);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process[] ps = Process.GetProcessesByName("notepad"); // <-- no path, no extension!
            if (ps.Length > 0)
            {
                MessageBox.Show(ps[0].MainWindowTitle);

                BringToForeground(ps[0].MainWindowHandle);
            }
        }

        private void BringToForeground(IntPtr extHandle)
        {
            if (IsIconic(extHandle))
            {
                ShowWindow(extHandle, SW_RESTORE);
            }
            SetForegroundWindow(extHandle);
        }

    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
techsuppoprtAuthor Commented:
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.
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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.
0
techsuppoprtAuthor Commented:
ok, understood. Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.