基本信息
源码名称:C# 远程桌面连接 实例源码下载
源码大小:3.31M
文件格式:.zip
开发语言:C#
更新时间:2017-06-24
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using AxMSTSCLib;

namespace MultiRemoteDesktopClient
{
    public delegate void Connected(object sender, EventArgs e, int ListIndex);
    public delegate void Connecting(object sender, EventArgs e, int ListIndex);
    public delegate void LoginComplete(object sender, EventArgs e, int ListIndex);
    public delegate void Disconnected(object sender, AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e, int ListIndex);
    public delegate void OnFormClosing(object sender, FormClosingEventArgs e, int ListIndex, IntPtr Handle);
    public delegate void OnFormActivated(object sender, EventArgs e, int ListIndex, IntPtr Handle);
    public delegate void OnFormShown(object sender, EventArgs e, int ListIndex, IntPtr Handle);
    public delegate void ServerSettingsChanged(object sender, Database.ServerDetails sd, int ListIndex);

    public partial class RdpClientWindow : Form
    {
        public event Connected Connected;
        public event Connecting Connecting;
        public event LoginComplete LoginComplete;
        public event Disconnected Disconnected;
        public event OnFormClosing OnFormClosing;
        public event OnFormActivated OnFormActivated;
        public event OnFormShown OnFormShown;
        public event ServerSettingsChanged ServerSettingsChanged;

        public Database.ServerDetails _sd;

        // used to easly locate in Server lists (RemoteDesktopClient)
        private int _listIndex = 0;

        private bool _isFitToWindow = false;

        public RdpClientWindow(Database.ServerDetails sd)
        {
            InitializeComponent();
            InitializeControl(sd);
            InitializeControlEvents();
        }

        public void InitializeControl(Database.ServerDetails sd)
        {
            GlobalHelper.infoWin.AddControl(new object[] {
                btnFitToScreen
            });

            this._sd = sd;

            rdpClient.Server = sd.Server;
            rdpClient.UserName = sd.Username;
            rdpClient.AdvancedSettings2.ClearTextPassword = sd.Password;
            rdpClient.ColorDepth = sd.ColorDepth;
            rdpClient.DesktopWidth = sd.DesktopWidth;
            rdpClient.DesktopHeight = sd.DesktopHeight;
            rdpClient.FullScreen = sd.Fullscreen;

            // this fixes the rdp control locking issue
            // when lossing its focus
            rdpClient.AdvancedSettings2.allowBackgroundInput = -1;

            // custom port
            if (sd.Port != 0)
            {
                rdpClient.AdvancedSettings2.RDPPort = sd.Port;
            }

            btnConnect.Enabled = false;
            tmrSC.Enabled = false;
        }

        public void InitializeControlEvents()
        {
            this.Shown  = new EventHandler(RdpClientWindow_Shown);
            this.FormClosing  = new FormClosingEventHandler(RdpClientWindow_FormClosing);
            this.Activated  = new EventHandler(RdpClientWindow_Activated);

            btnDisconnect.Click  = new EventHandler(ToolbarButtons_Click);
            btnConnect.Click  = new EventHandler(ToolbarButtons_Click);
            btnReconnect.Click  = new EventHandler(ToolbarButtons_Click);
            btnSettings.Click  = new EventHandler(ToolbarButtons_Click);
            btnFullscreen.Click  = new EventHandler(ToolbarButtons_Click);
            btnFitToScreen.Click  = new EventHandler(ToolbarButtons_Click);

            this.rdpClient.OnConnecting  = new EventHandler(rdpClient_OnConnecting);
            this.rdpClient.OnConnected  = new EventHandler(rdpClient_OnConnected);
            this.rdpClient.OnLoginComplete  = new EventHandler(rdpClient_OnLoginComplete);
            this.rdpClient.OnDisconnected  = new AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEventHandler(rdpClient_OnDisconnected);

            btnSndKey_TaskManager.Click  = new EventHandler(SendKeys_Button_Click);

            tmrSC.Tick  = new EventHandler(tmrSC_Tick);
        }

        #region EVENT: Send Keys
        void SendKeys_Button_Click(object sender, EventArgs e)
        {
            rdpClient.Focus();

            if (sender == btnSndKey_TaskManager)
            {
                //SendKeys.Send("(^%)");
                SendKeys.Send("(^%{END})");
            }

            //rdpClient.AdvancedSettings2.HotKeyCtrlAltDel;
        }
        #endregion

        #region EVENT: RDP Client

        void rdpClient_OnDisconnected(object sender, AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e)
        {
            Status("Disconnected from "   this._sd.Server);

            btnConnect.Enabled = true;
            btnDisconnect.Enabled = false;

            { // check connection status on output
                System.Diagnostics.Debug.WriteLine("OnDisconnected "   rdpClient.Connected);
            }

            if (Disconnected != null)
            {
                Disconnected(this, e, this._listIndex);
            }
        }

        void rdpClient_OnLoginComplete(object sender, EventArgs e)
        {
            Status("Loged in using "   this._sd.Username   " user account");

            { // check connection status on output
                System.Diagnostics.Debug.WriteLine("OnLoginComplete "   rdpClient.Connected);
            }

            if (LoginComplete != null)
            {
                LoginComplete(this, e, this._listIndex);
            }
        }

        void rdpClient_OnConnected(object sender, EventArgs e)
        {
            Status("Connected to "   this._sd.Server);

            { // check connection status on output
                System.Diagnostics.Debug.WriteLine("OnConnected "   rdpClient.Connected);
            }

            if (Connected != null)
            {
                Connected(this, e, this._listIndex);
            }
        }

