• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1513
  • Last Modified:

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.
0
techsuppoprt
Asked:
techsuppoprt
  • 5
  • 5
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 TomlinsonMiddle School Assistant TeacherCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
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
 
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 TomlinsonMiddle School Assistant TeacherCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now