【问题标题】:How can I set the window text of an application using .NET Process.Start()?如何使用 .NET Process.Start() 设置应用程序的窗口文本?
【发布时间】:2013-04-06 00:24:45
【问题描述】:

以前,我使用.bat 文件来运行一些控制台应用程序.exe。当我这样做时,我可以设置进程窗口标题文本。

例如:-

START "1 - 203.46.105.23:20600 - Sydney 24/7 #1" "C:\MyApplication\Streams\PBUcon\pbucon.exe" ini="C:\MyApplication\Streams\Active\1-203.46.105.23.20600.ini"

所以这会执行文件 pbucon.exe 并将一些参数传递给 exe。控制台窗口标题为1 - 203.46.105.23:20600 - Sydney 24/7 #1

我不确定如何使用 Process 命令以编程方式执行此操作?

这就是我正在做的......

var processStartInfo = new ProcessStartInfo
    {
        Arguments = string.Format("ini={0}", GameServerFile(gameServer, false)),
        FileName = newPbUconFile,
        WorkingDirectory = ActiveFolder
    };

Process.Start(processStartInfo);

有可能吗?

为了它的价值,我还在运行一个控制台应用程序,它会启动那些 pbucon.exe(在需要时)......并做很多其他事情。

【问题讨论】:

  • 可以从C#调用批处理文件...
  • 我知道 - 但我很想知道如果没有它是否可以做到。
  • Process / ProcessStartInfo 类中没有任何东西可以让你这样做,所以我怀疑你必须 p/Invoke 来实现这一点。
  • 任何线索如何通过 p/Invoke 执行此操作?

标签: c# .net console-application


【解决方案1】:

代码中的某处:

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

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

public void startProcess(string path, string title) {
   Process.Start(path);
   Thread.Sleep(1000); //Wait, the new programm must be full loaded
   IntPtr handle = FindWindow("ConsoleWindowClass", path); //get the Handle of the 
                                                           //console window
   SetWindowText(handle, title); //sets the caption
}

Pure Krome 更新

进程已经拥有该信息,而不是寻找句柄...所以我这样做了(因为我正在启动的程序是一个控制台应用程序,如果这与此有关...)

..... snipped .....
var process = Process.Start(path);
Thread.Sleep(1000); // Wait for the new program to start.
SetWindowText(process.MainWindowHandle, title);

HTH。

【讨论】:

猜你喜欢
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
  • 2017-04-21
  • 2010-09-11
相关资源
最近更新 更多