C#-如何重命名我启动的进程窗口? [英] C# - How can I rename a process window that I started?

查看:57
本文介绍了C#-如何重命名我启动的进程窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以重命名已启动的应用程序的窗口标题栏?IE.如果启动了Notepad.exe,则可以将其标题栏从无标题-记事本"重命名为新记事本名称".

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".

推荐答案

您可以使用P/Invoke来实现:

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");
}

代码示例中难看的骇客的背景是,似乎您在启动过程后立即调用SetWindowText,标题不会更改.消息可能在记事本的消息队列中结束得太早,因此记事本将在此后再次设置标题.

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...

这篇关于C#-如何重命名我启动的进程窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