        void rdpClient_OnConnecting(object sender, EventArgs e)
        {
            Status("Connecting to "   this._sd.Server);

            btnConnect.Enabled = false;
            btnDisconnect.Enabled = true;

            { // check connection status on output
                System.Diagnostics.Debug.WriteLine("OnConnecting "   rdpClient.Connected);
            }

            if (Connecting != null)
            {
                Connecting(this, e, this._listIndex);
            }
        }

        #endregion

        #region EVENT: server settings window

        Rectangle ssw_GetClientWindowSize()
        {
            return rdpClient.RectangleToScreen(rdpClient.ClientRectangle);
        }

        void ssw_ApplySettings(object sender, Database.ServerDetails sd)
        {
            this._sd = sd;

            MessageBox.Show("This will restart your connection", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

            Reconnect(true, false, false);
        }

        #endregion

        #region EVENT: other form controls

        void tmrSC_Tick(object sender, EventArgs e)
        {
            //Bitmap bmp = new Bitmap();
            //pictureBox1.BackgroundImage = ControlToImage.GetControlScreenshot(this.rdpClient);
            //pictureBox1.BackgroundImage = bmp;
        }

        void ToolbarButtons_Click(object sender, EventArgs e)
        {
            if (sender == btnDisconnect)
            {
                Disconnect();
            }
            else if (sender == btnConnect)
            {
                Connect();
            }
            else if (sender == btnReconnect)
            {
                Reconnect(false, this._isFitToWindow, false);
            }
            else if (sender == btnSettings)
            {
                ServerSettingsWindow ssw = new ServerSettingsWindow(this._sd);
                ssw.ApplySettings  = new ApplySettings(ssw_ApplySettings);
                ssw.GetClientWindowSize  = new GetClientWindowSize(ssw_GetClientWindowSize);
                ssw.ShowDialog();

                this._sd = ssw.CurrentServerSettings();

                if (ServerSettingsChanged != null)
                {
                    ServerSettingsChanged(sender, this._sd, this._listIndex);
                }
            }
            else if (sender == btnFullscreen)
            {
                DialogResult dr = MessageBox.Show("You are about to enter in Fullscreen mode.\r\nBy default, the remote desktop resolution will be the same as what you see on the window.\r\n\r\nWould you like to resize it automatically based on your screen resolution though it will be permanent as soon as you leave in Fullscreen.\r\n\r\nNote: This will reconnect.", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    Reconnect(false, false, true);
                }
                else
                {
                    rdpClient.FullScreen = true;
                }
            }
            else if (sender == btnFitToScreen)
            {
                DialogResult dr = MessageBox.Show("This will resize the server resolution based on this current client window size, though it will not affect you current settings.\r\n\r\nDo you want to continue?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                if (dr == DialogResult.OK)
                {
                    Reconnect(true, true, false);
                }
            }
        }

        void RdpClientWindow_Shown(object sender, EventArgs e)
        {
            if (OnFormShown != null)
            {
                OnFormShown(this, e, this._listIndex, this.Handle);
            }
        }

        void RdpClientWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("Are you sure you want to close this window?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information);

            if (dr == DialogResult.Yes)
            {
                Disconnect();
                rdpClient.Dispose();

                if (OnFormClosing != null)
                {
                    OnFormClosing(this, e, this._listIndex, this.Handle);
                }
            }
            else
            {
                e.Cancel = true;
            }
        }

        void RdpClientWindow_Activated(object sender, EventArgs e)
        {
            if (OnFormActivated != null)
            {
                OnFormActivated(this, e, this._listIndex, this.Handle);
            }
        }

        #endregion

        #region METHOD: s

        public void Connect()
        {
            Status("Starting ...");
            rdpClient.Connect();
        }

        public void Disconnect()
        {
            Status("Disconnecting ...");
            rdpClient.DisconnectedText = "Disconnected";

            if (rdpClient.Connected != 0)
            {
                rdpClient.Disconnect();
            }
        }

        public void Reconnect(bool hasChanges, bool isFitToWindow, bool isFullscreen)
        {
            Disconnect();

            Status("Waiting for the server to properly disconnect ...");

            // wait for the server to properly disconnect
            while (rdpClient.Connected != 0)
            {
                System.Threading.Thread.Sleep(1000);
                Application.DoEvents();
            }

            Status("Reconnecting ...");

            if (hasChanges)
            {
                rdpClient.Server = this._sd.Server;
                rdpClient.UserName = this._sd.Username;
                rdpClient.AdvancedSettings2.ClearTextPassword = this._sd.Password;
                rdpClient.ColorDepth = this._sd.ColorDepth;

                this._isFitToWindow = isFitToWindow;

                if (isFitToWindow)
                {
                    rdpClient.DesktopWidth = this.rdpClient.Width;
                    rdpClient.DesktopHeight = this.rdpClient.Height;
                }
                else
                {
                    rdpClient.DesktopWidth = this._sd.DesktopWidth;
                    rdpClient.DesktopHeight = this._sd.DesktopHeight;
                }

                rdpClient.FullScreen = this._sd.Fullscreen;
            }

            if (isFullscreen)
            {
                rdpClient.DesktopWidth = Screen.PrimaryScreen.Bounds.Width;
                rdpClient.DesktopHeight = Screen.PrimaryScreen.Bounds.Height;

                rdpClient.FullScreen = true;
            }

            Connect();
        }

        public Image GetCurrentScreen()
        {
            return ControlToImage.CaptureControlImage(ref rdpClient);
        }

        private void Status(string stat)
        {
            lblStatus.Text = stat;
        }

        #endregion

        #region PROPERTY

        public int ListIndex
        {
            get
            {
                return this._listIndex;
            }
            set
            {
                this._listIndex = value;
            }
        }

        #endregion
    }
}