使用 Process.Start 启动某些内容后,如何更改窗口标题? [英] How do I change the Window Title after starting something with Process.Start?
问题描述
如何在使用 Process.Start 启动某些内容后更改窗口标题?
How do I change the Window Title after starting something with Process.Start?
Dim myProc as Process
myProc = myProc.Start("NotePad.exe")
不幸的是 myProc.MainWindowTitle = "Fancy Notepad"
不能工作,因为它是只读的.那怎么办呢?
Unfortunately myProc.MainWindowTitle = "Fancy Notepad"
doesn't work as this is read only. So how can it be done?
推荐答案
您不能使用 Process.MainWindowTitle
更改窗口标题,因为该属性是只读.
You can't change the window title using Process.MainWindowTitle
because the property is readonly.
为了更改窗口标题,您首先需要获取目标窗口的句柄,然后指示操作系统使用 Win32 API 函数更改与该句柄关联的窗口的标题 SetWindowsText
像这样
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.
这篇关于使用 Process.Start 启动某些内容后,如何更改窗口标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!