从 Main 函数最大化窗口?

发布于 2024-11-07 00:23:50 字数 872 浏览 2 评论 0原文

我已经使用互斥体来运行单实例程序,现在我希望当用户重新打开应用程序时窗口当前最小化,使其最大化。

这是我当前在 Program.cs 文件中的代码:

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

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        bool Ok = true;
        string ProductName = Application.ProductName;
        Mutex m = new Mutex(true, ProductName, out Ok);
        if (!Ok)
        {
            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName(ProductName);
            SetForegroundWindow(p[0].MainWindowHandle);

    }
    else
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

    }
}

I have used a mutex to run a single instance program, and now I want the window to become maximized if it is currently minimized when the user reopens the application.

Here is the code I currently have in my Program.cs file:

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

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        bool Ok = true;
        string ProductName = Application.ProductName;
        Mutex m = new Mutex(true, ProductName, out Ok);
        if (!Ok)
        {
            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName(ProductName);
            SetForegroundWindow(p[0].MainWindowHandle);

    }
    else
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偷得浮生 2024-11-14 00:23:50

您正在寻找 ShowWindow 函数SW_MAXIMIZE 标志。

在 C# 中,P/Invoke 声明如下所示:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private const int SW_MAXIMIZE = 3;

将其添加到您的代码中:

if (!Ok)
{
   Process[] p = Process.GetProcessesByName(ProductName);
   SetForegroundWindow(p[0].MainWindowHandle);
   ShowWindow(p[0].MainWindowHandle, SW_MAXIMIZE);
}

如果您实际上想测试窗口是否在最大化之前先最小化,您可以使用老式的 IsIconic 函数

[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsIconic(IntPtr hWnd);

// [...]

if (!Ok)
{
   Process[] p = Process.GetProcessesByName(ProductName);
   IntPtr hwndMain= p[0].MainWindowHandle;
   SetForegroundWindow(hwndMain);

   if (IsIconic(hwndMain))
   {
      ShowWindow(hwndMain, SW_MAXIMIZE);
   }
}

如果您只想激活窗口(而不是最大化它),请使用 SW_SHOW 值(5) 而不是 SW_MAXIMIZE。这会将其恢复到最小化之前的先前状态。

You're looking for the ShowWindow function and the SW_MAXIMIZE flag.

In C#, the P/Invoke declaration would look like this:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private const int SW_MAXIMIZE = 3;

Add it to your code here:

if (!Ok)
{
   Process[] p = Process.GetProcessesByName(ProductName);
   SetForegroundWindow(p[0].MainWindowHandle);
   ShowWindow(p[0].MainWindowHandle, SW_MAXIMIZE);
}

If you actually want to test whether the window is minimized first before you maximize it, you can use the old-school IsIconic function:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsIconic(IntPtr hWnd);

// [...]

if (!Ok)
{
   Process[] p = Process.GetProcessesByName(ProductName);
   IntPtr hwndMain= p[0].MainWindowHandle;
   SetForegroundWindow(hwndMain);

   if (IsIconic(hwndMain))
   {
      ShowWindow(hwndMain, SW_MAXIMIZE);
   }
}

If you just want to activate the window (rather than maximize it), use the SW_SHOW value (5) instead of SW_MAXIMIZE. This will restore it to its previous state, before it was minimized.

别再吹冷风 2024-11-14 00:23:50

我想建议一个纯粹的.NET 解决方案(即没有操作系统依赖性)。

Program.cs

static class Program
{
    private static volatile bool _exitProcess;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        bool createdNew;
        var showMeEventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, "MyApp.ShowMe", out createdNew);

        if (createdNew)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form1();
            new Thread(() =>
            {
                while (!_exitProcess)
                {
                    showMeEventHandle.WaitOne(-1);
                    if (!_exitProcess)
                    {
                        if (form.InvokeRequired)
                        {
                            form.BeginInvoke((MethodInvoker)form.BringFormToFront);
                        }
                        else
                        {
                            form.BringFormToFront();
                        }
                    }
                }
            }).Start();

            Application.Run(form);
        }

        _exitProcess = true;
        showMeEventHandle.Set();

        showMeEventHandle.Close();
    }
}

ExtMethods.cs

public static class ExtMethods
{
    public static void BringFormToFront(this Form form)
    {
        form.WindowState = FormWindowState.Normal;
        form.ShowInTaskbar = true;
        form.Show();
        form.Activate();
    }
}

Form1.cs

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.BringFormToFront();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            ShowInTaskbar = false;
            Hide();
        }
    }

I would like to suggest a solution that is purely .NET (i.e. without OS dependency).

Program.cs

static class Program
{
    private static volatile bool _exitProcess;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        bool createdNew;
        var showMeEventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, "MyApp.ShowMe", out createdNew);

        if (createdNew)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form1();
            new Thread(() =>
            {
                while (!_exitProcess)
                {
                    showMeEventHandle.WaitOne(-1);
                    if (!_exitProcess)
                    {
                        if (form.InvokeRequired)
                        {
                            form.BeginInvoke((MethodInvoker)form.BringFormToFront);
                        }
                        else
                        {
                            form.BringFormToFront();
                        }
                    }
                }
            }).Start();

            Application.Run(form);
        }

        _exitProcess = true;
        showMeEventHandle.Set();

        showMeEventHandle.Close();
    }
}

ExtMethods.cs

public static class ExtMethods
{
    public static void BringFormToFront(this Form form)
    {
        form.WindowState = FormWindowState.Normal;
        form.ShowInTaskbar = true;
        form.Show();
        form.Activate();
    }
}

Form1.cs

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.BringFormToFront();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            ShowInTaskbar = false;
            Hide();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文