c#封装远程桌面


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 System.Runtime.InteropServices;
using MSTSCLib;
 
namespace PemRDPControl
{
 
    [Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IObjectSafety
    {
        // methods
        void GetInterfacceSafyOptions(
            System.Int32 riid,
            out System.Int32 pdwSupportedOptions,
            out System.Int32 pdwEnabledOptions);
        void SetInterfaceSafetyOptions(
            System.Int32 riid,
            System.Int32 dwOptionsSetMask,
            System.Int32 dwEnabledOptions);
    }
 
    //the function for ie to call
    [ComVisible(true)]
    [Guid("89970EFC-0014-4A1C-BA19-A18332E7A0BB")]
  [InterfaceType(ComInterfaceType.InterfaceIsDual)]  
    public interface IPemRDPControl
  {
      [DispId(1)]
      void SetControlSize(int width, int height);
      [DispId(2)]
      void Connect();
      string Server
      {
          get;
          set;
      }
 
      string UserName
      {
          get;
          set;
      }
      string PassWord
      {
          get;
          set;
      }
      string Domain
      {
          get;
          set;
      }
      int RDPPort
      {
          get;
          set;
      }
      bool ConnectToServerConsole
      {
          get;
          set;
      }
      bool ConnectToAdministerServer
      {
          get;
          set;
      }
      int DesktopWidth
      {
          get;
          set;
      }
      int DesktopHeight
      {
          get;
          set;
      }
      int Width
      {
          get;
          set;
      }
      int Height
      {
          get;
          set;
      }
      bool FullScreen
      {
          get;
          set;
      }
      ExtendedDisconnectReasonCode ExtendedDisconnectReason
      {
          get;
      }
 
    }
 
    //the events
    [ComVisible(true)]
    [Guid("60FB4E65-C928-4D2F-B52E-2993165D314D")]
  [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IPemRDPControlEvents
  {
      [DispId(21)]
      void OnConnected();
      [DispId(22)]
      void OnConnecting();
      [DispId(23)]
        void OnDisconnected(int reasoncode);
  }  
 
 
 
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(IPemRDPControl))]
    [ComSourceInterfaces(typeof(IPemRDPControlEvents))]
    [ComVisible(true)]
    [Guid("845FC80C-753A-4095-96D4-9BA2F6DE8E23")]
    public partial class UserControl1 : UserControl, IObjectSafety, IPemRDPControl
    {
        public delegate void OnConnectedHandler();
        public delegate void OnConnectingHandler();
        public delegate void OnDisconnectedHandler(int reasoncode);
        public event OnConnectedHandler OnConnected;
        public event OnDisconnectedHandler OnDisconnected;
        public event OnConnectingHandler OnConnecting;
 
        #region Property
        public string Server
        {
            get;
            set;
        }
        public string UserName
        {
            get;
            set;
        }
        public string PassWord
        {
            get;
            set;
        }
        public string Domain
        {
            get;
            set;
        }
        public int RDPPort
        {
            get;
            set;
        }
        public  bool ConnectToServerConsole
        {
            get;
            set;
        }
        public bool ConnectToAdministerServer
        {
            get;
            set;
        }
        public int DesktopWidth
        {
            get;
            set;
        }
        public int DesktopHeight
        {
            get;
            set;
        }
        public int Width
        {
            get;
            set;
        }
        public int Height
        {
            get;
            set;
        }
        public bool FullScreen
        {
            get;
            set;
        }
        #endregion
        public UserControl1()
        {
            InitializeComponent();
            Server = "";
            UserName = "";
            PassWord = "";
            ConnectToServerConsole = true;
            ConnectToAdministerServer = true;
            RDPPort = 3389;
            Domain = "";
            Width = Height = 0;
            DesktopWidth = DesktopHeight = 0;
            FullScreen = false;
 
        }
        public ExtendedDisconnectReasonCode ExtendedDisconnectReason
        {
            get { return (ExtendedDisconnectReasonCode)0; }
        }
       
