繁体   English   中英

在 PowerShell 中设置控制台 Top-Most

[英]Set console Top-Most in PowerShell

因此,虽然有很多关于如何将forms设置为最顶层的建议,但我找不到任何能让我的控制台运行在最顶层的东西。

所以我的问题是:如何让我的控制台在脚本中运行在最上面?

这需要一些 .NET 互操作,如本博客所述:

来自 TechEd 2012 的脚本…第 1 部分(将 PowerShell Window 放在顶部)

我已经复制了下面的相关代码,以防链接网站消失:

$signature = @'
[DllImport("user32.dll")]
public static extern bool SetWindowPos(
    IntPtr hWnd,
    IntPtr hWndInsertAfter,
    int X,
    int Y,
    int cx,
    int cy,
    uint uFlags);
'@

$type = Add-Type -MemberDefinition $signature -Name SetWindowPosition -Namespace SetWindowPos -Using System.Text -PassThru

$handle = (Get-Process -id $Global:PID).MainWindowHandle
$alwaysOnTop = New-Object -TypeName System.IntPtr -ArgumentList (-1)
$type::SetWindowPos($handle, $alwaysOnTop, 0, 0, 0, 0, 0x0003)

编辑:

如评论中所述:如果您来自批处理文件,则 PowerShell 在子进程中运行并且不拥有控制台 window,因此您必须进行更改:

$signature = @'
[DllImport("kernel32.dll")] public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool SetWindowPos(
    IntPtr hWnd,
    IntPtr hWndInsertAfter,
    int X,
    int Y,
    int cx,
    int cy,
    uint uFlags);
'@

$type = Add-Type -MemberDefinition $signature -Name SetWindowPosition -Namespace SetWindowPos -Using System.Text -PassThru

$handle = $type::GetConsoleWindow()
$type::SetWindowPos($handle, -1, 0, 0, 0, 0, 0x0003)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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