1. On Windows CE/Windows Mobile only 1 instance of any executable file is allowed to run at a time. The following will start the application if it's not running, OR bring its active window to the foreground if it's already running
Process.Start(Assembly.GetExecutingAssembly().GetModules(0).FullyQualifiedName, "")
The code can be called by the application itself, or by a different application. It will not work if the application is busy at the time the code is executed.
2. On both Windows CE and Windows XP, we can do so by finding the active window handle and call SetForegroundWindow
This makes use of the MainWindowHandle property of the .NET's Process class to retrieve the handle to the active window of the current process.
SetForegroundWindow(Process.GetCurrentProcess().MainWindowHandle)
3. In Win32 C++
Making a window the foreground window requires more than just calling the SetForegroundWindow API. You must first determine the foreground thread and attach it to your window, using AttachThreadInput, then call SetForegroundWindow. That way they can share input states.
First I call GetForegroundWindow to get the handle of the current foreground window. Then a few calls to GetWindowThreadProcessId retrieve the threads associated with the current foreground window and the window I want to bring to the foreground. If these threads are the same a simple call to SetForegroundWindow is all that is necessary. Otherwise, the foreground thread is attached to the window that I am bringing to the front and detached from what was the current foreground window. The AttachThreadInput API handles this.
Once this is accomplished, the state of the window is determined. If IsIconic says that the window is minimized, I restore it by issuing ShowWindow with the SW_RESTORE flag. Otherwise ShowWindow is called with the SW_SHOW flag.
Reference:
Simulate Window's Alt-Tab List and
Bring a Window to the Foreground, http://www.thescarms.com/vbasic/alttab.aspx
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.