在PowerShell中将控制台设置为最高 [英] Set console Top-Most in PowerShell
本文介绍了在PowerShell中将控制台设置为最高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,尽管有很多关于如何设置表单的建议,但我找不到使控制台运行在最高位置的任何内容.
So while there is much advise about how to set forms topmost, i couldnt find anything that makes my console run topmost.
所以我的问题是:如何在脚本执行过程中使控制台运行在最顶层?
So my question: How do I make my console run top-most during a script?
推荐答案
这需要一些.NET互操作,如本博客中所述:
This requires some .NET interop, as detailed in this blog:
I've copied the relevant code below in case the linked site disappears:
$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将在子进程中运行,并且不拥有控制台窗口,因此您必须进行更改:
As described in the comments: If you're from a batch file, PowerShell runs in a child process and doesn't own the console window, so you'll have to make changes:
$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)
这篇关于在PowerShell中将控制台设置为最高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文