如何启动基于控制台的流程并使用 Powershell 应用自定义标题

发布于 2024-09-07 14:01:41 字数 256 浏览 7 评论 0原文

我正在将旧的 cmd 命令转换为 Powershell,当前使用:

START "My Title" Path/To/ConsoleApp.exe

这将按预期启动 ConsoleApp,并将“我的标题”作为窗口标题。它已被替换为 Start-Process,它可以正常工作,但不提供更改标题的机制。

是否有另一种方法可以在不使用 cmd 命令的情况下实现此目的?

I am converting an old cmd command to Powershell, and currently use:

START "My Title" Path/To/ConsoleApp.exe

This works as expected to launch ConsoleApp with My Title as it's window title. This has been replaced with Start-Process which works correctly, but does not provide a mechanism to change the title.

Is there another way to do this without resorting to using the cmd command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风柔一江水 2024-09-14 14:01:41

更改进程主窗口的文本时有一个小怪癖:如果您在启动进程后尝试立即更改文本,则可能会由于许多可能的原因之一而失败(例如,显示的控件的句柄)该文本在函数调用时不存在)。因此,解决方案是在尝试更改文本之前使用 WaitForInputIdle() 方法:

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public static class Win32Api
{
    [DllImport("User32.dll", EntryPoint = "SetWindowText")]
    public static extern int SetWindowText(IntPtr hWnd, string text);
}
"@

$process = Start-Process -FilePath "notepad.exe" -PassThru
$process.WaitForInputIdle()
[Win32Api]::SetWindowText($process.MainWindowHandle, "My Custom Text")

请注意,在您进行自己的更改后,应用程序本身仍然可以更改窗口文本。

There is a small quirk when changing the text of the process' main window: if you try to change the text straight after you have started the process, it may fail due to one of many possible reasons (e.g. the handle to the control which displays the text does not exist at the time of the function call). So the solution is to use the WaitForInputIdle() method before trying to change the text:

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public static class Win32Api
{
    [DllImport("User32.dll", EntryPoint = "SetWindowText")]
    public static extern int SetWindowText(IntPtr hWnd, string text);
}
"@

$process = Start-Process -FilePath "notepad.exe" -PassThru
$process.WaitForInputIdle()
[Win32Api]::SetWindowText($process.MainWindowHandle, "My Custom Text")

Be aware that the application itself can still change the window text after you have made your own change.

山田美奈子 2024-09-14 14:01:41

我用 cmd.exe 尝试过,效果很好。

Add-Type -Type @"
using System;
using System.Runtime.InteropServices;
namespace WT {
   public class Temp {
      [DllImport("user32.dll")]
      public static extern bool SetWindowText(IntPtr hWnd, string lpString); 
   }
}
"@

$cmd = Start-Process cmd -PassThru
[wt.temp]::SetWindowText($cmd.MainWindowHandle, 'some text')

I tried this with cmd.exe and it worked well.

Add-Type -Type @"
using System;
using System.Runtime.InteropServices;
namespace WT {
   public class Temp {
      [DllImport("user32.dll")]
      public static extern bool SetWindowText(IntPtr hWnd, string lpString); 
   }
}
"@

$cmd = Start-Process cmd -PassThru
[wt.temp]::SetWindowText($cmd.MainWindowHandle, 'some text')
梦萦几度 2024-09-14 14:01:41

如果您想使用 powershell 生成一个具有自定义标题的进程,请尝试:

$StartInfo = new-object System.Diagnostics.ProcessStartInfo
$StartInfo.FileName = "$pshome\powershell.exe"
$StartInfo.Arguments = "-NoExit -Command `$Host.UI.RawUI.WindowTitle=`'Your Title Here`'"
[System.Diagnostics.Process]::Start($StartInfo)

注意转义标题字符串的反引号字符,它们至关重要!

If you want to spawn a process with powershell with a custom title try:

$StartInfo = new-object System.Diagnostics.ProcessStartInfo
$StartInfo.FileName = "$pshome\powershell.exe"
$StartInfo.Arguments = "-NoExit -Command `$Host.UI.RawUI.WindowTitle=`'Your Title Here`'"
[System.Diagnostics.Process]::Start($StartInfo)

Note the backtick characters that escape the string for the title, they are vital!

離殇 2024-09-14 14:01:41

$host.UI.RawUI.WindowTitle = "new title"

正如 George 所说,任何人/任何人都可以将其设置回来(例如自定义提示功能)。

$host.UI.RawUI.WindowTitle = "new title"

As already been said by George, anything/anyone can set it back (like custom prompt functions for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文