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

问题描述 投票:0回答:2

我是一个应用程序的业务用户,该应用程序具有两个独立的环境:测试和生产。重要的是我知道我一直在使用哪个环境,但应用程序没有给出任何指示。窗口标题、布局和所有功能都是相同的,程序中没有识别环境的功能,所以我有责任记住我当前正在使用哪个 .exe。

我想我可以修改快捷方式或使用命令提示符打开窗口,使标题清楚地显示“测试”或“生产”。

我尝试了以下操作,但是,虽然它按预期启动应用程序,但窗口标题没有更改。 (我怀疑这只在启动命令提示符时才有效)

start "different title" fake.exe

有办法做到这一点吗?任何想法将非常感激。

cmd titlebar
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"

0
投票

感谢以上。它非常方便,我已经使用它 6 个月了,但我需要使用进程 ID 而不仅仅是当前窗口名称来执行此操作。因此,我写了一个替代版本(代码如下)。

它可以使用以下参数从 CMD 运行: settext.exe /WindowToRename "旧名称" "新名称" 或者 settext.exe /pid ProcessID 这里“新名称”

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
© www.soinside.com 2019 - 2024. All rights reserved.