C # - How can I rename the process window that I started?

Can I rename the window title of the application that I started? That is, if I ran Notepad.exe, I could rename its title bar from "Untitled - Notepad" to "New notepad name."

+5
source share
5 answers

You can do this 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 that you immediately call SetWindowText immediately after starting the process, the name will not change. Perhaps the message ends too soon in the Notepad message queue, so the notepad will set the header again.

Also note that this is a very short change; if the user selects File → New (or does something else that causes Notepad to update the window title), the original name will be returned ...

+14
source

Actually, I sorted it myself, and it works great. Anyway, thanks.

 [DllImport("user32.dll")] static extern SetWindowText(IntPtr hWnd, string windowName); IntPtr handle = p.MainWindowHandle; SetWindowText(handle, "This is my new title"); 
+4
source

You cannot do this in C #, but you can do it using the low level API. Add a thread to the process, call SetWindowText () from it

+4
source

According to @Fredrik Mörk, the problem is that you have to wait until the window can receive messages to set its title. Waiting for 100 milliseconds can break the internal loop of program messages, and this is just a workaround. To receive a message, the window must have a handle that was used to access this window, so you can just wait for the window handle, which should be IntPtr.Zero (empty handle) at startup.

Here is an example of this approach:

 Process p = Process.Start("notepad.exe"); while (p.MainWindowHandle == IntPtr.Zero) Application.DoEvents(); SetWindowText(p.MainWindowHandle, "My Notepad"); 

Using Application.DoEvents() program will continue to receive and process system messages, so it will not block (or crash), although it will not be asynchronous.

You can also think to avoid CPU load by replacing the while expression with the SpinWait.SpinUntil call (so you need to import System.Threading ):

 Process p = Process.Start("notepad.exe"); SpinWait.SpinUntil(delegate { return p.MainWindowHandle != IntPtr.Zero; }); SetWindowText(p.MainWindowHandle, "My Notepad"); 
+3
source

No.

This will require the target application to allow the window title to be altered altogether. Many programs use their headers to display useful information (for example, the name of a file open for editing in Notepad, or <TITLE> an HTML document open in Firefox).

In one case, which I know about, this allows the user to set the header text with slight restrictions - CMD.EXE works in the console window. CMD supports the built-in TITLE command, which sets the window title based on its arguments. But this cannot be done by a second window without pasting key strokes into a specific console window, which is usually not recommended.

Edit:

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

This API function does indeed change the title bar of a top-level window. In fact, this is a call that an application, such as Notepad, is likely to use to set its own title anytime it thinks the name has changed.

The reason I argue that this is not because Notepad really changes the title whenever it wants. An application that supports arbitrary changes to its name must have some mechanism to remember that the title has been changed and not arbitrarily restore its preferred title bar.

-2
source

All Articles