vb.net 使用 Process.Start 启动某些内容后如何更改窗口标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17847418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:22:57  来源:igfitidea点击:

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

vb.netwinapi

提问by D_Bester

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

使用 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?

不幸的是myProc.MainWindowTitle = "Fancy Notepad"不起作用,因为这是只读的。那么怎么做呢?

采纳答案by User 12345678

You can't change the window title using Process.MainWindowTitlebecause the property is readonly.

您不能使用更改窗口标题,Process.MainWindowTitle因为该属性是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 SetWindowsTextlike this

为了更改窗口标题,您首先需要获取目标窗口的句柄,然后指示操作系统使用 Win32 API 函数更改与该句柄关联的窗口的标题,SetWindowsText如下所示

<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.

您需要等待几毫秒才能更改窗口标题,否则窗口标题不会更改。

回答by nathanchere

You'll need to use Win32API call SetWindowText()

您需要使用 Win32API 调用 SetWindowText()

The VB.Net import:

VB.Net 导入:

<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.

#1:在尝试设置窗口文本之前,您需要留出时间让进程启动。如果在创建窗口之前设置文本,它将看起来什么都不做。最简单的方法是线程休眠任意时间(例如 1 秒)。更好的方法是主动检测窗口何时被创建,但这超出了本问题的范围。

回答by Ferd

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:

由于各种原因,上述所有方法都失败了 - 找不到 HWND,或者在慢速 PC 上睡眠时间不够长。像这样称呼它。它重试直到读回标题:

   <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