using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AxMSTSCLib;
namespace WinRemoteDesktop
{
public partial class SessionPanel : UserControl
{
private AxMsRdpClient7NotSafeForScripting axMsRdpc = null;
private bool m_bLongConnect = true; //长连接
public string Index;
public bool LongConnect { get => m_bLongConnect; set => m_bLongConnect = value; }
public SessionPanel()
{
InitializeComponent();
}
public void CreateSession(string ServerIps, string Admin, string Password)
{
if (axMsRdpc != null)
{
if (axMsRdpc.Connected!=0)
{
axMsRdpc.Disconnect();
}
axMsRdpc.Dispose();
}
axMsRdpc = new AxMsRdpClient7NotSafeForScripting();
((System.ComponentModel.ISupportInitialize)(axMsRdpc)).BeginInit();
axMsRdpc.Dock = DockStyle.Fill;
axMsRdpc.Enabled = true;
// 绑定连接与释放事件
axMsRdpc.OnConnecting += new EventHandler(this.axMsRdpc_OnConnecting);
axMsRdpc.OnDisconnected += new IMsTscAxEvents_OnDisconnectedEventHandler(this.axMsRdpc_OnDisconnected);
this.Controls.Add(axMsRdpc);
//axMsRdpcForm.WindowState = FormWindowState.Maximized;
//axMsRdpcForm.Show();
((System.ComponentModel.ISupportInitialize)(axMsRdpc)).EndInit();
// RDP名字
axMsRdpc.Name = ServerIps;
// 服务器地址
axMsRdpc.Server = ServerIps;
// 远程登录账号
axMsRdpc.UserName = Admin;
// 远程端口号
axMsRdpc.AdvancedSettings7.RDPPort = 3389;
//axMsRdpc.AdvancedSettings7.ContainerHandledFullScreen = 1;
// 自动控制屏幕显示尺寸
//axMsRdpc.AdvancedSettings7.SmartSizing = true;
// 启用CredSSP身份验证(有些服务器连接没有反应,需要开启这个)
axMsRdpc.AdvancedSettings7.EnableCredSspSupport = true;
// 远程登录密码
axMsRdpc.AdvancedSettings7.ClearTextPassword = Password;
// 禁用公共模式
//axMsRdpc.AdvancedSettings7.PublicMode = false;
// 颜色位数 8,16,24,32
axMsRdpc.ColorDepth = 32;
// 开启全屏 true|flase
axMsRdpc.FullScreen = false;
SetRdpWithHeight(this.Width, this.Height);
// 远程连接
axMsRdpc.Connect();
}
private void axMsRdpc_OnDisconnected(object sender, IMsTscAxEvents_OnDisconnectedEvent e)
{
if (axMsRdpc == sender)
{
string disconnectedText = string.Format("远程桌面 {0} 连接已断开!", axMsRdpc.Server);
axMsRdpc.DisconnectedText = disconnectedText;
if (LongConnect)
{
Global.WinMessage(disconnectedText, "远程连接");
}
}
}
private void axMsRdpc_OnConnecting(object sender, EventArgs e)
{
if (axMsRdpc == sender)
{
axMsRdpc.ConnectingText = axMsRdpc.GetStatusText(Convert.ToUInt32(axMsRdpc.Connected));
}
}
public void axMsRdpcDisconnected()
{
if (axMsRdpc != null && axMsRdpc.Connected != 0)
{
axMsRdpc.Disconnect();
}
}
public void SetRdpWithHeight(int width, int height)
{
axMsRdpc.DesktopWidth = width;
axMsRdpc.DesktopHeight = height;
}
public string GetConnectServer()
{
if (axMsRdpc!=null && axMsRdpc.Connected != 0)
{
return axMsRdpc.Server;
}
return "";
}
}
}