从命令提示符启动应用程序时更改应用程序标题

7
我是一个应用程序的商业用户,该应用程序有两个不同的环境:测试和生产。重要的是我始终知道我正在使用哪个环境,但是应用程序没有任何指示。窗口标题、布局和所有功能都是相同的,程序中也没有识别环境的功能,因此我需要记住我当前正在使用哪个 .exe 文件。
我想到了修改快捷方式或使用命令提示符打开窗口,以便标题清楚地显示“TEST”或“PRODUCTION”。
我尝试了下面的方法,虽然它按预期启动了应用程序,但窗口标题没有改变。(我怀疑这只在启动命令提示符时有效)
start "different title" fake.exe

有没有一种方法可以实现这个?任何想法都将不胜感激。

2个回答

8

你需要编写一个程序来完成这个任务。

你需要调用Windows的API。以下是制作标题栏更改程序的方法。

使用记事本创建一个文件,命名为SetText.bas,并将其存储在桌面上。

将以下内容粘贴到文件中。

Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication  

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long

Sub Main()
    On Error Resume Next
    Dim CmdLine As String
    Dim Ret as Long
    Dim A() as String
    Dim hwindows as long

    CmdLine = Command()
    If Left(CmdLine, 2) = "/?" Then
        MsgBox("Usage:" & vbCrLf & vbCrLf & "ChangeTitleBar Oldname NewName")
    Else
        A = Split(CmdLine, Chr(34), -1, vbBinaryCompare)
        hwindows = FindWindow(vbNullString, A(1))
        Ret = SetWindowText(hwindows, A(3))

    End If
End Sub
End Module

然后在命令提示符窗口中键入。
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%userprofile%\desktop\SetText.exe" "%userprofile%\desktop\settext.bas" /verbose

您的桌面上创建了一个名为settext.exe的程序。使用方法如下:
"%userprofile%\desktop\settext" "Untitled - Notepad" "A Renamed Notepad"

2
使用说明:1)程序打开后必须运行最终命令。2)这是一次性的标题更改。如果程序自己更改了标题,则您设置的标题将无关紧要。(以记事本为例,如果您将其重命名然后开始输入,它将变成“* 无标题 - 记事本”。 - Tim
太棒了!不是代码,而是帮助那些可能没有任何编程经验或不了解Visual Studio的人的方式!非常酷! - marsh-wiggle
@Ndlr:非常感谢您提供的精彩答案,但不幸的是它无法处理动态更改窗口标题的情况,例如在Notepad++中,窗口名称可能为“**new 12 - Notepadd++”。如果我只想将标题设置为“MyNotepad++”而不进行动态更改,是否有解决方案? - Vishal Zanzrukia

0
感谢上述内容。这非常方便,我已经使用了6个月,但我需要使用进程ID而不仅仅是当前窗口名称来执行此操作。因此,我编写了这个替代版本(以下是代码)。
可以使用以下参数从CMD运行: settext.exe /WindowToRename "旧名称" "新名称" 或者 settext.exe /pid 进程ID "新名称"
Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Diagnostics
Imports System.Collections.Generic

Public Module MyApplication

    Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean

    Sub Main()
        On Error Resume Next
        Dim CmdLine As String
        Dim Args() As String

        CmdLine = Command()
        Args = ParseCommandLine(CmdLine)

        If Args.Length >= 3 Then
            If Args(0).ToLower() = "/pid" Then
                Dim targetPid As Integer
                If Integer.TryParse(Args(1), targetPid) Then
                    ChangeTitleByPid(targetPid, Args(2))
                Else
                    Console.WriteLine("Invalid process ID.")
                End If
            ElseIf Args(0).ToLower() = "/windowtorename" Then
                Dim oldName As String = Args(1)
                Dim newName As String = Args(2)
                ChangeTitleByOldName(oldName, newName)
            Else
                ShowUsage()
            End If
        Else
            ShowUsage()
        End If
    End Sub

    Private Sub ShowUsage()
        MsgBox("Usage:" & vbCrLf & vbCrLf & "settext.exe /WindowToRename ""Old name"" ""New Name""" & vbCrLf & vbCrLf & "settext.exe /PID ProcessID ""New Name""")
        Console.WriteLine("Usage:")
        Console.WriteLine("To change the title by process ID: ChangeTitle /pid [Process ID] [New Title]")
        Console.WriteLine("To change the title by old name: ChangeTitle /oldname [Old Name] [New Name]")
    End Sub

    Private Sub ChangeTitleByPid(pid As Integer, newTitle As String)
        Dim processes() As Process = Process.GetProcesses()
        For Each proc As Process In processes
            If proc.Id = pid Then
                SetWindowText(proc.MainWindowHandle, newTitle)
                Exit For
            End If
        Next
    End Sub

    Private Sub ChangeTitleByOldName(oldName As String, newTitle As String)
        Dim processes() As Process = Process.GetProcesses()
        For Each proc As Process In processes
            If proc.MainWindowTitle = oldName Then
                SetWindowText(proc.MainWindowHandle, newTitle)
                Exit For
            End If
        Next
    End Sub

    Private Function ParseCommandLine(commandLine As String) As String()
        Dim args As New List(Of String)
        Dim inQuotes = False
        Dim currentArg As String = ""
        For Each c As Char In commandLine
            If c = """"c Then
                inQuotes = Not inQuotes
            ElseIf c = " "c AndAlso Not inQuotes Then
                args.Add(currentArg)
                currentArg = ""
            Else
                currentArg &= c
            End If
        Next
        args.Add(currentArg)
        Return args.ToArray()
    End Function
End Module

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接