Reputation: 5911
How do I change the Window Title after starting something with Process.Start?
Dim myProc as Process
myProc = myProc.Start("NotePad.exe")
Unfortunately myProc.MainWindowTitle = "Fancy Notepad"
doesn't work as this is read only. So how can it be done?
Upvotes: 3
Views: 10635
Reputation: 216
All the above fail for various reasons - no HWND can be found, or on slow PC's the sleep is not long enough. Call it like this. It retries until is reads back the title:
<DllImport("user32.dll")>
Shared Function SetWindowText(ByVal hwnd As IntPtr, ByVal windowName As String) As Boolean
End Function
SetWindowTextCall(SomeProcess.MainWindowHandle, "Name of Windows")
''' SetWindowTextCall is here to wrap the SetWindowtext API call. This call fails when there is no
''' hwnd as Windows takes its sweet time to get that. It has a counter to make sure we do not get stuck
''' </summary>
''' <param name="hwnd">Handle to the window to change the text on</param>
''' <param name="windowName">the name of the Window </param>
'''
Public Function SetWindowTextCall(hwnd As IntPtr, windowName As String) As Boolean
Dim status As Boolean = False
Dim WindowCounter As Integer = 0
While Not status
Try
Thread.Sleep(100)
status = SetWindowText(hwnd, windowName)
Catch ' can fail to be a valid window handle
Return False
End Try
WindowCounter = WindowCounter + 1
If WindowCounter > 200 Then ' 20 seconds
status = True
End If
End While
Return True
End Function
Upvotes: 0
Reputation: 7804
You can't change the window title using Process.MainWindowTitle
because the property is readonly.
In order to change the window title you will firstly need to obtain a handle to target window and then instruct the Operating System to change the title of the window associated with that handle using the Win32 API function SetWindowsText
like this
<DllImport("user32.dll")> _
Shared Function SetWindowText(ByVal hwnd As IntPtr, ByVal windowName As String) As Boolean
End Function
Once you have defined the function above you can proceed to manipulate the window title using the following code:
Dim process As New Process()
process.StartInfo.FileName = "notepad.exe"
process.Start()
Thread.Sleep(100)
SetWindowText(process.MainWindowHandle, "Fancy Notepad")
You need to wait a short few milliseconds before changing the window title otherwise the window title will not change.
Upvotes: 2
Reputation: 8098
You'll need to use Win32API call SetWindowText()
The VB.Net import:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SetWindowText(ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean
End Function
Usage example:
myProc.Start("notepad.exe")
'Note #1
SetWindowText(myProc.MainWindowHandle, "h4x3d title")
#1: you'll need to allow time for the process to start before trying to set the window text. If you set the text before the window is created, it will appear to do nothing. The easiest way is to thread sleep an arbitrary amount of time (eg 1 second). A better way would be to actively detect when the window has been created, but that's outside the scope of this question.
Upvotes: 1