【发布时间】:2020-07-31 00:51:56
【问题描述】:
我正在尝试在 C# 中创建一个 Windows 窗体,我将在其中按下一个按钮,然后我最小化的 Firefox 选项卡将最大化或至少打开。我尝试的一切只是创建一个新的 Firefox 窗口,而不是打开我现有的窗口。我不知道该怎么做。我试过 ShowWindowAsync 但我不是很明白。
【问题讨论】:
我正在尝试在 C# 中创建一个 Windows 窗体,我将在其中按下一个按钮,然后我最小化的 Firefox 选项卡将最大化或至少打开。我尝试的一切只是创建一个新的 Firefox 窗口,而不是打开我现有的窗口。我不知道该怎么做。我试过 ShowWindowAsync 但我不是很明白。
【问题讨论】:
这是我的工作控制台应用程序:
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
}
有关 ShowWindow 的更多信息,请查看here。
编辑:事实证明,最新版本的 Firefox 在后台运行多个进程:
var firefoxProcesses = Process.GetProcessesByName("firefox");
foreach (var firefox in firefoxProcesses)
{
SetProcessWindowState(firefox, WindowState.ShowNormal);
}
【讨论】: