Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

vb.net - How do I change the Window Title after starting something with Process.Start?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...