public override bool Connect() { this.SetEventHandlers(); try { RDP_Client.Connect(); base.Connect(); return(true); } catch (Exception ex) { Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, Language.strRdpConnectionOpenFailed + Constants.vbNewLine + ex.Message); } return(false); }
//private void OpenVirtualChannel() //{ // mHandle = WtsApi32.WTSVirtualChannelOpen(IntPtr.Zero, -1, "TSCS"); // try // { // string testString = "Hello World"; // //MemoryStream ms = new MemoryStream(); // //GZipStream gs = new GZipStream(ms, CompressionMode.Compress, true); // //FileStream fs = File.OpenRead(); // byte[] buffer = new byte[1024]; // //int bytesRead = 0; // //while ((bytesRead = fs.Read(buffer, 0, 1024)) != 0) // //{ // // gs.Write(buffer, 0, bytesRead); // //} // //gs.Close(); // byte[] gziped = System.Text.Encoding.UTF8.GetBytes(testString); // //ms.Position = 0; // //ms.Read(gziped, 0, (int)ms.Length); // int written = 0; // bool ret = WtsApi32.WTSVirtualChannelWrite(mHandle, gziped, gziped.Length, ref written); // if (ret || written == gziped.Length) // MessageBox.Show("Sent!", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information); // else // MessageBox.Show("Bumm! Somethings gone wrong!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // } // catch (Exception ex) // { // MessageBox.Show("Somethings gone wrong:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // } //} #endregion private void ConnectRemoteApp() { rdc.Dock = DockStyle.Fill; this.Controls.Add(rdc); //this.Hide(); //这里是Form1在加载的时候就陈隐藏掉,这样就只看到RemoteAPP了,估计微软也是这样处理的 rdc.RemoteProgram2.RemoteProgramMode = true; rdc.OnConnected += (_1, _2) => { rdc.RemoteProgram2.ServerStartProgram(txtPath.Text.Trim(), "", "%SYSTEMROOT%", true, "", false); }; rdc.Server = txtServer.Text.Trim(); rdc.UserName = string.IsNullOrEmpty(txtDomain.Text.Trim()) ? txtUser.Text.ToString() : txtDomain.Text.ToString() + "\\" + txtUser.Text.ToString(); //注意这里的用户名格式 rdc.AdvancedSettings7.ClearTextPassword = txtPass.Text.Trim(); //用户名密码 //rdc.AdvancedSettings7.EnableCredSspSupport = true; //rdc.AdvancedSettings7.PublicMode = false; rdc.DesktopWidth = SystemInformation.VirtualScreen.Width; rdc.DesktopHeight = SystemInformation.VirtualScreen.Height; rdc.AdvancedSettings7.SmartSizing = true; rdc.AdvancedSettings7.keepAliveInterval = 5000; //rdc.AdvancedSettings7.DisplayConnectionBar = true; //rdc.AdvancedSettings7.CanAutoReconnect = true; rdc.AdvancedSettings7.DisableCtrlAltDel = 1; rdc.AdvancedSettings7.DisplayConnectionBar = true; rdc.AdvancedSettings7.EnableAutoReconnect = cbAutoConnect.Checked?true:false; rdc.AdvancedSettings7.keepAliveInterval = 5000; rdc.CreateVirtualChannels("TSTnls"); //rdc.SendOnVirtualChannel("TSTnls", "Hello"); rdc.FullScreen = true; //禁用ctrl+alt+del,只允许remoteapp,不允许远程桌面登录 rdc.AdvancedSettings7.DisableCtrlAltDel = 1; rdc.AdvancedSettings7.EnableWindowsKey = -1; //rdc.AdvancedSettings3. try { rdc.Connect(); /*rdc.OnConnected += (object sender, EventArgs e) => * { * Thread thread = new Thread(Send); * thread.Start(); * };*/ } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
/// <summary> /// 创建远程桌面连接 /// </summary> /// <param name="args">参数数组 new string[] { ServerIp, UserName, Password }</param> private void CreateAxMsRdpClient(string[] args) { string[] ServerIps = args[0].Split(':'); Form form = new Form(); form.ShowIcon = false; //form.StartPosition = FormStartPosition.Manual; form.Name = ServerIps[0].Replace(".", ""); form.Text = string.Format("{0} ({1})", args[3], ServerIps[0]); form.Size = new Size(1024, 768); Rectangle ScreenArea = Screen.PrimaryScreen.Bounds; AxMsRdpClient7NotSafeForScripting axMsRdpc = new AxMsRdpClient7NotSafeForScripting(); ((System.ComponentModel.ISupportInitialize)(axMsRdpc)).BeginInit(); axMsRdpc.Dock = DockStyle.Fill; axMsRdpc.Enabled = true; axMsRdpc.Name = string.Format("axMsRdpc_{0}", ServerIps[0].Replace(".", "")); // 绑定连接与释放事件 axMsRdpc.OnConnecting += new EventHandler(this.axMsRdpc_OnConnecting); axMsRdpc.OnDisconnected += new IMsTscAxEvents_OnDisconnectedEventHandler(this.axMsRdpc_OnDisconnected); form.Controls.Add(axMsRdpc); form.Show(); ((System.ComponentModel.ISupportInitialize)(axMsRdpc)).EndInit(); // 服务器地址 axMsRdpc.Server = ServerIps[0]; // 远程登录账号 axMsRdpc.UserName = args[1]; // 远程端口号 axMsRdpc.AdvancedSettings7.RDPPort = ServerIps.Length == 1 ? 3389 : Convert.ToInt32(ServerIps[1]); // 自动控制屏幕显示尺寸 //axMsRdpc.AdvancedSettings9.SmartSizing = true; // 启用CredSSP身份验证(有些服务器连接没有反应,需要开启这个) axMsRdpc.AdvancedSettings7.EnableCredSspSupport = true; // 远程登录密码 axMsRdpc.AdvancedSettings7.ClearTextPassword = args[2]; // 颜色位数 8,16,24,32 axMsRdpc.ColorDepth = 32; // 开启全屏 true|flase axMsRdpc.FullScreen = this.isFullScreen; // 设置远程桌面宽度为显示器宽度 axMsRdpc.DesktopWidth = ScreenArea.Width; // 设置远程桌面宽度为显示器高度 axMsRdpc.DesktopHeight = ScreenArea.Height; // 远程连接 axMsRdpc.Connect(); }
private void ConnectCore() { _form = new TestForm(); _form.Show(); _ax = _form.RdpControl; _ax.Server = _context.Server.Name; _ax.Domain = _context.Server.Domain; _ax.UserName = _context.Server.Username; _ax.AdvancedSettings8.ClearTextPassword = _context.Server.Password; _ax.OnConnected += delegate { _connectedEvent.Set(); }; _ax.OnDisconnected += AxOnDisconnected; _ax.Connect(); // You need a message loop or else the RDP client control will never connect. // That's why we run the form on a separate thread. Application.Run(_form); _form.Dispose(); }
private void ConnectCore() { _form = new TestForm(); _form.Show(); _ax = _form.RdpControl; _ax.Server = _context.Server.Name; _ax.Domain = _context.Server.Domain; _ax.UserName = _context.Server.Username; _ax.AdvancedSettings8.ClearTextPassword = _context.Server.Password; _ax.OnConnected += delegate { _connectedEvent.Set(); }; _ax.OnDisconnected += AxOnDisconnected; _ax.Connect(); // You need a message loop or else the RDP client control will never connect. // That's why we run the form on a separate thread. Application.Run(_form); _form.Dispose(); }
private void connectRemoteDesktop(string loginname, string password, string serverIP, int serverPort = 3389, string title = "") { Form form = new Form(); form.ShowIcon = false; form.Name = string.Format("form_rdp_{0}_{1}", serverIP.Replace(".", "_"), DateTime.Now.Second.ToString()); form.Text = title; form.Size = new Size(1024, 768); form.Resize += new System.EventHandler(this.RDPForm_Resize); form.Closing += new CancelEventHandler(this.RDPForm_Closing); Rectangle ScreenArea = Screen.PrimaryScreen.Bounds; AxMsRdpClient7NotSafeForScripting axMsRdpc = new AxMsRdpClient7NotSafeForScripting(); ((System.ComponentModel.ISupportInitialize)(axMsRdpc)).BeginInit(); axMsRdpc.Dock = DockStyle.Fill; axMsRdpc.Enabled = true; axMsRdpc.Name = string.Format("axMsRdpc_{0}_{1}", serverIP.Replace(".", "_"), DateTime.Now.Second.ToString()); // bind rdp connect's events axMsRdpc.OnDisconnected += RDC_Event_OnDisconnected; axMsRdpc.OnLeaveFullScreenMode += RDC_Event_OnLeaveFullScreenMode; axMsRdpc.OnEnterFullScreenMode += RDC_Event_OnEnterFullScreenMode; form.Controls.Add(axMsRdpc); form.Show(); ((System.ComponentModel.ISupportInitialize)(axMsRdpc)).EndInit(); axMsRdpc.Server = serverIP; axMsRdpc.UserName = loginname; axMsRdpc.AdvancedSettings7.RDPPort = Convert.ToInt32(serverPort); //axMsRdpc.AdvancedSettings9.SmartSizing = true; axMsRdpc.AdvancedSettings7.EnableCredSspSupport = true; axMsRdpc.AdvancedSettings7.ClearTextPassword = password; axMsRdpc.ColorDepth = 32; axMsRdpc.FullScreen = true; axMsRdpc.DesktopWidth = ScreenArea.Width; axMsRdpc.DesktopHeight = ScreenArea.Height; axMsRdpc.Connect(); rdp2formMap.Add(axMsRdpc.Name, form); form2rdpMap.Add(form.Name, axMsRdpc); }
public override void Connect(DbItemRemoteLink linkSettings, DbItemSetting lastSetting) { DbItemSettingRdp lastSettingRdp = lastSetting as DbItemSettingRdp; if (lastSettingRdp == null) { return; } //分离服务器地址和端口 string[] addr = linkSettings.Server.Split(':'); int port = 3389; if (addr.Length > 1) { int.TryParse(addr[1], out port); } if (port <= 0) { port = 3389; } //初始化远程连接属性 if (MsRdpClient7 != null) //win7及以下版本 { MsRdpClient7.Server = addr[0]; MsRdpClient7.UserName = linkSettings.UserName; MsRdpClient7.AdvancedSettings2.ClearTextPassword = linkSettings.Password; MsRdpClient7.AdvancedSettings2.RDPPort = port; MsRdpClientOcx = (IMsRdpClientNonScriptable5)MsRdpClient7.GetOcx(); MsRdpClientOcx.PromptForCredentials = false; //凭据提示对话框 MsRdpClientOcx.AllowPromptingForCredentials = true; //显示密码输入框 MsRdpClientOcx.DisableConnectionBar = true; //禁用连接栏 //将窗口句柄设置或检索为控件显示的任何对话框的父窗口 var parentHwnd = CommonServices.HWNDtoRemotableHandle(new WindowInteropHelper(Window.GetWindow(this)).Handle); MsRdpClientOcx.set_UIParentWindowHandle(ref parentHwnd); MsRdpClient7.AdvancedSettings.BitmapPeristence = 1; //启用位图缓存 MsRdpClient7.AdvancedSettings.Compress = 1; //启用压缩 MsRdpClient7.AdvancedSettings.ContainerHandledFullScreen = 1; //启用容器处理的全屏模式。 MsRdpClient7.AdvancedSettings2.BitmapPersistence = 1; //持久位图缓存 MsRdpClient7.AdvancedSettings2.CachePersistenceActive = 1; //持久位图缓存 MsRdpClient7.AdvancedSettings2.GrabFocusOnConnect = false; //连接的时候获取焦点 MsRdpClient7.AdvancedSettings7.EnableCredSspSupport = true; //指定是否为此连接启用凭据安全服务提供程序 //分辨率 if (lastSettingRdp.SizeIndex == DbItemSetting.DESKSIZE_AUTO)//自适应分辨率 { Window windows = Window.GetWindow(this); MsRdpClient7.DesktopWidth = (int)windows.Width - 4; MsRdpClient7.DesktopHeight = (int)windows.Height - 34; } else { var size = lastSettingRdp.GetDeskTopSize(); MsRdpClient7.DesktopWidth = (int)size.Width; MsRdpClient7.DesktopHeight = (int)size.Height; } //性能选项 if (lastSettingRdp.Performance != DbItemSettingRdp.CONNECTION_TYPE_AUTO) { MsRdpClient7.AdvancedSettings8.NetworkConnectionType = (uint)lastSettingRdp.Performance; } //颜色深度 switch (lastSettingRdp.ColorDepthMode) { case DbItemSettingRdp.COLOR_15BPP: MsRdpClient7.ColorDepth = 15; break; case DbItemSettingRdp.COLOR_16BPP: MsRdpClient7.ColorDepth = 16; break; case DbItemSettingRdp.COLOR_24BPP: MsRdpClient7.ColorDepth = 24; break; case DbItemSettingRdp.COLOR_32BPP: MsRdpClient7.ColorDepth = 32; break; default: MsRdpClient7.ColorDepth = 32; break; } //音频 MsRdpClient7.AdvancedSettings6.AudioRedirectionMode = (uint)lastSettingRdp.AudioRedirectionMode - 1; //组合键 MsRdpClient7.SecuredSettings2.KeyboardHookMode = lastSettingRdp.KeyboardHookMode - 1; //本地资源 MsRdpClient7.AdvancedSettings2.RedirectPrinters = lastSettingRdp.RedirectionPrintf.Value ? true : false; //打印机 MsRdpClient7.AdvancedSettings6.RedirectClipboard = lastSettingRdp.RedirectionClipboard.Value ? true : false; //剪贴板重定向 MsRdpClient7.AdvancedSettings3.RedirectSmartCards = lastSettingRdp.RedirectionsMartcard.Value ? true : false; //智能卡重定向 MsRdpClient7.AdvancedSettings3.RedirectPorts = lastSettingRdp.RedirectionsPort.Value ? true : false; //端口重定向 MsRdpClient7.AdvancedSettings3.RedirectDrives = lastSettingRdp.RedirectionsDriver.Value ? true : false; //驱动器重定向 MsRdpClient7.AdvancedSettings4.ConnectionBarShowMinimizeButton = false;//显示全部工具栏上的最小化按钮 MsRdpClient7.AdvancedSettings7.ConnectToAdministerServer = false; MsRdpClient7.Connect(); User32.EnumChildWindows(MsRdpClient7.Handle, EnumWindowsProc, IntPtr.Zero); } else //win8 及以上版本 { MsRdpClient9.Server = addr[0]; MsRdpClient9.UserName = linkSettings.UserName; MsRdpClient9.AdvancedSettings2.ClearTextPassword = linkSettings.Password; MsRdpClient9.AdvancedSettings2.RDPPort = port; MsRdpClientOcx = (IMsRdpClientNonScriptable5)MsRdpClient9.GetOcx(); MsRdpClientOcx.PromptForCredentials = false; //凭据提示对话框 MsRdpClientOcx.AllowPromptingForCredentials = true; //显示密码输入框 MsRdpClientOcx.DisableConnectionBar = true; //禁用连接栏 //将窗口句柄设置或检索为控件显示的任何对话框的父窗口 var parentHwnd = CommonServices.HWNDtoRemotableHandle(new WindowInteropHelper(Window.GetWindow(this)).Handle); MsRdpClientOcx.set_UIParentWindowHandle(ref parentHwnd); MsRdpClient9.AdvancedSettings.BitmapPeristence = 1; //启用位图缓存 MsRdpClient9.AdvancedSettings.Compress = 1; //启用压缩 MsRdpClient9.AdvancedSettings.ContainerHandledFullScreen = 1; //启用容器处理的全屏模式。 MsRdpClient9.AdvancedSettings2.BitmapPersistence = 1; //持久位图缓存 MsRdpClient9.AdvancedSettings2.CachePersistenceActive = 1; //持久位图缓存 MsRdpClient9.AdvancedSettings2.GrabFocusOnConnect = false; //连接的时候获取焦点 MsRdpClient9.AdvancedSettings7.EnableCredSspSupport = true; //指定是否为此连接启用凭据安全服务提供程序 //分辨率 if (lastSettingRdp.SizeIndex == DbItemSetting.DESKSIZE_AUTO)//自适应分辨率 { Window windows = Window.GetWindow(this); MsRdpClient9.DesktopWidth = (int)windows.Width - 4; MsRdpClient9.DesktopHeight = (int)windows.Height - 34; } else { var size = lastSettingRdp.GetDeskTopSize(); MsRdpClient9.DesktopWidth = (int)size.Width; MsRdpClient9.DesktopHeight = (int)size.Height; } //性能选项 if (lastSettingRdp.Performance == DbItemSettingRdp.CONNECTION_TYPE_AUTO) { MsRdpClient9.AdvancedSettings9.BandwidthDetection = true; //自动检查带宽 } else { MsRdpClient9.AdvancedSettings8.NetworkConnectionType = (uint)lastSettingRdp.Performance; } //颜色深度 switch (lastSettingRdp.ColorDepthMode) { case DbItemSettingRdp.COLOR_15BPP: MsRdpClient9.ColorDepth = 15; break; case DbItemSettingRdp.COLOR_16BPP: MsRdpClient9.ColorDepth = 16; break; case DbItemSettingRdp.COLOR_24BPP: MsRdpClient9.ColorDepth = 24; break; case DbItemSettingRdp.COLOR_32BPP: MsRdpClient9.ColorDepth = 32; break; default: MsRdpClient9.ColorDepth = 32; break; } //音频 MsRdpClient9.AdvancedSettings6.AudioRedirectionMode = (uint)lastSettingRdp.AudioRedirectionMode - 1; //录音设备 MsRdpClient9.AdvancedSettings8.AudioCaptureRedirectionMode = lastSettingRdp.AudioCaptureRedirectionMode == DbItemSettingRdp.AAUDIOCAPTURE_TRUE; //组合键 MsRdpClient9.SecuredSettings2.KeyboardHookMode = lastSettingRdp.KeyboardHookMode - 1; //重定向 MsRdpClient9.AdvancedSettings2.RedirectPrinters = lastSettingRdp.RedirectionPrintf.Value ? true : false; //打印机 MsRdpClient9.AdvancedSettings6.RedirectClipboard = lastSettingRdp.RedirectionClipboard.Value ? true : false; //剪贴板重定向 MsRdpClient9.AdvancedSettings3.RedirectSmartCards = lastSettingRdp.RedirectionsMartcard.Value ? true : false; //智能卡重定向 MsRdpClient9.AdvancedSettings3.RedirectPorts = lastSettingRdp.RedirectionsPort.Value ? true : false; //端口重定向 MsRdpClient9.AdvancedSettings3.RedirectDrives = lastSettingRdp.RedirectionsDriver.Value ? true : false; //驱动器重定向 MsRdpClient9.AdvancedSettings4.ConnectionBarShowMinimizeButton = false; //显示全部工具栏上的最小化按钮 MsRdpClient9.Connect(); User32.EnumChildWindows(MsRdpClient9.Handle, EnumWindowsProc, IntPtr.Zero); } }
private void connectRemoteDesktop(string loginname, string password, string serverIP, int serverPort = 3389, string title = "", bool useMultimon = false) { Form form = new Form(); form.ShowIcon = false; form.Name = string.Format("form_rdp_{0}_{1}", serverIP.Replace(".", "_"), DateTime.Now.Second.ToString()); form.Text = title; form.Size = new Size(1024, 768); form.Resize += new System.EventHandler(this.RDPForm_Resize); form.Closing += new CancelEventHandler(this.RDPForm_Closing); Rectangle ScreenArea; if (Screen.AllScreens.Length > 1) { Trace.TraceInformation("Screen Count = {0}", Screen.AllScreens.Length); Screen scr = null; for (int i = 0; i < Screen.AllScreens.Length; i++) { scr = Screen.AllScreens[i]; Trace.TraceInformation("Screen[{0}]:Primary = {1}", i, scr.Primary); if (scr.Primary) { break; } } if (scr != null) { ScreenArea = scr.Bounds; Trace.TraceInformation("set ScreenArea:width={0} , height={1}", ScreenArea.Width, ScreenArea.Height); } else { ScreenArea = Screen.PrimaryScreen.Bounds; } } else { ScreenArea = Screen.PrimaryScreen.Bounds; } AxMsRdpClient7NotSafeForScripting axMsRdpc = new AxMsRdpClient7NotSafeForScripting(); ((System.ComponentModel.ISupportInitialize)(axMsRdpc)).BeginInit(); axMsRdpc.Dock = DockStyle.Fill; axMsRdpc.Enabled = true; axMsRdpc.Name = string.Format("axMsRdpc_{0}_{1}", serverIP.Replace(".", "_"), DateTime.Now.Second.ToString()); // bind rdp connect's events axMsRdpc.OnDisconnected += RDC_Event_OnDisconnected; axMsRdpc.OnLeaveFullScreenMode += RDC_Event_OnLeaveFullScreenMode; axMsRdpc.OnEnterFullScreenMode += RDC_Event_OnEnterFullScreenMode; form.Controls.Add(axMsRdpc); form.Show(); ((System.ComponentModel.ISupportInitialize)(axMsRdpc)).EndInit(); IMsRdpClientNonScriptable5 sc = (IMsRdpClientNonScriptable5)axMsRdpc.GetOcx(); sc.UseMultimon = useMultimon; axMsRdpc.Server = serverIP; axMsRdpc.UserName = loginname; axMsRdpc.AdvancedSettings7.RDPPort = Convert.ToInt32(serverPort); axMsRdpc.AdvancedSettings7.EnableCredSspSupport = true; axMsRdpc.AdvancedSettings7.ClearTextPassword = password; axMsRdpc.AdvancedSettings7.Compress = 1; //axMsRdpc.AdvancedSettings7.SmartSizing = true; //自动缩放图像 axMsRdpc.AdvancedSettings7.ContainerHandledFullScreen = 0; axMsRdpc.ColorDepth = 32; axMsRdpc.FullScreen = true; Trace.TraceInformation("ScreenArea:width={0} , height={1}", ScreenArea.Width, ScreenArea.Height); axMsRdpc.DesktopWidth = ScreenArea.Width; axMsRdpc.DesktopHeight = ScreenArea.Height; axMsRdpc.Connect(); rdp2formMap.Add(axMsRdpc.Name, form); form2rdpMap.Add(form.Name, axMsRdpc); }
/// <summary> /// 创建远程桌面连接 /// </summary> /// <param name="args">参数数组 new string[] { ServerIp, UserName, Password }</param> private void CreateAxMsRdpClient(string[] args) { string[] ServerIps = args[0].Split(':'); Form axMsRdpcForm = new Form(); axMsRdpcForm.ShowIcon = false; //axMsRdpcForm.StartPosition = FormStartPosition.Manual; axMsRdpcForm.Name = string.Format("Form_{0}", ServerIps[0].Replace(".", "")); axMsRdpcForm.Text = string.Format("{0} ({1})", args[3], ServerIps[0]); axMsRdpcForm.Size = new Size(1024, 768); axMsRdpcForm.FormClosed += new FormClosedEventHandler(this.axMsRdpcForm_Closed); //Rectangle ScreenArea = Screen.PrimaryScreen.Bounds; // 给axMsRdpc取个名字 string _axMsRdpcName = string.Format("axMsRdpc_{0}", ServerIps[0].Replace(".", "")); if (axMsRdpcArray.Contains(_axMsRdpcName)) { Global.WinMessage("此远程已经连接,请勿重复连接!"); return; } else { axMsRdpc = new AxMsRdpClient7NotSafeForScripting(); } // 添加到当前缓存 axMsRdpcArray.Add(_axMsRdpcName); ((System.ComponentModel.ISupportInitialize)(axMsRdpc)).BeginInit(); axMsRdpc.Dock = DockStyle.Fill; axMsRdpc.Enabled = true; // 绑定连接与释放事件 axMsRdpc.OnConnecting += new EventHandler(this.axMsRdpc_OnConnecting); axMsRdpc.OnDisconnected += new IMsTscAxEvents_OnDisconnectedEventHandler(this.axMsRdpc_OnDisconnected); axMsRdpcForm.Controls.Add(axMsRdpc); axMsRdpcForm.WindowState = FormWindowState.Maximized; axMsRdpcForm.Show(); ((System.ComponentModel.ISupportInitialize)(axMsRdpc)).EndInit(); // RDP名字 axMsRdpc.Name = _axMsRdpcName; // 服务器地址 axMsRdpc.Server = ServerIps[0]; // 远程登录账号 axMsRdpc.UserName = args[1]; // 远程端口号 axMsRdpc.AdvancedSettings7.RDPPort = ServerIps.Length == 1 ? 3389 : Convert.ToInt32(ServerIps[1]); //axMsRdpc.AdvancedSettings7.ContainerHandledFullScreen = 1; // 自动控制屏幕显示尺寸 //axMsRdpc.AdvancedSettings7.SmartSizing = true; // 启用CredSSP身份验证(有些服务器连接没有反应,需要开启这个) axMsRdpc.AdvancedSettings7.EnableCredSspSupport = true; // 远程登录密码 axMsRdpc.AdvancedSettings7.ClearTextPassword = args[2]; // 禁用公共模式 //axMsRdpc.AdvancedSettings7.PublicMode = false; // 颜色位数 8,16,24,32 axMsRdpc.ColorDepth = 32; // 开启全屏 true|flase axMsRdpc.FullScreen = this.isFullScreen; // 设置远程桌面宽度为显示器宽度 //axMsRdpc.DesktopWidth = ScreenArea.Width; axMsRdpc.DesktopWidth = axMsRdpcForm.ClientRectangle.Width; // 设置远程桌面宽度为显示器高度 //axMsRdpc.DesktopHeight = ScreenArea.Height; axMsRdpc.DesktopHeight = axMsRdpcForm.ClientRectangle.Height; // 远程连接 axMsRdpc.Connect(); }
/// <summary> /// 打开远程桌面窗口连接给定ip的windows主机 /// </summary> public static void OpenRemoteDesktop(RdpInput input) { string serverIp = input.ServerIp; string userName = input.UserName; string password = input.Password; string description = input.Description; Action <string> onDisconnected = input.OnDisconnected; string[] serverIps = serverIp.Split(':'); serverIp = serverIps[0]; int serverPort = serverIps.Length == 1 ? 3389 : Convert.ToInt32(serverIps[1]); string id = serverIp.Replace(".", "_"); string formName = $"form_{id}"; string formText = $"开源矿工远程桌面 - {description} ({serverIp})"; if (_formDicByName.TryGetValue(formName, out Form form)) { form.Show(); if (form.WindowState == FormWindowState.Minimized) { form.WindowState = FormWindowState.Normal; } form.Activate(); } else { form = new Form { ShowIcon = false, Name = formName, Text = formText, Size = new Size(1200, 900), StartPosition = FormStartPosition.CenterScreen, MaximizeBox = false, WindowState = FormWindowState.Normal }; form.FormClosed += (object sender, FormClosedEventArgs e) => { Form frm = (Form)sender; foreach (Control ctrl in frm.Controls) { if (ctrl.GetType().Name == nameof(AxMsRdpClient7NotSafeForScripting)) { _formDicByName.Remove(frm.Name); var axMsRdp = (AxMsRdpClient7NotSafeForScripting)ctrl; if (axMsRdp != null) { if (axMsRdp.Connected != 0) { axMsRdp.Disconnect(); } axMsRdp.Dispose(); } } } }; _formDicByName.Add(formName, form); AxMsRdpClient7NotSafeForScripting axMsRdpc = new AxMsRdpClient7NotSafeForScripting(); ((System.ComponentModel.ISupportInitialize)axMsRdpc).BeginInit(); axMsRdpc.Dock = DockStyle.Fill; axMsRdpc.Enabled = true; axMsRdpc.OnConnecting += (object sender, EventArgs e) => { var axMsRdp = sender as AxMsRdpClient7NotSafeForScripting; axMsRdp.ConnectingText = axMsRdp.GetStatusText(Convert.ToUInt32(axMsRdp.Connected)); axMsRdp.FindForm().WindowState = FormWindowState.Normal; }; axMsRdpc.OnDisconnected += (object sender, IMsTscAxEvents_OnDisconnectedEvent e) => { var axMsRdp = sender as AxMsRdpClient7NotSafeForScripting; string disconnectedText = $"{formText}远程桌面连接已断开!"; axMsRdp.DisconnectedText = disconnectedText; axMsRdp.FindForm().Close(); onDisconnected?.Invoke(disconnectedText); }; form.Controls.Add(axMsRdpc); form.Show(); ((System.ComponentModel.ISupportInitialize)(axMsRdpc)).EndInit(); axMsRdpc.Name = $"axMsRdpc_{id}"; axMsRdpc.Server = serverIp; axMsRdpc.UserName = userName; axMsRdpc.AdvancedSettings7.RDPPort = serverPort; // 启用CredSSP身份验证(有些服务器连接没有反应,需要开启这个) axMsRdpc.AdvancedSettings7.EnableCredSspSupport = true; axMsRdpc.AdvancedSettings7.ClearTextPassword = password; // 禁用公共模式 //axMsRdpc.AdvancedSettings7.PublicMode = false; // 颜色位数 8,16,24,32 axMsRdpc.ColorDepth = 32; axMsRdpc.FullScreen = false; axMsRdpc.DesktopWidth = form.ClientRectangle.Width; axMsRdpc.DesktopHeight = form.ClientRectangle.Height; axMsRdpc.Connect(); } }