zoukankan      html  css  js  c++  java
  • com组件远程桌面rdp模块的调用

    rdp(remote desktop protocol)是一个多通道的协议,包括客户端视音传输、文件传输和通讯端口转向等等功能,通过压缩处理的数据网络传输也是相当快。我们在windows操作系统下面,经常用到的mstsc.exe,也提供了com组件调用的接口。

    可以建立一个winform的project,通过【工具箱】->【Choose Items】将com控件添加进来。

    ↑ 可以看到选项卡下面列了多个版本的组件,这里要提醒一下,它们是有版本功能的区别的,他们的clsid都是不一样的,而且代表着不同操作系统版本。这里的向下兼容是对操作系统版本而言,换句话说高一级的版本com控件不一定能在低一级操作系统环境正常运行。

    下面从工具箱拖拉过程对于winform应该没有啥异议的地方,我们来看一下wpf如何添加组件,同上的操作,工具箱中com控件的状态是不可用的,通过project的【add reference】列,找到了一项。

    ↑ terminal service,有种眼熟的感觉,linux下面常用的就是terminal终端,加进来发现少了一个AxInterop.MSTSCLib.dll。解决办法很简单,在wpf project里面加一个winform窗口,让后从工具箱把控件拉过去,project就自动加上这两个接口操作文件。

    下面在代码里里敲入一句xaml。

    1 <WindowsFormsHost Visibility="{Binding HostVisible}" x:Name="host" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />

     我们再加一个cs,来继承一下这个activex控件。WndProc句柄操作在对象初始化的时候会调用到,以及在rdp的connect等过程也会执行到,看注释是为了解决鼠标焦点异常的问题。

     1 public class MyRDP : AxMSTSCLib.AxMsRdpClient2NotSafeForScripting
     2 {
     3     public MyRDP()
     4         : base()
     5     {
     6     }
     7 
     8     protected override void WndProc(ref System.Windows.Forms.Message m)
     9     {
    10         // Fix for the missing focus issue on the rdp client component
    11         if (m.Msg == 0x0021) // WM_MOUSEACTIVATE
    12         {
    13             if (!this.ContainsFocus)
    14             {
    15                 this.Focus();
    16             }
    17         }
    18 
    19         base.WndProc(ref m);
    20     }
    21 }

     好,在viewmodel里面看一下初始化。看了下这个rdp实现了ISupportInitialize接口,目的是为了初始化相关依赖属性,初始化顺序在BeginInit和EndInit之间完成,只要实现了这个接口,设计器自动帮你完成,在做winform的东西,不知道大家注意到form窗体下面的designer.cs。

    private void InitData()
    {
        this.rdp = new MyRDP();
        ((System.ComponentModel.ISupportInitialize)(rdp)).BeginInit();
        this.rdp.Name = "rdp";
        this.rdp.Enabled = true;
        this.rdp.Dock = System.Windows.Forms.DockStyle.None;
        this.rdp.Location = new System.Drawing.Point(0, 0);
        this.rdp.OnConnecting += new EventHandler(this.RDPClient_OnConnecting);
        this.rdp.OnConnected += new EventHandler(this.RDPClient_OnConnected);
        this.rdp.OnDisconnected += new AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEventHandler(this.RDPClient_OnDisconnected);
        host.Child = this.rdp;
        ((System.ComponentModel.ISupportInitialize)(rdp)).EndInit();
    
        this.BtnContent = "connect";
    
        this.MaskVisible = System.Windows.Visibility.Visible;
        this.HostVisible = System.Windows.Visibility.Collapsed;
    }

    下面看一下connect的部分。

     1 private void Connect()
     2 {
     3     this.rdp.Server = this.Address;
     4     this.rdp.UserName = this.Name;
     5     this.rdp.AdvancedSettings2.RDPPort = 3389;
     6     this.rdp.AdvancedSettings2.SmartSizing = true;
     7 
     8     this.rdp.Width = Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenWidth);
     9     this.rdp.Height = Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenHeight);
    10     this.rdp.DesktopWidth = Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenWidth);
    11     this.rdp.DesktopHeight = Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenHeight);
    12     this.rdp.FullScreenTitle = "this is test";
    13     MSTSCLib.IMsTscNonScriptable secured = (MSTSCLib.IMsTscNonScriptable)rdp.GetOcx();
    14     secured.ClearTextPassword = this.Password;
    15 
    16     try
    17     {
    18         this.rdp.Connect();
    19     }
    20     catch
    21     {
    22     }
    23 }

    ok,咱们看一下大概的效果。

    full screen的代码很easy。

    1 private void ToggleFullScreen()
    2 {
    3     this.rdp.FullScreen = !this.rdp.FullScreen;
    4 }

    好了上面rdp组件大概使用过程,另外这里有个叫WindowsFormsHost的控件值得说一下,xaml里面它的作用是承载winform控件,因为它是独立的hdwnd,所以它是凌驾于xaml控件之上的,比如用scrollviewer根本包不住它,stackoverflow也有人做了相关的扩展。最新4.5 beta framework里面好像对这个空间做了相关扩展,官方文档也有介绍,具体还没有正式发布出来。

  • 相关阅读:
    ASP.NET MVC 实现二级域名(泛域名)
    linq to sql 语句基本查询(3):Select和Count/Sum/Min/Max/Avg
    DataSet用法详细
    MVC中返回Json的几种声明方式
    利用索引提高SQL Server数据处理的效率
    嫁给以下十种男人的女人离“地狱”就不远了
    Thinkpad x200 X201拆机换风扇教程 实图
    笔记一则
    冬季谨防胃病复发 放松精神吃温热食物
    就是这样给X200加内存的(申精)
  • 原文地址:https://www.cnblogs.com/yuefei/p/4390510.html
Copyright © 2011-2022 走看看