Calling ALT-TAB

  • Thread starter Thread starter Chris Mason
  • Start date Start date
C

Chris Mason

I have an application that runs full screen on a system and I would like to
be able to (at times) switch another program using an ALT-TAB like
interface. However the system only has a touchscreen display so doing this
from a keyboard is not an option. Is there any way I can instantiate the
built in ALT-TAB window or is there any way I can generate the ALT-TAB
event/message progammatically.

Thanks for any help.
Chris
 
Interesting. I vaguely remember doing something similar in VB6 so I
could use SendKeys to populate fields in another application.
Unfortunately, that was quite some time ago, and in the wrong
language, but at least it is possible to do a similar thing.

IIRC, I had to check the title parameter of the currently running
windows, decide which to switch to, and then run the switch.

I know it's not a lot of use to you, but at least now you know it can
be done.
 
Hi,

You will have to PInvoke, first you need to get the handle of the target
window, then you need to give it focus.

Here is some code:

[DllImport("coredll",EntryPoint="FindWindow")]
public static extern IntPtr FindWindow(string lpClassName,string
lpWindowName);

[DllImport("coredll",EntryPoint="SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);


//this is how you use it

IntPtr mainwin = FindWindow(null, "Product Details");

if(! mainwin.Equals(new System.IntPtr(0)))
{
//Ask for login/Password
SetForegroundWindow(mainwin);
}


cheers,
 
hi,

sorry, I took the declarations from the pocketpc app, the correct dll to
import is user32.dll no coredll.dl

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You will have to PInvoke, first you need to get the handle of the target
window, then you need to give it focus.

Here is some code:

[DllImport("coredll",EntryPoint="FindWindow")]
public static extern IntPtr FindWindow(string lpClassName,string
lpWindowName);

[DllImport("coredll",EntryPoint="SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);


//this is how you use it

IntPtr mainwin = FindWindow(null, "Product Details");

if(! mainwin.Equals(new System.IntPtr(0)))
{
//Ask for login/Password
SetForegroundWindow(mainwin);
}


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Chris Mason said:
I have an application that runs full screen on a system and I would like
to
be able to (at times) switch another program using an ALT-TAB like
interface. However the system only has a touchscreen display so doing
this
from a keyboard is not an option. Is there any way I can instantiate the
built in ALT-TAB window or is there any way I can generate the ALT-TAB
event/message progammatically.

Thanks for any help.
Chris
 
Well the SendKeys method looks like it has some hope. A couple of small
issues though. The first is that when the alt-tab window comes up, it just
goes right away. It does not stay for any length of period. Do you know if
there is a way to send the key and have it stay until the user selects a
program from the list. SendKeys.SendWait() did not seem to help either.

The second problem was that the alt-tab window would only come up if I had
another window such as a messagebox come up after it. Otherwise nothing
would have happened. Any idea what may be causing that?

Thanks again
 
Thanks for the suggestions, but it is not quite what I need. The problem is
that I do not know what the other program that I am going to be changing
focus is. It could be any application. And I was trying to avoid having to
go through and enumerate all the windows that are open, put them in a box
for the user to choose from, and then make the switch... especially when
windows does all that for me when I figure out how to tap into that :)

Chris

Ignacio Machin ( .NET/ C# MVP ) said:
hi,

sorry, I took the declarations from the pocketpc app, the correct dll to
import is user32.dll no coredll.dl

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:e8qUF%[email protected]...
Hi,

You will have to PInvoke, first you need to get the handle of the target
window, then you need to give it focus.

Here is some code:

[DllImport("coredll",EntryPoint="FindWindow")]
public static extern IntPtr FindWindow(string lpClassName,string
lpWindowName);

[DllImport("coredll",EntryPoint="SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);


//this is how you use it

IntPtr mainwin = FindWindow(null, "Product Details");

if(! mainwin.Equals(new System.IntPtr(0)))
{
//Ask for login/Password
SetForegroundWindow(mainwin);
}


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Chris Mason said:
I have an application that runs full screen on a system and I would like
to
be able to (at times) switch another program using an ALT-TAB like
interface. However the system only has a touchscreen display so doing
this
from a keyboard is not an option. Is there any way I can instantiate the
built in ALT-TAB window or is there any way I can generate the ALT-TAB
event/message progammatically.

Thanks for any help.
Chris
 
If you want to emulate the behaviour of ALT+TAB key combination just like if
you're pressing and holding the keys, you can use PInvoke calling the
keybd_event() API, instead of using the SendKeys class. The API provides
more possibilities to send keystrokes and emulate keystroke behaviour.

Here is an example for your situation:

[DllImport("user32")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int
dwExtraInfo);
private const byte VK_MENU = 0x12;
private const byte VK_TAB = 0x09;
private const int KEYEVENTF_EXTENDEDKEY = 0x01;
private const int KEYEVENTF_KEYUP = 0x02;

private void button1_Click(object sender, System.EventArgs e)
{
keybd_event(VK_MENU,0,0,0);
keybd_event(VK_TAB,0,0,0);
System.Threading.Thread.Sleep(1000);
keybd_event(VK_TAB,0,0,0);
System.Threading.Thread.Sleep(1000);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
}

Hope it helps.

-- Ricky Lee
==================================================
^o^ "When all doors are closed, God will open a Windows" ^o^
==================================================
 
Back
Top