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".
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...
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");
As @Fredrik Mörk thinks, the problem is that there's need to wait for the window to be able to receive messages, before its title can be set. Waiting 100 milliseconds can break the internal message looper of the program and it's just a workaround. To receive a message, the window must have a handle that's used for referencing that window, thus you can simply wait for the window to have a handle, like this:
Process p = Process.Start("notepad.exe");
while (p.MainWindowHandle == IntPtr.Zero)
Application.DoEvents();
SetWindowText(p.MainWindowHandle, "My Notepad");
Using Application.DoEvents()
the program will continue to receive and process system messages, so it won't block (nor crash), although it isn't asynchronous.
You can also think to avoid CPU overhead by replacing the while
statement with a SpinWait.SpinUntil
call (from System.Threading
):
Process p = Process.Start("notepad.exe");
SpinWait.SpinUntil(() => p.MainWindowHandle != IntPtr.Zero);
SetWindowText(p.MainWindowHandle, "My Notepad");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With