Winform中调用Wpf用户控件的键盘预览
我有一个Winform应用程序,我从中调用Wpf Usercontrol,它实际上是一个Viewport3D,Winform应用程序在其上绘制3D元素,例如圆柱体 我需要从Winform应用程序访问键盘,以便捕捉用于放大/缩小的^+和^-按键,以及用于旋转显示器的箭头键 基本代码如下所示。键盘控制处理程序在Sub New中定义。虽然mouseControl工作正常,但Form1从未接收到键盘笔划。在Form1中启用键盘预览 有人有什么建议吗 提前谢谢。克劳德Winform中调用Wpf用户控件的键盘预览,wpf,vb.net,winforms,keyboard,Wpf,Vb.net,Winforms,Keyboard,我有一个Winform应用程序,我从中调用Wpf Usercontrol,它实际上是一个Viewport3D,Winform应用程序在其上绘制3D元素,例如圆柱体 我需要从Winform应用程序访问键盘,以便捕捉用于放大/缩小的^+和^-按键,以及用于旋转显示器的箭头键 基本代码如下所示。键盘控制处理程序在Sub New中定义。虽然mouseControl工作正常,但Form1从未接收到键盘笔划。在Form1中启用键盘预览 有人有什么建议吗 提前谢谢。克劳德 Private Su
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
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>
好吧,经过大量的研究和反复试验,我发现要将键盘输入传递到托管表单,需要Wpf Usercontrol的两个设置:
WpfUc.Focusable = True
WpfUC.myViewport.Focusable = True
很容易,但很难找到