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

南楼画角 提交于 2021-02-10 08:49:06

问题


There are some tools out there for managing multiple terminal (mstsc) sessions.

How would I go about achieving something similar in WPF?


回答1:


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.




回答2:


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



回答3:


  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.



来源:https://stackoverflow.com/questions/3632872/how-to-host-a-terminal-session-mstsc-in-a-wpf-applicaiton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!