Reputation: 83
I am a business user of an application that has two separate environments: test and production. It is important that I know which environment I'm using at all times, but the application gives no indication. Window title, layout, and all features are identical, and there is no function in the program to identify the environment, so it's my responsibility to remember which .exe I'm currently using.
I had the thought that I could modify the shortcut or use a command prompt to open the window such that the title clearly says "TEST" or "PRODUCTION".
I attempted the below, but, while it launches the application as expected, there is no change to the window title. (I suspect this only works when launching command prompts)
start "different title" fake.exe
Is there a way to accomplish this? Any ideas would be very much appreciated.
Upvotes: 8
Views: 9384
Reputation: 9
Thanks for the above. It was very handy and I've been using it for 6 months but I had a need to perform this with a process ID instead of just the current window name. As such I've written an alternative version of this (code below).
It can be run from CMD with the following arguments: settext.exe /WindowToRename "Old name" "New Name" or settext.exe /pid ProcessIDHere "New Name"
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
Upvotes: 0
Reputation: 106
You need to make a program to do this.
You need to call the Windows' API. This is how to make a title bar changing program.
Create a file using notepad and call it SetText.bas. Store it on your desktop.
Paste this into it.
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
Then type in a command prompt window.
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%userprofile%\desktop\SetText.exe" "%userprofile%\desktop\settext.bas" /verbose
A program has been created on your desktop called settext.exe. To use
"%userprofile%\desktop\settext" "Untitled - Notepad" "A Renamed Notepad"
Upvotes: 9