0

I need to be able to switch between two third-party applications based on the input from a COM port, i.e. when I receive Command1 from the device, make App1 the active (topmost) window, and when I receive Command2 from the device, make App2 the active window. Why do I want to do this? This application will be running on a self-service station and I will be monitoring the input of a motion sensor; so when nobody is in front of the station I need to show App2 (e.g. advertisement) and when someone stands in front I need to show App1 (e.g. main application).

I am thinking of an application that runs in the background and listens to the input on the COM port and switches the active window accordingly. So the question is, how can I change the active window using .NET? The OS will be Windows XP Pro SP2, but it would be better if it works on Windows 7 too.

MarioVW
  • 2,225
  • 3
  • 22
  • 28
  • FYI no such thing as a simple service that interacts with a users desktop in 7; http://stackoverflow.com/questions/267838/how-can-a-windows-service-execute-a-gui-application – Alex K. Feb 17 '12 at 17:09
  • Windows Services cannot interact with a user's desktop starting with Windows Vista. This shouldn't be a service anyway, just create a background application (i.e., a standard forms application that doesn't show any forms). Also, changing the active window is fundamentally broken. That's something that only the user should be able to do, not an application. Windows will fight you every step of the way, and your users won't be very happy. – Cody Gray - on strike Feb 17 '12 at 17:27
  • Changed the question to "background app", thanks... also added more info on _why_ I want to do this – MarioVW Feb 17 '12 at 17:35
  • Maybe you can use two Forms in a single application and just show and hide the appropriate one. I suppose it would be much easier and you can encapsulate logic for each of the windows in diffrent classes (or even dlls loaded by the application) to keep it spearated. – Lukasz M Feb 17 '12 at 19:19
  • @Lucas the problem is that these are both third-party applications, so I have no control over them. – MarioVW Feb 17 '12 at 19:32
  • Oh, I see. Sorry, I haven't noticed that. – Lukasz M Feb 17 '12 at 19:57

1 Answers1

2

You can use;

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

Then find the relevant process by name or by looping GetProcesses e.g. assuming a single instance:

Process[] process = Process.GetProcessesByName("myapp");
if (process.Length > 0 && process[0].MainWindowHandle != IntPtr.Zero) {
   SetForegroundWindow(process[0].MainWindowHandle);
}
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Everything you say is true, but I feel compelled to point out that [`SetForegroundWindow`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539.aspx) comes with...conditions. As I mentioned in a comment earlier, trying to change the active window from within an application to a window that is owned by a different process is typically discouraged. Malicious applications used to seek out ways of doing this to annoy their users, and the Windows team has been slowly eclipsing their abilities to do this. Chances are, this code will just cause the other app to flash its taskbar button. – Cody Gray - on strike Feb 17 '12 at 21:42
  • I tried out your code and it does work indeed. I want to point out the fact that I spawned both processes (third-party applications) from my application, so that might have helped. Still the behavior is somewhat inconsistent, because when I'm moving the mouse it does flash the taskbar instead of switching windows. Anyway, this solution seems to be good enough for my needs. – MarioVW Feb 17 '12 at 23:38