Ejemplo n.º 1
0
        public Server(ServerModel serverModel)
        {
            var rdpClient = new AxMsRdpClient9NotSafeForScripting
            {
                Dock   = DockStyle.None,
                Width  = Screen.PrimaryScreen.Bounds.Width,
                Height = Screen.PrimaryScreen.WorkingArea.Height
            };

            ((System.ComponentModel.ISupportInitialize)(rdpClient)).BeginInit();
            this.Controls.Add(rdpClient);
            ((System.ComponentModel.ISupportInitialize)(rdpClient)).EndInit();

            rdpClient.Server = serverModel.Server;

            rdpClient.AdvancedSettings2.RDPPort = serverModel.Port;

            rdpClient.UserName = serverModel.UserName;
            rdpClient.AdvancedSettings2.ClearTextPassword = serverModel.Password;


            if (serverModel.Type == "1") //普通远程桌面模式
            {
                //映射键盘
                rdpClient.SecuredSettings3.KeyboardHookMode = 1;
            }
            else //运行远程程序模式
            {
                rdpClient.RemoteProgram2.RemoteProgramMode = true;

                rdpClient.OnLoginComplete += (o, args) =>
                {
                    rdpClient.RemoteProgram2.ServerStartProgram("123", "", "%SYSTEMROOT%", false, "", false);
                    rdpClient.OnRemoteProgramResult += (o1, args1) =>
                    {
                        if (args1.lError != RemoteProgramResult.remoteAppResultOk)
                        {
                            rdpClient.Dispose();
                        }
                    };
                };
            }

            //偏好设置
            ((IMsRdpClientNonScriptable5)rdpClient.GetOcx()).PromptForCredentials = false;
            rdpClient.AdvancedSettings9.EnableCredSspSupport = true;
            rdpClient.ColorDepth = 16;
            rdpClient.AdvancedSettings9.RedirectDrives = true; //共享本地磁盘
            rdpClient.ConnectingText = $"正在连接";
            //连接远程桌面
            rdpClient.Connect();


            InitializeComponent();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 连接远程桌面
        /// </summary>
        private void ConnectRemoteHost(object sender, EventArgs e)
        {
            var host = (RemoteHost)((MetroTile)sender).Tag;

            #region 1.0 创建页签
            var page = new TabPage($"{host.Name}[{host.FullAddress}]");
            tabMain.TabPages.Add(page);
            page.ContextMenuStrip = menuTabPage;
            tabMain.SelectedTab   = page;
            #endregion

            #region 2.0 创建远程桌面客户端
            var rdpClient = new AxMsRdpClient9NotSafeForScripting
            {
                Dock   = DockStyle.None,
                Width  = page.Width,
                Height = page.Height
            };
            page.Controls.Add(rdpClient);

            rdpClient.Server = host.Address;
            if (host.Port != 3389)
            {
                rdpClient.AdvancedSettings2.RDPPort = host.Port;
            }

            rdpClient.UserName = host.User;
            rdpClient.AdvancedSettings2.ClearTextPassword = host.Pwd;

            #region 远程模式 [桌面/仅程序]
            if (string.IsNullOrEmpty(host.RemoteProgram)) //普通远程桌面模式
            {
                //映射键盘
                rdpClient.SecuredSettings3.KeyboardHookMode = 1;
            }
            else //运行远程程序模式
            {
                rdpClient.RemoteProgram2.RemoteProgramMode = true;
                rdpClient.Width            = Screen.PrimaryScreen.Bounds.Width;
                rdpClient.Height           = Screen.PrimaryScreen.Bounds.Height;
                rdpClient.OnLoginComplete += (o, args) =>
                {
                    rdpClient.RemoteProgram2.ServerStartProgram(host.RemoteProgram, "", "%SYSTEMROOT%", false, "", false);
                    rdpClient.OnRemoteProgramResult += (o1, args1) =>
                    {
                        if (args1.lError != RemoteProgramResult.remoteAppResultOk)
                        {
                            rdpClient.Dispose();
                            MessageBox.Show(args1.lError.ToString(), "打开远程程序失败");
                        }
                    };

                    tabMain.TabPages.Remove(page);
                };
            }
            #endregion

            /* 因为分辨率比例问题,缩放效果并不怎么样
             * rdpClient.Width = Screen.PrimaryScreen.Bounds.Width;
             * rdpClient.Height = Screen.PrimaryScreen.Bounds.Height;
             * rdpClient.AdvancedSettings9.SmartSizing = true;
             */

            //偏好设置
            var clientNonScriptable = (IMsRdpClientNonScriptable5)rdpClient.GetOcx();
            clientNonScriptable.PromptForCredentials         = false;
            rdpClient.AdvancedSettings9.EnableCredSspSupport = true;
            rdpClient.ColorDepth     = 16;
            rdpClient.ConnectingText = $"正在连接[{host.Name}],请稍等... {host.FullAddress}";

            //是否连接会话0 (仅在 Windows Server 2003 中有效)
            rdpClient.AdvancedSettings9.ConnectToAdministerServer = host.Ext.ConnectSession0;

            #region 共享本地磁盘 [可配置]
            //是否共享所有本地磁盘
            rdpClient.AdvancedSettings9.RedirectDrives = host.Ext.ShareAllDisk;

            //共享选中的本地磁盘
            if (!rdpClient.AdvancedSettings9.RedirectDrives)
            {
                var diskList = host.Ext.ShareDiskList;
                if (diskList?.Count > 0)
                {
                    var driveCollection = clientNonScriptable.DriveCollection;
                    for (uint i = 0; i < driveCollection.DriveCount; i++)
                    {
                        var driveByIndex = driveCollection.DriveByIndex[i];
                        var driveName    = driveByIndex.Name.Substring(0, driveByIndex.Name.Length - 1);
                        driveByIndex.RedirectionState = diskList.Contains(driveName);
                    }
                }
            }
            #endregion

            #endregion

            //连接远程桌面
            rdpClient.Connect();
        }
Ejemplo n.º 3
0
        void ConnectRemoteHost(object sender, EventArgs e)
        {
            var host = (RemoteHost)((MetroTile)sender).Tag;

            #region 1.0 创建页签
            var page = new TabPage($"{host.Name}[{host.FullAddress}]");
            tabMain.TabPages.Add(page);
            page.ContextMenuStrip = menuTabPage;
            tabMain.SelectedTab   = page;
            #endregion

            #region 2.0 创建远程桌面客户端
            var rdpClient = new AxMsRdpClient9NotSafeForScripting
            {
                Dock   = DockStyle.None,
                Width  = page.Width,
                Height = page.Height
            };
            page.Controls.Add(rdpClient);

            rdpClient.Server = host.Address;
            if (host.Port != 3389)
            {
                rdpClient.AdvancedSettings2.RDPPort = host.Port;
            }

            rdpClient.UserName = host.User;
            rdpClient.AdvancedSettings2.ClearTextPassword = host.Pwd;

            //进运行远程程序模式
            if (!string.IsNullOrEmpty(host.RemoteProgram))
            {
                rdpClient.RemoteProgram2.RemoteProgramMode = true;
                rdpClient.Width            = Screen.PrimaryScreen.Bounds.Width;
                rdpClient.Height           = Screen.PrimaryScreen.Bounds.Height;
                rdpClient.OnLoginComplete += (o, args) =>
                {
                    rdpClient.RemoteProgram2.ServerStartProgram(host.RemoteProgram, "", "%SYSTEMROOT%", false, "", false);
                    rdpClient.OnRemoteProgramResult += (o1, args1) =>
                    {
                        if (args1.lError != RemoteProgramResult.remoteAppResultOk)
                        {
                            rdpClient.Dispose();
                            MessageBox.Show(args1.lError.ToString(), "打开远程程序失败");
                        }
                    };

                    tabMain.TabPages.Remove(page);
                };
            }

            //rdpClient.RemoteProgram2.RemoteProgramMode = true;
            //rdpClient.OnLoginComplete += (o, args) =>
            //{
            //    rdpClient.RemoteProgram2.ServerStartProgram("cmd", "", "%SYSTEMROOT%", false, "", false);
            //    tabMain.TabPages.Remove(page);
            //};

            /* 因为分辨率比例问题,缩放效果并不怎么样
             * rdpClient.Width = Screen.PrimaryScreen.Bounds.Width;
             * rdpClient.Height = Screen.PrimaryScreen.Bounds.Height;
             * rdpClient.AdvancedSettings9.SmartSizing = true;
             */

            //偏好设置
            ((IMsRdpClientNonScriptable5)rdpClient.GetOcx()).PromptForCredentials = false;
            rdpClient.AdvancedSettings9.EnableCredSspSupport = true;
            rdpClient.ColorDepth     = 16;
            rdpClient.ConnectingText = $"正在连接[{host.Name}],请稍等... {host.FullAddress}";
            #endregion

            //连接远程桌面
            rdpClient.Connect();
        }