开发者

How to host a terminal session (mstsc) in a WPF applicaiton?

开发者 https://www.devze.com 2023-01-15 03:39 出处:网络
There are some tools out there for ma开发者_如何学JAVAnaging multipleterminal(mstsc) sessions. How would I go about achieving something similar in WPF?You should use WindowsFormsHost element to host

There are some tools out there for ma开发者_如何学JAVAnaging multiple terminal (mstsc) sessions.

How would I go about achieving something similar in WPF?


You should use WindowsFormsHost element to host the ActiveX control of RDP.

There is short sample how to integrate Windows Media Player into WPF application. The hosting of the RDP control is similar.


  1. You should add to project two libs: AxInterop.MSTSCLib.dll, Interop.MSTSCLib.dll

You can get it from MS RDCMan from official MS site. How to add it from COM tab in "References" - is great question... 2. Add to XAML WindowsFormsHost:

<UserControl x:Class="VMViewer"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="231" d:DesignWidth="274" Loaded="UserControl_Loaded">
<Border BorderThickness="1" BorderBrush="CornflowerBlue">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="22"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" x:Name="connectBtn" Content="Connect" Click="Button_Click" DockPanel.Dock="Top" HorizontalAlignment="Stretch" />
        <WindowsFormsHost Grid.Row="1" Margin="0,0,0,0" x:Name="wfHost" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Grid>
</Border>

  1. Create new rdp client class:

    public class RdpControl : AxMSTSCLib.AxMsRdpClient9NotSafeForScripting { public RdpControl() : base() { }

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        // Fix for the missing focus issue on the rdp client component
        if (m.Msg == 0x0021) // WM_MOUSEACTIVATE
        {
            if (!this.ContainsFocus)
            {
                this.Focus();
            }
        }
    
        base.WndProc(ref m);
    }}
    
  2. In behind code of your UserControl:

    private void InitData() { _rdp = new RdpControl(); ((System.ComponentModel.ISupportInitialize)(_rdp)).BeginInit(); _rdp.Name = "rdp"; _rdp.Enabled = true; wfHost.Child = _rdp; ((System.ComponentModel.ISupportInitialize)(_rdp)).EndInit(); }

    private void Connect()
    {
        _rdp.Server = CurrentVM.Name;
        _rdp.UserName = CurrentVM.Login;
        _rdp.AdvancedSettings9.ClearTextPassword = CurrentVM.Password;
        _rdp.ColorDepth = 24;
        _rdp.AdvancedSettings9.SmartSizing = true;
        _rdp.AdvancedSettings9.AuthenticationLevel = 2;
        _rdp.AdvancedSettings9.EnableCredSspSupport = true;
        _rdp.Width = Convert.ToInt32(this.ActualWidth);
        _rdp.Height = Convert.ToInt32(this.ActualHeight);
        _rdp.DesktopWidth = Convert.ToInt32(this.ActualWidth);
        _rdp.DesktopHeight = Convert.ToInt32(this.ActualHeight);
        try
        {
            _rdp.Connect();
        }
        catch
        {
        }
    }
    
  3. Add to UserControl button with this handler:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        InitData();
        Connect();
    }
    

Hope it helps.


Those tools are most likely using the Remote Desktop ActiveX Control which is designed to be hosted in web pages, but since it is an ActiveX control, you should be able to host it on its own as well.

If nothing else, you could embed a web browser control in your WPF application and then embed the ActiveX control inside that.

See the following links:

  • Downloading and Using the Remote Desktop ActiveX Control
  • Sample Webpage Included with the Remote Desktop ActiveX Control
0

精彩评论

暂无评论...
验证码 换一张
取 消