从主要功能最大化窗口? [英] Maximize window from the Main function?

查看:86
本文介绍了从主要功能最大化窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了一个互斥体运行一个实例程序,现在我想窗口变成最大化如果当用户重新打开应用程序目前最小化。

下面是code我目前在我的Program.cs文件:

 静态类节目
{
    [的DllImport(user32.dll中)
    公共静态的extern BOOL SetForegroundWindow(IntPtr的的HWND);

    ///<总结>
    ///的主入口点的应用程序。
    ///< /总结>
    [STAThread]
    静态无效的主要()
    {
        布尔OK =真;
        串产品名称= Application.ProductName;
        互斥M =新的互斥(真的,产品名称,出好);
        如果(OK!)
        {
            的System.Diagnostics.Process [] P = System.Diagnostics.Process.GetProcessesByName(产品名称);
            SetForegroundWindow(P [0] .MainWindowHandle);

    }
    其他
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(假);
        Application.Run(新Form1中());

    }
}
 

解决方案

您正在寻找的 的ShowWindow 功能 SW_MAXIMIZE 标记。

在C#中,P / Invoke的声明是这样的:

  [的DllImport(user32.dll中,字符集= CharSet.Auto)
[返回:的MarshalAs(UnmanagedType.Bool)
私人静态外部布尔的ShowWindow(IntPtr的的HWND,INT的nCmdShow);

私人const int的SW_MAXIMIZE = 3;
 

将它添加到您的code在这里:

 如果(OK!)
{
   流程[] P = Process.GetProcessesByName(产品名称);
   SetForegroundWindow(P [0] .MainWindowHandle);
   的ShowWindow(P [0] .MainWindowHandle,SW_MAXIMIZE);
}
 


如果你真的想测试之前,最大限度地发挥它的窗口是否被最小化首先,你可以用老派的 IsIconic 功能

  [的DllImport(user32.dll中,字符集= CharSet.Auto)
[返回:的MarshalAs(UnmanagedType.Bool)
私人静态外部布尔IsIconic(IntPtr的的HWND);

// [...]

如果(OK!)
{
   流程[] P = Process.GetProcessesByName(产品名称);
   IntPtr的hwndMain = P [0] .MainWindowHandle;
   SetForegroundWindow(hwndMain);

   如果(IsIconic(hwndMain))
   {
      的ShowWindow(hwndMain,SW_MAXIMIZE);
   }
}
 

如果您只想激活窗口(而不是最大化),使用 SW_SHOW 值( 5 ),而不是 SW_MAXIMIZE 。这将其恢复到previous状态,它被最小化了。

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

    }
}

解决方案

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.

这篇关于从主要功能最大化窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