C#远程桌面连接工具

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

 

转载于:https://www.cnblogs.com/zhangwc/p/9110589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值