[英]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.