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> /// 關閉當前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