views:

2177

answers:

4

Is there any way I can rename the window titlebar of an application that I've launched? I.e. if I launched Notepad.exe, I could rename its title bar from "Untitled - Notepad" to "New Notepad Name".

A: 

No.

This would require that the target application allow the window title to be modified at all. Many programs use their titles to show useful information (such as the name of the file open for editing in Notepad, or the <TITLE> of the HTML document open in Firefox).

The one case I am aware of that lets a user set the title text with few restrictions is CMD.EXE running in a console window. CMD supports the TITLE built-in command that sets the window title based on its arguments. But that can't be done by a second window without injecting key strokes into the particular console window, which is generally not recommended.

Edit:

Since the idea is floating that SetWindowText() will do this for you, let me clarify.

That API function does indeed change the title bar of a top level window. It is, in fact, the call that an application like Notepad is likely using to set its own title any time it thinks that the title has changed.

The reason I claim that this is not a solution is that Notepad does indeed change the title when it wants to. An application that supported arbitrary changes to its title would have a mechanism of some kind to remember that the title was changed and not arbitrarily restore its preferred title string.

RBerteig
SetWindowText IS a solution to the problem. It performs exactly what I asked for, therefore there's no denying it's a solution to the problem. I didn't actually ask for it to be permanent anyway, but like I've posted in other comments, there's an easy workaround to keep it permanent anyway.
GenericTypeTea
It is not a general solution. You asked for a general solution, and used an application that maintains information in the title as an example.
RBerteig
+2  A: 

Actually, I sorted it myself and it works perfectly. Thanks anyway.

[DllImport("user32.dll")]
static extern SetWindowText(IntPtr hWnd, string windowName);

IntPtr handle = p.MainWindowHandle;
SetWindowText(handle, "This is my new title");
GenericTypeTea
Don't expect your title to survive past the arbitrary event in the other application that causes it to update its title. Notepad will do that when the user opens a different document, chooses File|New, or File|Save As for instance.
RBerteig
I just created an event to handle such a possibility, so as soon as the text changes, it's set back again.
GenericTypeTea
+1  A: 

You can do it using P/Invoke:

[DllImport("user32.dll")]
static extern int SetWindowText(IntPtr hWnd, string text);



private void StartMyNotepad()
{
    Process p = Process.Start("notepad.exe");
    Thread.Sleep(100);  // <-- ugly hack
    SetWindowText(p.MainWindowHandle, "My Notepad");
}

The background of the ugly hack in the code sample is that it seems as if you call SetWindowText immediately after starting the process, the title will not change. Perhaps the message ends up too early in the message queue of Notepad, so that notepad will set the title again afterwards.

Also note that this is a very brief change; if the user selects File -> New (or does anything else that will cause Notepad to update the window title), the original title will be back...

Fredrik Mörk
Like I said in my answer and a comment on the question, Notepad (and most other apps) don't support this. Forcing the title to change with SetWindowText() is not a real solution because the App will just put it back at some moment of its own choosing.
RBerteig
It is a solution to my problem as the app I'm using wont ever set it back. I only used notepad as an example. Plus if you really wanted to, it's quite simple to create your own event to handle the window handle's title change and then reset it again.
GenericTypeTea
Just to note on the 'ugly hack', I didn't require that as I already had control of the process and know that it's running with it's titlebar visible.
GenericTypeTea
I would argue that changing another application's window title is a Really Bad Idea if it wasn't designed to support it through a documented API of some form.
RBerteig
MS recommends calling WaitForInputIdle where you have the Sleep call. http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx This takes it from "Hack" to "Recommended Practice" (which I've found necessary some of the time)
Brad Bruce
+3  A: 

You can't do it in C#, but you can do it using low level API. Inject a thread into the process, call SetWindowText() from it

J-16 SDiZ
Any app that uses the title to convey information will overwrite that text with its own at some point, as Notepad does.
RBerteig