WPF:DrawingObject (Halcon)

本文翻譯自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;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章