欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

远程桌面连接工具

程序员文章站 2022-06-30 14:19:43
1、注册控件:regsvr32 C:\Windows\System32\mstscax.dll。 2、添加RDP Control控件到工具箱中。我选择的RDP Client Control (redistributable) - version 9 3、在VS项目中引用C:\Windows\Syst ......

1、注册控件:regsvr32 C:\Windows\System32\mstscax.dll。

2、添加RDP Control控件到工具箱中。我选择的RDP Client Control (redistributable) - version 9

 远程桌面连接工具

3、在VS项目中引用C:\Windows\System32\mstscax.dll

远程桌面连接工具

 4、RdpTabPage类

using System;
using System.Windows.Forms;

namespace RdpClient
{
    public class RdpTabPage : TabPage
    {
        AxMSTSCLib.AxMsRdpClient9 rdpc = null;
        protected override void OnCreateControl()
        {
            rdpc = new AxMSTSCLib.AxMsRdpClient9();
            rdpc.OnDisconnected += new AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEventHandler(rdpc_OnDisconnected);
            this.Controls.Add(rdpc);
            rdpc.Dock = DockStyle.Fill;
            base.OnCreateControl();
        }

        void rdpc_OnDisconnected(object sender, AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e)
        {
            ((TabControl)this.Parent).TabPages.Remove(this);
        }

        public void Disconnect()
        {
            try
            {
                if (rdpc.Connected == 1)
                {
                    rdpc.Disconnect();
                }
            }
            catch (Exception) { }
        }

        private void SetRdpClientProperties(RdpInfo info)
        {
            rdpc.Server = info.MachineName;
            rdpc.UserName = info.UserName;
            rdpc.Domain = "";
            rdpc.AdvancedSettings9.RDPPort = info.RdpPort;
            rdpc.AdvancedSettings9.ClearTextPassword = info.Password;
            rdpc.AdvancedSettings9.RedirectDrives = true;
            rdpc.AdvancedSettings9.RedirectPrinters = true;
            rdpc.AdvancedSettings9.RedirectSmartCards = true;
            rdpc.AdvancedSettings9.ConnectToServerConsole = true;
            rdpc.ColorDepth = 32;
            rdpc.Dock = DockStyle.Fill;
        }

        public void Connect(RdpInfo info)
        {
            SetRdpClientProperties(info);
            rdpc.Connect();
        }
    }
}

5、主窗体

远程桌面连接工具

using System;
using System.Windows.Forms;

namespace RdpClient
{
    public partial class Form_Main : Form
    {
        #region 构造
        public Form_Main()
        {
            InitializeComponent();
        }
        #endregion

        #region 方法
        /// <summary>
        /// 绑定远程桌面列表
        /// </summary>
        private void BindMenuItem()
        {
            tsb_Rdps.DropDownItems.Clear();
            if (Config.RdpInfos == null) return;

            foreach (var info in Config.RdpInfos.Items)
            {
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.ImageScaling = ToolStripItemImageScaling.SizeToFit;
                item.Name = string.Format("MenuItem{0}", info.AliasName);
                item.Text = info.AliasName;
                item.Tag = info;
                tsb_Rdps.DropDownItems.Add(item);
            }
        }

        public void RdpConnect(RdpInfo info)
        {
            RdpTabPage rdpTab = new RdpTabPage();
            rdpTab.Text = info.AliasName;

            foreach (TabPage tp in tab_Rdps.TabPages)
            {
                if (tp.Text == rdpTab.Text)
                {
                    tab_Rdps.SelectTab(tp);
                    return;
                }
            }

            tab_Rdps.TabPages.Add(rdpTab);
            rdpTab.Connect(info);
            tab_Rdps.SelectTab(rdpTab);
        }
        #endregion

        #region 事件
        private void Form_Main_Load(object sender, EventArgs e)
        {
            Config.Load();
            BindMenuItem();
        }
        private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确定退出程序?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                e.Cancel = true;
                return;
            }
        }

        /// <summary>
        /// 连接远程桌面
        /// </summary>
        private void tsb_Rdps_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            RdpInfo item = e.ClickedItem.Tag as RdpInfo;
            if (item == null) return;

            RdpConnect(item);
        }

        /// <summary>
        /// 双击选项卡时关闭相应选项卡
        /// </summary>
        private void tab_Rdps_DoubleClick(object sender, EventArgs e)
        {
            if (tab_Rdps.SelectedIndex < 0) return;

            RdpTabPage rdpTab = (RdpTabPage)tab_Rdps.SelectedTab;
            if (rdpTab != null)
            {
                rdpTab.Disconnect();
            }
        }
        /// <summary>
        /// 关闭当前Tab页的远程桌面
        /// </summary>
        private void tsb_Disconnect_Click(object sender, EventArgs e)
        {
            if (tab_Rdps.SelectedIndex < 0) return;

            RdpTabPage rdpTab = (RdpTabPage)tab_Rdps.SelectedTab;
            if (rdpTab != null)
            {
                rdpTab.Disconnect();
            }
        }

        /// <summary>
        /// 关闭所有Tab页的远程桌面
        /// </summary>
        private void tsb_DisconnectAll_Click(object sender, EventArgs e)
        {
            foreach (TabPage tab in tab_Rdps.TabPages)
            {
                RdpTabPage rdpTab = (RdpTabPage)tab;
                rdpTab.Disconnect();
            }
        }

        /// <summary>
        /// 最大化
        /// </summary>
        private void tsb_Max_Click(object sender, EventArgs e)
        {
            if (tab_Rdps.SelectedIndex < 0) return;

            RdpTabPage rdpTab = (RdpTabPage)tab_Rdps.SelectedTab;
            if (rdpTab != null)
            {
                var rdpc = rdpTab.Controls[0] as AxMSTSCLib.AxMsRdpClient9;
                rdpc.FullScreen = true;
                rdpc.FullScreenTitle = rdpTab.Text;
            }
        }

        private void tsb_Config_Click(object sender, EventArgs e)
        {
            Form_Settings form = new Form_Settings();
            if (form.ShowDialog() == DialogResult.OK)
            {
                BindMenuItem();
            }
        }

        private void tsb_About_Click(object sender, EventArgs e)
        {
            MessageBox.Show("远程桌面", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion
    }
}

6、程序下载地址

https://pan.baidu.com/s/1Fcolic2QAcNANJszscVUjg