Question

If I have a program of Winform or Wpf, for example A.exe, I double-click it,a window shows, and I hide it,then double-click A.exe again, how can I make the hiden window show again?

summary for winform(1,4 and 5 are required) and Wpf(1,2 and 3 are required):

1. SingleInstanceAppliction Class

internal class SingleInstanceAppliction
{
    private static Mutex _mutex;
    static SingleInstanceAppliction()
    {
        WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
    }
    internal static int WM_SHOWME { get; private set; }
    internal static void SetGuid(string guid)
    {
        _mutex = new Mutex(true, guid);
    }
    internal static void ReleaseMutex()
    {
        _mutex.ReleaseMutex();
    }
    internal static bool IsFirst()
    {
        return _mutex.WaitOne(TimeSpan.Zero, true);
    }
    [DllImport("user32")]
    private static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
    [DllImport("user32")]
    private static extern int RegisterWindowMessage(string message);
    internal static void PostMessage()
    {
        PostMessage((IntPtr)0xffff, WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
    }
}

2.For Wpf in App.xaml.cs

    protected override void OnStartup(StartupEventArgs e)
    {
        SingleInstanceAppliction.SetGuid("{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
        if (SingleInstanceAppliction.IsFirst())
        {
            base.OnStartup(e);
            SingleInstanceAppliction.ReleaseMutex();
        }
        else
        {
            SingleInstanceAppliction.PostMessage();
            Shutdown();
        }
    }

3.For Wpf in MainWindow.xaml.cs

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        var source = PresentationSource.FromVisual(this) as HwndSource;
        source.AddHook(WndProc);
    }
    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == SingleInstanceAppliction.WM_SHOWME)
        {
            if (WindowState == WindowState.Minimized)
            {
                WindowState = WindowState.Normal;
            }
            var top = Topmost;
            Topmost = true;
            Topmost = top;
        }
        return IntPtr.Zero;
    }

4.For Winform in Program.cs

    [STAThread]
    private static void Main()
    {
        SingleInstanceAppliction.SetGuid("{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
        if (SingleInstanceAppliction.IsFirst())
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            SingleInstanceAppliction.ReleaseMutex();
        }
        else
        {
            SingleInstanceAppliction.PostMessage();
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

5.For Winform in Form1.cs

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == SingleInstanceAppliction.WM_SHOWME)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                WindowState = FormWindowState.Normal;
            }
            var top = TopMost;
            TopMost = true;
            TopMost = top;
        }
        base.WndProc(ref m);
    }
Was it helpful?

Solution

In Winforms (VB.NET) you can make it a single instance application

Project Properties > Application > Make single instance application

This option is not available in C# or in express editions of Visual Studio however: http://msdn.microsoft.com/en-us/library/vstudio/8fz4ssw2(v=vs.100).aspx

So this may be of use: How can I enforce a single instance of my application?

OTHER TIPS

You can try this:

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

    [DllImport("user32.dll")]
    static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);


    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        bool createdNew = true;
        using (Mutex mutex = new Mutex(true, "SomeUniqueName", out createdNew))
        {
            if (createdNew)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Application.Run(new MyForm());
            }
            else
            {
                Process current = Process.GetCurrentProcess();
                foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                {
                    if (process.Id != current.Id)
                    {
                        SetForegroundWindow(process.MainWindowHandle);
                        ShowWindowAsync(process.MainWindowHandle, 9);    //9 == SW_RESTORE
                        break;
                    }
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top