[英]How to minimize the remote desktop connection (RDC) window through C#?
下面的代码使我可以通过mstsc.exe与计算机建立远程桌面连接。
string ipAddress = "XXX.XX.XXX.XXX" // IP Address of other machine
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = "mstsc.exe";
proc.StartInfo.Arguments = "/v:" + ipAddress ;
proc.Start();
成功启动RDC窗口(镜像窗口)后,我希望将其最小化。 有什么办法可以通过C#做到这一点吗?
这是我尝试过的,但没有区别:
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
任何帮助都感激不尽。
您可以使用user32.dll
的ShowWindow
函数。 将以下导入添加到您的程序。 您将需要参考using System.Runtime.InteropServices;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
启动RDP所必须具备的功能将按其原样运行,但是随后您将需要获得在远程桌面打开后创建的新的mstsc
进程。 您开始的原始进程在proc.Start()
之后退出。 使用下面的代码将使您获得第一个mstsc
进程。 注意:如果您打开了多个RDP窗口,则应该选择比仅采用第一个更好的选择。
Process process = Process.GetProcessesByName("mstsc").First();
然后使用SW_MINIMIZE = 6
调用ShowWindow
方法,如下所示
ShowWindow(process.MainWindowHandle, SW_MINIMIZE);
完整的解决方案成为:
private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void Main(string[] args) {
string ipAddress = "xxx.xxx.xxx.xxx";
Process proc = new Process();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = "mstsc.exe";
proc.StartInfo.Arguments = "/v:" + ipAddress ;
proc.Start();
// NOTE: add some kind of delay to wait for the new process to be created.
Process process = Process.GetProcessesByName("mstsc").First();
ShowWindow(process.MainWindowHandle, SW_MINIMIZE);
}
注意:来自@Sergio的答案将起作用,但是它将最小化所创建的初始过程。 如果您需要输入凭据,我认为这不是正确的方法。
使用Windows样式,这可行。
string ipAddress = "xxx.xx.xxx.xxx"; // IP Address of other machine
ProcessStartInfo p = new ProcessStartInfo("mstsc.exe");
p.UseShellExecute = true;
p.Arguments = "/v:" + ipAddress;
p.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(p);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.