我建议将窗口位置设置为始终位于底部。在 C# 中,您可以通过如下所示的类来执行此操作:
public static class SetWindowPosition
{
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
private const UInt32 SWP_NOACTIVATE = 0x0010;
[DllImport("user32.dll")]
private static extern bool LockSetForegroundWindow(uint uLockCode);
private const UInt32 LSFW_LOCK = 1;
public static void ForceWindowToStayOnBottom(Process process)
{
SetWindowPos(
process.MainWindowHandle, // The handle of the browser window
HWND_BOTTOM, // Tells it the position, in this case the bottom
0, 0, 0, 0, // Coordinates for sizing of the window - Will be overriden by NOSIZE
SWP_NOSIZE | // Says to keep the window its current size
SWP_NOMOVE | // Says to keep the window in its current spot
SWP_NOACTIVATE // Activation brings the window to the top, we don't want that
);
// If you don't notice this helping, it can probably be deleted.
// It only deals with other applications and gets automatically undone on user interaction
LockSetForegroundWindow(
LSFW_LOCK); // Locks calls to SetForegroundWindow
}
/// <returns> Returns the parent process of each process by the name of processName </returns>
public static List<Process> GetPrimaryProcesses(string processName) =>
Process.GetProcesses() // Gets a list of every process on computer
.Where(process => process.ProcessName.Contains(processName) // Reduces the list to every process by the name we are looking for
&& process.MainWindowHandle != IntPtr.Zero) // Removes any process without a MainWindow (which amounts to every child process)
.ToList();
}
称为:
SetWindowPosition.ForceWindowToStayOnBottom(
SetWindowPosition.GetPrimaryProcesses("chrome")[0]);
其中[0] 是您要最小化的窗口的索引(通常它们按打开的顺序返回)。
这与您将窗口设置为 AlwaysOnTop 时所做的类似(例如,尝试打开任务管理器,单击选项,然后选择 Always On Top),但相反。您应该可以在任何您喜欢的进程上调用它,包括 Web 驱动程序控制台窗口。
如果您想进一步了解它的工作原理,这两个帖子(1、2)是我用来让它工作的,以及 MSDN 文档here。