        public void Connect()
        {
            ((Control)this).Width = Width;
            ((Control)this).Height = Height;
            axMstscObj.Width = Width;
            axMstscObj.Height = Height;
            axMstscObj.DesktopWidth = DesktopWidth;
            axMstscObj.DesktopHeight = DesktopHeight;
           // axMstscObj.ful.FullScreen = FullScreen;
            axMstscObj.Domain = Domain;
            axMstscObj.Server = Server;
            axMstscObj.UserName = UserName;
            IMsRdpClientAdvancedSettings7 iClientSetting = (IMsRdpClientAdvancedSettings7)axMstscObj.AdvancedSettings;
            iClientSetting.RedirectDrives = false;
            iClientSetting.RedirectPrinters = true;
            iClientSetting.RedirectPorts = false;
            iClientSetting.RedirectSmartCards = true;
            iClientSetting.RDPPort = RDPPort;
            iClientSetting.ClearTextPassword = PassWord;
            iClientSetting.ConnectToServerConsole = ConnectToServerConsole;
            iClientSetting.ConnectToAdministerServer = ConnectToAdministerServer;
            axMstscObj.Connect();
        }
        private void axMstscObj_OnConnected(object sender, EventArgs e)
        {
            //fire event to ie
            OnConnected();
        }
 
        private void axMstscObj_OnDisconnected(object sender, AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e)
        {
            //fire event to ie
            OnDisconnected(e.discReason);           
        }
 
        private void axMstscObj_OnConnecting(object sender, EventArgs e)
        {
            //fire event to ie
            OnConnecting();
        }
 
        private void axMstscObj_OnLogonError(object sender, AxMSTSCLib.IMsTscAxEvents_OnLogonErrorEvent e)
        {
 
        }
        #region IPemRDPControl 成员
        public void SetControlSize(int width, int height)
        {
            axMstscObj.Width = width;
            axMstscObj.Height = height;
        }
        #endregion
        #region IObjectSafety 成员
        public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
        {
            pdwSupportedOptions = 1;
            pdwEnabledOptions = 2;
        }
 
        public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
        {
        }
        #endregion
 
        private void axMstscObj_OnAuthenticationWarningDismissed(object sender, EventArgs e)
        {
        }
 
        private void axMstscObj_OnAuthenticationWarningDisplayed(object sender, EventArgs e)
        {
        }
 
    }
}

 网页调用

 <html>
 <head>
  <title>Demo test</title>
   <script language="javascript">
       function TestConnect() {
           try {
               var obj = document.getElementById("MyActive");
               obj.Server = "testserver";
               obj.RDPPort = 3389;
               obj.UserName = "root";
 
               obj.PassWord = "testpwd";
               obj.ConnectToServerConsole = true;
               obj.ConnectToAdministerServer = true;
               var screenHeight = 1920;
               var screenWidth = 1080;
               obj.Width = screenHeight;
               obj.Height = screenWidth;
               obj.DesktopWidth = screenHeight;
               obj.DesktopHeight = screenWidth;
               obj.Connect();
           }
           catch (e) {
               alert(e.Message);
           }
   }
   </script>
 </head>
 <body style=" margin-left:0; margin-right: 0; margin-top:0;margin-bottom:0" >
<script for="MyActive" event="OnConnected" language="javascript">
 //alert("OnConnected");
</script>
<script for="MyActive" event="OnConnecting" language="javascript">
 //alert("OnConnecting");
</script>
<script for="MyActive" event="OnDisConnected(reasoncode)" language="javascript">
 alert("OnDisConnected:" + reasoncode);
 //alert(document.getElementById("MyActive").ExtendedDisconnectReason);
</script>
 <!--
<a href='javascript::' onclick='TestConnect();'>connect</a> <br />
<a href='javascript::' onclick='document.getElementById("MyActive").SetControlSize(300, 200);'>Set size</a><br />
<a href='javascript::' onclick='alert(document.getElementById("MyActive").ExtendedDisconnectReason);'>show externalerrorcode</a><br />
 -->
  <object id="MyActive" name="MyActive" classid="clsid:845FC80C-753A-4095-96D4-9BA2F6DE8E23"  VIEWASTEXT ></object>
    <script language="javascript">
        function OpenActiveX()
        {
            try {
             var obj = document.getElementById("MyActive");
             if (obj != null) {
                // obj.mWidth = 1030;
                 // obj.mHeight = 780;
                 TestConnect();
             }
             else
                 alert("Load failed.");
         }
         catch(Err)
         {
          alert(Err.description);
         }
  }
  OpenActiveX();
    </script>
 </body>
</html>

 

 

 

 

 


豫ICP备12024565号-1   E-mail:admin@hlc8.com