您的位置:首页 > 编程语言 > C#

C# Winform应用程序调用Microsoft RDP client control实现远程连接(VS2013)

2015-07-15 09:55 1376 查看
试了好一会,终于成功了!



先说问题:

1、控件已成功添加到工具箱中,但未在活动设计器中启用。

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

VS2013提醒——“COM 引用“MSTSCLib”是 ActiveX 控件“AxMSTSCLib”的互操作程序集,但含有 /link 标志的编译器已将其标为已链接。系统会将此 COM 引用视为引用,并且不会链接该引用。”

                                                                                                        ————暂且不知道这是什么意思,但是程序运行正常!

2、第一次做时,不知道代码写错了还是怎么的,对了,还缺少一个自动生成的app.config(不知道我的第一次怎么没有),点击“链接”按钮没有任何反应。

3、实现步骤:

(1)创建Winform应用程序,拖拉控件,设计窗体:

首先需要添加RDP Control控件:







一定要对该控件进行注册:regsvr32  C:\Windows\System32\mstscax.dll

(2)添加引用:





剩余的Combobox、textBox、btn控件以及背景、图标就不再详细介绍。

(3)剩下的就是编写“链接”按钮的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MSTSCLib;

namespace RDP_2_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnConnect_Click(object sender, EventArgs e)//链接按钮代码
{
if(btnConnect.Text=="链接")
{
//MessageBox.Show("正在准备链接...");
//if()
if(string.IsNullOrEmpty(cbxserver.Text) || string.IsNullOrEmpty(cbxusername.Text) || string.IsNullOrEmpty(txtpassword.Text))
{
MessageBox.Show("请输入一个有效的远程连接信息———主机IP,端口号,用户名,密码!", "登录失败提示",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
}
else
{
rdpClient.Server = cbxserver.Text;
rdpClient.UserName = cbxusername.Text;
rdpClient.AdvancedSettings2.RDPPort = 3389;
rdpClient.AdvancedSettings2.SmartSizing = true;

rdpClient.AdvancedSettings9.NegotiateSecurityLayer = true;

IMsTscNonScriptable secured = (IMsTscNonScriptable)rdpClient.GetOcx();
secured.ClearTextPassword = txtpassword.Text;
rdpClient.AdvancedSettings5.ClearTextPassword = txtpassword.Text;

rdpClient.ColorDepth = 24;

//rdpClient.Width = this.Size.Width;
//rdpClient.Height = this.Size.Height;
//rdpClient.DesktopWidth = this.Size.Width;
//rdpClient.DesktopHeight = this.Size.Height;
//rdpClient.FullScreenTitle = "this is test";

rdpClient.Connect();

btnConnect.Text = "Disconnect";
btnConnect.BackColor = Color.Red;
}
}

else
{
btnConnect.Text = "Connect";
btnConnect.BackColor = Color.PaleGreen;
try
{
rdpClient.Disconnect();
}
catch(Exception)
{

}

rdpClient.Refresh();
}
}

private void btnFullscreen_Click(object sender, EventArgs e)//全屏按钮代码
        {
            rdpClient.FullScreen = !rdpClient.FullScreen;
           
        }
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  RDP C# winform