Winform 中的键盘预览调用 Wpf 用户控件

Posted

技术标签:

【中文标题】Winform 中的键盘预览调用 Wpf 用户控件【英文标题】:Keyboard preview in Winform calling a Wpf Usercontrol 【发布时间】:2021-02-28 14:46:28 【问题描述】:

我有一个 Winform 应用程序,我从中调用 Wpf Usercontrol,它实际上是一个 Viewport3D,Winform 应用程序在其上绘制 3D 元素,例如圆柱体。

我需要从 Winform 应用程序访问键盘,以便捕捉 ^+ 和 ^- 击键来放大/缩小,以及箭头键来旋转显示。

基本代码如下所示。键盘控制处理程序在 Sub New 中定义。虽然 mouseControl 运行良好,但 Form1 永远不会收到键盘敲击。 Form1 中启用了键盘预览。

有人有什么建议吗?

提前致谢。克劳德

        Private Sub Form1_Load(sender As System.Object, _
                               e      As System.EventArgs) Handles MyBase.Load 


        ' Create the WPF UserControl.
        '
        WpfUc        = New UserControl1
        ' ==========================

        Host.Dock =  DockStyle.Fill
        Host.Name = "Host"


        ' Assign the WPF UserControl to the ElementHost control's
        ' Child property.
        '
        Host.Child = WpfUc      


        ' Add the ElementHost control to the form's
        ' collection of child controls.
        '
        Me.Controls.Add(Host)


        Visual3d            = New ModelVisual3D
        Group3d             = New Model3DGroup

     
        Visual3d.Content      = Group3d
        WpfUc.myViewport.Children.Add(Visual3d)


        DefineCamera(TheCamera, WpfUC.myViewport)
        DefineModelCylinders(Group3d)
    End Sub

    Private Sub DefineCamera(ByRef iCamera     As OrthographicCamera,          _
                             ByRef viewport    As Windows.Controls.Viewport3D)

        iCamera                   = New OrthographicCamera
        CameraController          = New SphericalCameraController(iCamera, viewport, WpfUc, WpfUc, WpfUc)
        iCamera.FarPlaneDistance  = 100
        iCamera.NearPlaneDistance = 0
        iCamera.Position          = New Point3D(0, 0, 100)
        iCamera.Width             = 15

    End Sub

    Friend Sub New(ByVal Camera          As OrthographicCamera,           _
                   ByVal viewport        As Windows.Controls.Viewport3D,  _
                   ByVal keyboardControl As UIElement,                    _
                   ByVal wheelControl    As UIElement,                    _
                   ByVal mouseControl    As UIElement)

        TheCamera                   = Camera
        viewport.Camera             = TheCamera

        iKeyboardControl            = keyboardControl
        AddHandler KeyboardControl.PreviewKeyDown,      AddressOf KeyboardControl_KeyDown

        iWheelControl               = wheelControl
        AddHandler WheelControl.PreviewMouseWheel,      AddressOf WheelControl_PreviewMouseWheel

        iMouseControl               = mouseControl
        AddHandler iMouseControl.MouseDown,             AddressOf MouseControl_MouseDown

        PositionCamera()

    End Sub

  Private Sub KeyboardControl_KeyDown(ByVal sender As Object, _
                                      ByVal e      As KeyEventArgs)

        Select Case e.Key
            Case Key.Up
                IncreasePhi()
            Case Key.Down
                DecreasePhi()
            Case Key.Left
                IncreaseTheta()
            Case Key.Right
                DecreaseTheta()
            Case Key.Add, Key.OemPlus
                IncreaseR()
            Case Key.Subtract, Key.OemMinus
                DecreaseR()
        End Select

        PositionCamera()
    End Sub

Wpf UserControl 出现在下面。

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="HostingWpfUserControlInWf.UserControl1"
    d:DesignWidth="477.396" d:DesignHeight="274.41"
    >
   <Grid Margin="0">
      <!-- Viewport3D is the rendering surface. -->
      <Viewport3D x:Name="myViewport" x:FieldModifier="Public" Margin="0,0,0,0">
      </Viewport3D>
   </Grid>
</UserControl>

【问题讨论】:

【参考方案1】:

好吧,经过大量研究和反复试验,我发现需要 Wpf 用户控件的 2 个设置才能将键盘输入传递给托管表单:

    WpfUc.Focusable = True

    WpfUC.myViewport.Focusable = True

很容易,但很难找到......

【讨论】:

以上是关于Winform 中的键盘预览调用 Wpf 用户控件的主要内容,如果未能解决你的问题,请参考以下文章

WinForm中调用WPF控件

winform 添加wpf 窗体怎么使用

会winform再学wpf简单吗

C#中如何实现WPF调用Winform中用户自定义的控件呢?

请问如何在c#中用WPF调用Winform变写的用户控件,知道的请给予解说,谢谢!

在winform中如何让图片控件根据键盘来移动