【问题标题】:when I double-click a program of Winform or Wpf twice当我双击 Winform 或 Wpf 的程序两次时
【发布时间】:2014-01-08 11:46:20
【问题描述】:

如果我有一个Winform或Wpf的程序,例如A.exe,我双击它,出现一个窗口,然后我隐藏它,然后再次双击A.exe,我怎样才能使隐藏窗口再次显示?

winform(需要1,4和5)和Wpf(需要1,2和3)的总结:

1。 SingleInstanceAppliction 类

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.App.xaml.cs中的Wpf

    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.MainWindow.xaml.cs中的Wpf

    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.Program.cs中的Winform

    [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.Form1.cs中的Winform

    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);
    }

【问题讨论】:

标签: c# wpf winforms single-instance


【解决方案1】:

在 Winforms (VB.NET) 中,您可以将其设为单实例应用程序

Project Properties > Application > Make single instance application

此选项在 C# 或 Visual Studio 的速成版中不可用:http://msdn.microsoft.com/en-us/library/vstudio/8fz4ssw2(v=vs.100).aspx

所以这可能有用:How can I enforce a single instance of my application?

【讨论】:

  • winformidea恐怕有误,我的专业版VS中只有“配置”、“平台”、“程序集名称”、“默认命名空间”、“目标框架” ,"输出类型","仅客户端框架子集","启动对象","程序集信息","资源",no "制作单实例应用程序"。
  • @PlantainYao 不确定您在看什么/在哪里,该选项位于“属性”->“应用程序”上方的 Save My.Settings on Shutdown。确保选中启用应用程序框架
  • @Mark Hall 我刚才弄错了,C#.Net的VS中没有“Make single instance application”复选框,而VB.Net的VS中有一个。
【解决方案2】:

你可以试试这个:

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;
                    }
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多