简体   繁体   中英

How to maximize/ minimize currently opened Firefox window through C#

I'm trying to create a windows form in C#, where I would press a button and then my minimized Firefox tab would maximize or at least open up. Everything I tried just makes a new Firefox window instead of opening the current one I have. I don't know how to do this. I tried ShowWindowAsync but I didn't really understand it.

This is my working Console application:

using System;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    static void Main()
    {
        var firefox = Process.GetProcessesByName("firefox").FirstOrDefault();
        if (firefox != null)
        {
            SetProcessWindowState(firefox, WindowState.ShowNormal);
        }
    }

    static void SetProcessWindowState(Process process, WindowState windowState)
    {
        ShowWindow(process.MainWindowHandle, (int)windowState);
    }
}

enum WindowState
{
    Maximize = 3, Minimize = 6, ShowNormal = 1
}

For more information about ShowWindow look here .

EDIT: it turns out that the newest version of Firefox runs multiple processes in the background:

var firefoxProcesses = Process.GetProcessesByName("firefox");
foreach (var firefox in firefoxProcesses)
{
    SetProcessWindowState(firefox, WindowState.ShowNormal);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM