本文翻译自Halcon中的实例(DrawingObjectsWPF)。
此实例介绍了:存在一个*.hdev格式的图像处理文件,把整个图像处理过程导出为C#代码;通过右键点击界面上的图像而画矩形,圆等。
1)创建WPF窗体文件,.Net版本选择3.5;
2)添加HalconDotNet 3.5版本的引用;
3)进入XAML文件中:
3.1)添加Window的命名空间引用,设置窗体大小,及Loaded事件;
xmlns:my="clr-namespace:HalconDotNet;assembly=halcondotnet" mc:Ignorable="d" Title="MainWindow" Height="525" Width="525" Loaded="MainWindow_Loaded"
4)提供右键菜单,并为每个菜单提供Click事件:
<Window.Resources> <ContextMenu x:Key="cmButton"> <MenuItem Header="rectangle1" Click="OnRectangle1_Click"/> <MenuItem Header="rectangle2" Click="OnRectangle2_Click"/> <MenuItem Header="circle" Click="OnCircle_Click"/> <MenuItem Header="ellipse" Click="OnEllipse_Click"/> <MenuItem Header="clear all objects" Click="OnClearAllObjects_Click"/> </ContextMenu> </Window.Resources>
5)设置Grid为两行一列:
<Grid.RowDefinitions> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="*" Name="Contianer"></RowDefinition> </Grid.RowDefinitions>
第一行为Label;第二行为Halcon窗体:
<Label Content="Add new drawing objects via the context menu (right mouse button)" Name="label1" Grid.Row="0"/> <my:HSmartWindowControlWPF HorizontalAlignment="Left" Height="494" Margin="0" Grid.Row="1" VerticalAlignment="Top" Width="517" Name="hSmartWindowControlWPF1" MouseRightButtonDown="HSmartWindowControlWPF1_MouseRightButtonDown" HInitWindow="HSmartWindowControlWPF1_HInitWindow"/>
上面窗体的名字为:hSmartWindowControlWPF1,并提供右击和初始化的事件;
6)接下来就是为对应的方法提供处理过程(C#文件),其中右键的处理为:
ContextMenu cm = this.FindResource("cmButton") as ContextMenu; cm.PlacementTarget = sender as Button; cm.IsOpen = true;