|
Pop up main Window using Win32API -- Joe --
I need only one instance of a Windows app running and also pop up its main form when the user double click the desktop shortcut (executable). See the code below: <code> using System; using System.Collections.Generic; using System.Windows.Forms; using System.ServiceProcess; using System.Diagnostics; using System.Net; using System.Runtime.InteropServices; namespace MyNamespace { public class MyClass { public enum ShowWindow { SW_HIDE = 0, SW_SHOWNORMAL = 1, SW_NORMAL = 1, SW_SHOWMINIMIZED = 2, SW_SHOWMAXIMIZED = 3, SW_MAXIMIZE = 3, SW_SHOWNOACTIVATE = 4, SW_SHOW = 5, SW_MINIMIZE = 6, SW_SHOWMINNOACTIVE = 7, SW_SHOWNA = 8, SW_RESTORE = 9, SW_SHOWDEFAULT = 10, SW_FORCEMINIMIZE = 11, SW_MAX = 11 } private static void Main() { Process launchedProcesses = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); if (launchedProcesses.Length > 1) { // need to pop up window here RestoreWindow(launchedProcesses .MainWindowHandle); return; } Application.Run(new frmMain()); } public static void RestoreWindow(IntPtr handle) { API.ShowWindowAsync(handle, (int)ShowWindow.SW_SHOWNORMAL); API.SetForegroundWindow(handle).ToString(); } } public static class API { public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); public static extern bool SetForegroundWindow(IntPtr hWnd); } } </code> Everything is working but a little strangely. Sometime the window pop up sometime not. Also when I set the property ShowInTaskBar of the main form to false (because it is minimized to system tray) the code do not work at all. Help please!!!! |
|
-- Tito --
You can use the system.process class to determine if the application is running or not. If you do find it running, you can either kill it or switch to it. |
|
-- Joe --
Tito, can I switch to it and pop up its main window up? I will try to do it with that Process class. |
|
-- JamieFraser --
You can't do that with the process class. Have a look on google / codeproject for "single instance app" or something like that. Or if you are using .NET 2.0, you can set a setting in the project to ensure that only one instance of the app can run at a time. On 18/07/06, Joe <jonathanpoulin> wrote: Tito, can I switch to it and pop up its main window up? I will try to do it with that Process class. > |
|
-- Joe --
Thanks! For the option is VS2005 I can't find it. Any advise? Thanks again man! Joe. |