Mutex从最小化带回来

问题描述 投票:0回答:1

我当时正在使用互斥锁检查一个实例。它有效,但要完美我需要修复一个bug。如果程序处于最小化状态,则单击“确定”后将无法自行恢复。有任何想法吗?

这是在Program.cs中:

if (process.Id != current.Id)
{
    SetForegroundWindow(process.MainWindowHandle);
    MessageBox.Show(new Form1 { TopMost = true }, "Application is already running!");

    Form1 f1 = new Form1();

    f1.WindowState = FormWindowState.Normal; // dont work
    f1.BringToFront();                       // dont work
    f1.Focus();                              // dont work

    break;
}
c# mutex
1个回答
0
投票

创建一个扩展方法:

using System.Runtime.InteropServices;

namespace System.Windows.Forms
{
    public static class Extensions
    {
        [DllImport( "user32.dll" )]
        private static extern int ShowWindow( IntPtr hWnd, uint Msg );

        private const uint SW_RESTORE = 0x09;

        public static void Restore( this Form form )
        {
            if (form.WindowState == FormWindowState.Minimized)
            {
                ShowWindow(form.Handle, SW_RESTORE);
            }
        }
    }
}

然后在你的代码中使用form.Restore()

© www.soinside.com 2019 - 2024. All rights reserved.