写个自己的远程桌面

今天偶然在首页看到了这篇文件http://www.cnblogs.com/mobwiz/p/remote_desktop_share.html,感觉很有意思,一起以来,远程桌面在我心目中是很高大上的,哪想着几行代码就能实现了

自己试了一下,同事那边连了三个连接也没什么问题,和远程桌面效果完全一致。

下面是代码,只为以后自己用到找代码方便,基本来自上面的文章,感谢作者。截图什么的请看原文

 

服务器端代码,请先在引用里面添加rdpcomapi,需要在COM项中才能找到

private void button1_Click(object sender, EventArgs e)
        {
            var _rdpSession = new RDPSession();  // 新建RDP Session

            _rdpSession.OnAttendeeConnected += _rdpSession_OnAttendeeConnected;

            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);

            _rdpSession.SetDesktopSharedRect(rect.X, rect.Y, rect.Right, rect.Bottom); // 设置共享区域,如果不设置默认为整个屏幕,当然如果有多个屏幕,还是设置下主屏幕,否则,区域会很大

            _rdpSession.Open(); // 打开会话

            IRDPSRAPIInvitation invitation = _rdpSession.Invitations.CreateInvitation("baseAuth", "groupName", "", 64);  // 创建申请
            Console.WriteLine(invitation.ConnectionString);
            Clipboard.SetText(invitation.ConnectionString);//最最重要的连接字符串
        }

        //经过测试,这个一定得有,否则连接不上
        private void _rdpSession_OnAttendeeConnected(object pAttendee)
        {
            textBox1.AppendText("有用户连接");
            IRDPSRAPIAttendee att = pAttendee as IRDPSRAPIAttendee;


            att.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_VIEW;//这个一定要有
        }

  

客户端代码,在工具箱中,添加RDP Viewer Class,然后把该控件拖到窗体上

private void button1_Click(object sender, EventArgs e)
{
   //textBox1里面放了连接字符串
     axRDPViewer1.Connect(textBox1.Text, Environment.UserName, "");
}

  

下面是效果图,同时三个连接

posted @ 2016-02-04 14:12  灰色逻辑  阅读(1665)  评论(1编辑  收藏  举报