如何在 WPF 应用程序中托管终端会话 (mstsc)?

发布于 2024-09-17 16:40:35 字数 61 浏览 10 评论 0原文

有一些工具可用于管理多个终端 (mstsc) 会话。

我将如何在 WPF 中实现类似的目标?

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

How would I go about achieving something similar in WPF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

护你周全 2024-09-24 16:40:35

您应该使用 WindowsFormsHost 元素来托管 RDP 的 ActiveX 控件。

有一个简短的示例如何将 Windows Media Player 集成到 WPF 应用程序中。 RDP 控件的托管类似。

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.

孤独岁月 2024-09-24 16:40:35
  1. 您应该向项目添加两个库:
    AxInterop.MSTSCLib.dll,
    Interop.MSTSCLib.dll

您可以从 MS 官方网站的 MS RDCMan 获取它。如何从“参考”中的 COM 选项卡添加它 - 是个好问题......
2. 添加到 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. 创建新的 rdp 客户端类:

    公共类 RdpControl :AxMSTSCLib.AxMsRdpClient9NotSafeForScripting
    {
    公共 RdpControl()
    : 根据()
    {
    }

    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        // 修复 rdp 客户端组件缺少焦点问题
        if (m.Msg == 0x0021) // WM_MOUSEACTIVATE
        {
            if (!this.ContainsFocus)
            {
                this.Focus();
            }
        }
    
        base.WndProc(ref m);
    }}
    
  2. 在您的 UserControl 的后面代码中:

    私有无效InitData()
    {
    _rdp = 新的 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);
        尝试
        {
            _rdp.Connect();
        }
        抓住
        {
        }
    }
    
  3. 使用此处理程序添加到 UserControl 按钮:

    private void Button_Click(对象发送者, RoutedEventArgs e)
    {
        初始化数据();
        连接();
    }
    

希望有帮助。

  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.

不美如何 2024-09-24 16:40:35

这些工具很可能使用远程桌面 ActiveX 控件,该控件设计为托管在网页中,但由于它是 ActiveX 控件,因此您也应该能够自行托管它。

如果不出意外,您可以在 WPF 应用程序中嵌入一个 Web 浏览器控件,然后在其中嵌入 ActiveX 控件。

请参阅以下链接:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文