문제

There are some tools out there for managing 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.

다른 팁

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:

  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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top