在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 远程桌面连接 实例源码下载

C# 远程桌面连接 实例源码下载

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:3.31M
  • 下载次数:115
  • 浏览次数:1188
  • 发布时间:2017-06-24
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: C# 远程桌面

实例介绍

【实例简介】

【实例截图】

【核心代码】


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
    }
}


标签: C# 远程桌面

实例下载地址

C# 远程桌面连接 实例源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警