コンソールベースのプロセスを開始し、PowerShellを使用してカスタムタイトルを適用する方法
-
01-10-2019 - |
質問
私は古いものを変えています cmd
PowerShellへのコマンド、そして現在使用しています:
START "My Title" Path/To/ConsoleApp.exe
これは、ウィンドウのタイトルとして私のタイトルでコンソレップを立ち上げることになったとおりに機能します。これは、正しく機能するStart-Processに置き換えられていますが、タイトルを変更するメカニズムを提供しません。
これを行う別の方法はありますか それなし 使用に頼る cmd
指図?
解決
プロセスのテキストを変更する際に小さな癖があります。メインウィンドウのテキスト:プロセスを開始した直後にテキストを変更しようとすると、多くの考えられる理由の1つが失敗する可能性があります(例えば、表示するコントロールのハンドルが表示されます。テキストは、関数呼び出しの時点では存在しません)。したがって、解決策はを使用することです 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")
あなたがあなた自身の変更を加えた後、アプリケーション自体がまだウィンドウテキストを変更できることに注意してください。
他のヒント
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')
$ host.ui.rawui.windowtitle = "new title"
ジョージがすでに言ったように、何でも/誰でもそれを後退させることができます(たとえば、カスタムプロンプト関数など)。
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)
タイトルの文字列を逃れる墓のキャラクターに注意してください、それらは不可欠です!
所属していません StackOverflow