我將一個老cmd
命令Powershell的應用自定義標題,和目前使用:如何啓動一個基於控制檯的過程和使用PowerShell
START "My Title" Path/To/ConsoleApp.exe
可正常工作與我的標題推出ConsoleApp它是窗口標題。這已被替換爲正常工作的啓動過程,但未提供更改標題的機制。
是否有另一種方式做到這一點沒有訴諸使用cmd
命令?
我將一個老cmd
命令Powershell的應用自定義標題,和目前使用:如何啓動一個基於控制檯的過程和使用PowerShell
START "My Title" Path/To/ConsoleApp.exe
可正常工作與我的標題推出ConsoleApp它是窗口標題。這已被替換爲正常工作的啓動過程,但未提供更改標題的機制。
是否有另一種方式做到這一點沒有訴諸使用cmd
命令?
有改變工藝主窗口的文本時,小怪癖:如果您嘗試更改文本,你已經開始處理後直,它可能會失敗的許多可能的原因是由於一個(例如在函數調用時不存在顯示文本的控件的句柄)。所以,解決的辦法是試圖改變文本之前使用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'作爲有問題的進程(由OP指定)。 'WaitForInputIdle()'爲非GUI應用程序引發'InvalidOperationException',非GUI應用程序則'MainWindowHandle'爲'0'。 – 2012-08-27 10:17:39
不幸的是,這不適合我。我需要運行一個非gui應用程序,並且出現以下錯誤:調用「WaitForInputIdle」參數爲「0」的異常:「WaitForInputIdle失敗,這可能是因爲進程沒有圖形界面。 – gdelfino 2014-11-18 15:16:30
我用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')
在我的腳本中,「[wt.temp] :: SetWindowText($ cmd.MainWindowHandle,'some text')」有時返回True,sometimas False,但它永遠不會設置我的非GUI應用程序的標題。 – gdelfino 2014-11-18 15:25:28
它現在工作!我真的不知道改變了什麼。也許我只需要重新啓動我的PowerShell。感謝您提供此代碼。 – gdelfino 2014-11-19 08:54:38
這個解決方案並不總是爲我工作。在嘗試設置窗口標題之前,我添加了一個延遲(Start-Sleep -s 5),現在看來工作更可靠。 – gdelfino 2014-11-19 10:01:49
$ host.UI.RawUI.WindowTitle =「新頭銜」
正如前面已經說過喬治,什麼/任何人都可以將其設置回(例如,如自定義提示功能)。
完美!比其他解決方案簡單得多。我不知道爲什麼它沒有任何upvotes ... – 2015-08-19 00:20:39
如果你想生成一個進程使用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)
注意,逃避標題字符串墳墓人物,他們是至關重要的!
是否有任何解決方案爲您工作?對於我的非gui應用程序,它不起作用,因爲WaitForInputIdle無法正常工作(請參閱下面的答案評論中的錯誤)。 – gdelfino 2014-11-18 16:55:06