我正在嘗試使用WPF,GridSplitter和DirectX呈現WindowsFormsHostControl爲我的遊戲製作一個現代編輯器。我可以很好地使用主C#程序中調用的C++ DirectX例程進行繪製,但我遇到了一個奇怪的問題。接下來是我的編輯窗口的圖像:WindowsFormsHost中的WPF,GridSplitter和DirectX
的中心區是C++的DirectX渲染的一個,在左,右區我有WPF控件。你可以看到左邊的區域被WPF GridSplitter分割。
在我調用DirectX CreateDevice()函數之前,我可以上下拖動GridSplitter而不會出現任何問題,但是當調用該函數並啓動DirectX渲染時,我也可以拖動GridSplitter,但它會返回原始中心如果我做出微小的鼠標移動,在幾毫秒後放置位置,而如果我用鼠標做出「大」移動,GridSplitter會相應地移動(或多或少......但我失去了精確度,用戶會認爲它真的很糟糕)。
即使我禁用了渲染(但我繼續將DirectX設備連接到WindowsFormsHost hwnd),它不是機器速度問題,但異常仍然存在。似乎一旦創建了DirectX設備,它就會向WPF「吃」事件。我在DirectX或WPF初始化過程中忘記了什麼嗎?有辦法消除這個問題?
我現在想的唯一解決方案是在用戶拖動GridSplitter並在用戶完成拖動時重新創建它時關閉DirectX設備,但我希望能夠解決問題,而不是避免它!非常感謝你的答覆。請注意,在包含在WindowsFormsHost中的面板中,我在運行時添加了一個WinForms控件(從Winforms Control類派生的一個類)從那裏我把窗口句柄,並將其傳遞給調用DirectX CreateDevice()函數的C++例程。
<Grid Name="ccgrid1" ClipToBounds="False" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="Auto">
</RowDefinition>
<RowDefinition Height="*">
</RowDefinition>
<RowDefinition Height="Auto">
</RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Grid.Row="0" IsEnabled="True" HorizontalAlignment="Stretch" Name="ccCaratteri" VerticalAlignment="Stretch"
HorizontalContentAlignment="Center" VerticalContentAlignment="Stretch" ClipToBounds="False"
Background="#FFAD4545" Width="Auto" SelectionChanged="ccCaratteri_SelectionChanged">
<ListBox.BindingGroup>
<BindingGroup />
</ListBox.BindingGroup>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"
Width="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBox}},
Path=ActualWidth}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<GridSplitter Grid.Row="1" Height="10" ResizeDirection="Rows" HorizontalAlignment="Stretch">
<GridSplitter.Template>
<ControlTemplate TargetType="{x:Type GridSplitter}">
<Grid>
<Button Content="⁞" />
<Rectangle Fill="#00FFFFFF" />
</Grid>
</ControlTemplate>
</GridSplitter.Template>
</GridSplitter>
<TreeView x:Name="t1" Grid.Row="2" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
KeyboardNavigation.TabNavigation="Cycle">
<TreeViewItem Name="RTBContainer" Header="Global" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" Focusable="False" IsExpanded="True" >
<Border BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" BorderThickness="1,1,1,0" >
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Kerning" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Padding="0,0,0,0"/>
<local:CustomNumericTextbox Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch" BorderThickness="0" CJNumOfVisibleChars="-1" CJRangeValidation="-100,100"
CJSigned="True" CJMaxLength="3"></local:CustomNumericTextbox>
</Grid>
</Border>
<Border BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" BorderThickness="1">
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Leading" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Padding="0,0,0,0" />
<local:CustomNumericTextbox Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" BorderThickness="0"
CJNumOfVisibleChars="-1" CJRangeValidation="-100,100" CJSigned="True" CJMaxLength="3"></local:CustomNumericTextbox>
</Grid>
</Border>
</TreeViewItem>
</TreeView>
</Grid>
<WindowsFormsHost Grid.Row="1" Grid.Column="1" Background="Black" >
<wf:Panel x:Name="ccWinFormsPanel"/>
<!--<wf:Button x:Name="ccFormButton" Width="1200"/>-->
<!--<wf:MaskedTextBox x:Name="ccFormControl" Mask="00/00/0000"/>-->
</WindowsFormsHost>
<ScrollViewer Grid.Row="1" Grid.Column="2" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<Canvas Name="ccCanvasBitmap" Height="Auto" Background="Aquamarine" ClipToBounds="True" />
</ScrollViewer>
<Menu Grid.Row="0" Grid.Column="0" DockPanel.Dock="Top" Grid.ColumnSpan="3" VerticalAlignment="Bottom" Height="21">
<MenuItem Header="F_ile">
<MenuItem Header="_New Font..." Name="menuNewFont" Click="menuNewFont_Click"/>
<MenuItem Header="_Open Font..." Name="menuOpenFont" Click="menuOpenFont_Click"/>
<MenuItem Header="_Import Image..." Name="menuImportImage" Click="menuImportImage_Click"/>
</MenuItem>
</Menu>
<StatusBar Grid.Row="2" Grid.ColumnSpan="3" Height="auto">
<Label>Zoom</Label>
<ComboBox Name="ccZoom" SelectionChanged="ccZoom_SelectionChanged" x:FieldModifier="public">
</ComboBox>
</StatusBar>
</Grid>
WPF控件和DirectX設備窗口之間的窗口層次結構是什麼? – legalize
@legalize感謝您的回答,我用您提出的信息更新了我的原始問題,參見上文。 – 1Garrett2010
那麼你的DirectX設備是在'ccWinFormsPanel'上創建的? – legalize