LEADTOOLS Annotations for WPF and Silverlight Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.22
Using WPF Non Automated Annotations in Run Mode
Take the following steps to create and run a program that implements non-automated annotations, This program will let you create a Note and a button annotations, set the user mode through a menu, and activate the annotations by left clicking on the annotation.
  1. Start Visual Studio .NET.
  2. Choose File->New->Project… from the menu.
  3. In the New Project dialog box, choose either "Visual C# Projects" - NET Framework 3.0 or "Visual Basic Projects" - NET Framework 3.0 in the Projects Type List, and choose "Windows Application (WPF) " in the Templates List.
  4. Type the project name as "Using Non Automated Annotations in Run Mode" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK .
  5. In the "Solution Explorer" window, right-click on the "References" folder (For a VB project, right-click on the project file in solution explorer), and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to Leadtools For .NET "\LEAD Technologies\LEADTOOLS 17\Bin\DotNet\Win32 " folder and select the following DLLs:
    • Leadtools.Windows.Annotations.dll
    • Leadtools.Windows.Controls.dll
    • Leadtools.Windows.Media.Transitions.dll
    Click the Select button and then press the OK button to add the above DLLs to the application.
  6. Open Window1.xaml file and replace it by the following: [Visual Basic]
                <Window x:Class="Window1"
                           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                           Title="Using_Non_Automated_Annotations_in_Run_Mode" Height="300" Width="300"
                           Loaded="WindowLoaded"
                           xmlns:Leadtools_Windows_Controls="clr-namespace:Leadtools.Windows.Controls;assembly=Leadtools.Windows.Controls"
                    >
                    <StackPanel>
                       <RadioButton Name="rbDesignMode" GroupName="UserMode" Content="Design Mode" IsChecked="True" Checked="CheckedChanged"/>
                       <RadioButton Name="rbRunMode" GroupName="UserMode" Content="Run Mode" Checked="CheckedChanged"/>
                       <Separator/>
                       <Leadtools_Windows_Controls:BitmapSourceViewer x:Name="imageViewer1" Width="NaN" Height="NaN"/>
                    </StackPanel>
                </Window>
                
    
    [C#]
                <Window x:Class="Using_Non_Automated_Annotations_in_Run_Mode.Window1"
                           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                           Title="Using_Non_Automated_Annotations_in_Run_Mode" Height="300" Width="300"
                           Loaded="WindowLoaded"
                           xmlns:Leadtools_Windows_Controls="clr-namespace:Leadtools.Windows.Controls;assembly=Leadtools.Windows.Controls"
                    >
                    <StackPanel>
                       <RadioButton Name="rbDesignMode" GroupName="UserMode" Content="Design Mode" IsChecked="True" Checked="CheckedChanged"/>
                       <RadioButton Name="rbRunMode" GroupName="UserMode" Content="Run Mode" Checked="CheckedChanged"/>
                       <Separator/>
                       <Leadtools_Windows_Controls:BitmapSourceViewer x:Name="imageViewer1" Width="NaN" Height="NaN"/>
                    </StackPanel>
                </Window>
                
    
  7. Switch to Window1 code view (for VB project right-click Window1 in the solution explorer then select View Code and for C# project open Window1.xaml.cs file) and add the following lines at the beginning of the file: [Visual Basic]
                Imports Leadtools.Windows.Annotations
                
    
    [C#]
                using Leadtools.Windows.Annotations;
                
    
  8. Declare the following private enumeration: [Visual Basic]
                ' user mode
                Private Enum UserModeEnum
                   RunMode
                   DesignMode
                End Enum
                
    
    [C#]
                // user mode
                private enum UserModeEnum
                {
                   RunMode,
                   DesignMode
                }
                
    
  9. Declare the following private variables: [Visual Basic]
                ' Annotation container object
                Private annContainerObj As AnnContainer
                ' Current user mode
                Private currentUserMode As UserModeEnum
                
    
    [C#]
                // Annotation container object
                private AnnContainer annContainerObj;
                // Current user mode
                private UserModeEnum currentUserMode;
                
    
  10. Add the following functions code to class Window1: [Visual Basic]
                Private Sub WindowLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
                   ' load the main image into our viewer 
                   imageViewer1.Source = New BitmapImage(New Uri("C:\Users\Public\Documents\LEADTOOLS Images\eye.gif"))
                   ' we are in design mode now
                   currentUserMode = UserModeEnum.DesignMode
                   If (Not imageViewer1.Source Is Nothing) Then
                      ' initialize the AnnContainer object and associate it with rasterImageViewer1 image
                      annContainerObj = New AnnContainer()
                      annContainerObj.Width = imageViewer1.Source.Width
                      annContainerObj.Height = imageViewer1.Source.Height
                      ' set the annContainerObj as a content of BitmapSourceViewer.
                      imageViewer1.Content = annContainerObj
                      ' create Annotation Button Object and add it to the container
                      annContainerObj.Children.Add(CreateAnnButtonObject(New Rect(1, 1, annContainerObj.Width / 4, annContainerObj.Height / 4)))
                      ' create Annotation Note Object and add it to the container
                      annContainerObj.Children.Add(CreateAnnNoteObject(New Rect(annContainerObj.Width / 2, annContainerObj.Height / 2, (annContainerObj.Width / 4) - 1, (annContainerObj.Height / 4) - 1)))
                      'handle the container MouseLeftButtonDown event
                      AddHandler annContainerObj.MouseLeftButtonDown, New MouseButtonEventHandler(AddressOf annContainerObj_MouseLeftButtonDown)
                   End If
                End Sub
                Sub annContainerObj_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
                   If (imageViewer1.Source Is Nothing) Then
                      Return
                   End If
                   If (currentUserMode = UserModeEnum.RunMode) Then
                      Dim obj As AnnObject = annContainerObj.HitTest(e.GetPosition(annContainerObj))
                      If ((Not obj Is Nothing) AndAlso ((TypeOf obj Is AnnButtonObject) Or (TypeOf obj Is AnnNoteObject))) Then
                         If (TypeOf obj Is AnnButtonObject) Then
                            Dim buttonObj As AnnButtonObject = obj
                            buttonObj.IsPushed = True
                            HandleHyperLink(buttonObj)
                            buttonObj.IsPushed = False
                         Else
                            Dim annNoteObj As AnnNoteObject = obj
                            HandleHyperLink(annNoteObj)
                         End If
                      End If
                   End If
                End Sub
                Private Sub CheckedChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
                   UserModeChanged(sender)
                End Sub
                Private Sub UserModeChanged(ByVal sender As Object)
                   currentUserMode = IIf(sender Is rbDesignMode, UserModeEnum.DesignMode, UserModeEnum.RunMode)
                End Sub
                Private Function CreateAnnButtonObject(ByVal boundingRect As Rect) As AnnObject
                   Dim annButtonObj As AnnButtonObject = New AnnButtonObject()
                   annButtonObj.Text = "Click me"
                   annButtonObj.TextBrush = Brushes.Red
                   annButtonObj.Stroke = Brushes.Red
                   annButtonObj.StrokeThickness = 3
                   annButtonObj.Fill = Brushes.Yellow
                   annButtonObj.FontFamily = New FontFamily("Arial")
                   annButtonObj.FontSize = 14
                   annButtonObj.FontWeight = FontWeights.Bold
                   annButtonObj.Hyperlink = "www.leadtools.com"
                   annButtonObj.Left = boundingRect.Left
                   annButtonObj.Top = boundingRect.Top
                   annButtonObj.Width = boundingRect.Width
                   annButtonObj.Height = boundingRect.Height
                   Return annButtonObj
                End Function
                Private Function CreateAnnNoteObject(ByVal boundingRect As Rect) As AnnObject
                   Dim annNoteObj As AnnNoteObject = New AnnNoteObject()
                   annNoteObj.Text = "This is my Text"
                   annNoteObj.TextBrush = Brushes.Red
                   annNoteObj.Stroke = Brushes.Red
                   annNoteObj.StrokeThickness = 3
                   annNoteObj.Fill = Brushes.Yellow
                   annNoteObj.FontFamily = New FontFamily("Arial")
                   annNoteObj.FontSize = 14
                   annNoteObj.FontWeight = FontWeights.Bold
                   annNoteObj.Hyperlink = "Notepad.exe"
                   annNoteObj.Left = boundingRect.Left
                   annNoteObj.Top = boundingRect.Top
                   annNoteObj.Width = boundingRect.Width
                   annNoteObj.Height = boundingRect.Height
                   Return annNoteObj
                End Function
                Private Sub HandleHyperLink(ByVal obj As AnnObject)
                   If (Not obj.Hyperlink = String.Empty) Then
                      Try
                         System.Diagnostics.Process.Start(obj.Hyperlink)
                      Catch ex As Exception
                         MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation)
                      End Try
                   End If
                End Sub
                
    
    [C#]
                private void WindowLoaded(object sender, RoutedEventArgs e)
                {
                   // load the main image into our viewer 
                   imageViewer1.Source = new BitmapImage(new Uri(@"C:\Users\Public\Documents\LEADTOOLS Images\eye.gif"));
                   // we are in design mode now
                   currentUserMode = UserModeEnum.DesignMode;
                   if(imageViewer1.Source != null)
                   {
                      // initialize the AnnContainer object and associate it with rasterImageViewer1 image
                      annContainerObj = new AnnContainer();
                      annContainerObj.Width = imageViewer1.Source.Width;
                      annContainerObj.Height = imageViewer1.Source.Height;
                      
                      // set the annContainerObj as a content of BitmapSourceViewer.
                      imageViewer1.Content = annContainerObj;
                      
                      // create Annotation Button Object and add it to the container
                      annContainerObj.Children.Add(CreateAnnButtonObject(new Rect(1, 1, annContainerObj.Width / 4, annContainerObj.Height / 4)));
                      // create Annotation Note Object and add it to the container
                      annContainerObj.Children.Add(CreateAnnNoteObject(new Rect(annContainerObj.Width / 2, annContainerObj.Height / 2, (annContainerObj.Width / 4) - 1, (annContainerObj.Height / 4) - 1)));
                      //handle the container MouseLeftButtonDown event
                      annContainerObj.MouseLeftButtonDown += new MouseButtonEventHandler(annContainerObj_MouseLeftButtonDown);
                   }
                }
                void annContainerObj_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                {
                   if(imageViewer1.Source == null)
                      return;
                   if(currentUserMode == UserModeEnum.RunMode)
                   {
                      AnnObject obj = annContainerObj.HitTest(e.GetPosition(annContainerObj));
                      if(obj != null && ((obj is AnnButtonObject) || (obj is AnnNoteObject)))
                      {
                         if(obj is AnnButtonObject)
                         {
                            AnnButtonObject buttonObj = obj as AnnButtonObject;
                            buttonObj.IsPushed = true;
                            HandleHyperLink(buttonObj);
                            buttonObj.IsPushed = false;
                         }
                         else
                         {
                            AnnNoteObject annNoteObj = obj as AnnNoteObject;
                            HandleHyperLink(annNoteObj);
                         }
                      }
                   }
                }
                private void CheckedChanged(object sender, RoutedEventArgs e)
                {
                   UserModeChanged(sender);
                }
                private void UserModeChanged(object sender)
                {
                   currentUserMode = (sender == rbDesignMode) ? UserModeEnum.DesignMode : UserModeEnum.RunMode;
                }
                private AnnObject CreateAnnButtonObject(Rect boundingRect)
                {
                  AnnButtonObject annButtonObj = new AnnButtonObject();
                  annButtonObj.Text = "Click me";
                  annButtonObj.TextBrush = Brushes.Red;
                  annButtonObj.Stroke = Brushes.Red;
                  annButtonObj.StrokeThickness = 3;
                  annButtonObj.Fill = Brushes.Yellow;
                  annButtonObj.FontFamily = new FontFamily("Arial");
                  annButtonObj.FontSize = 14;
                  annButtonObj.FontWeight = FontWeights.Bold;
                  annButtonObj.Hyperlink = "www.leadtools.com";
                  annButtonObj.Left = boundingRect.Left;
                   annButtonObj.Top = boundingRect.Top;
                   annButtonObj.Width = boundingRect.Width;
                   annButtonObj.Height = boundingRect.Height;
                   return annButtonObj;
                }
                private AnnObject CreateAnnNoteObject(Rect boundingRect)
                {
                   AnnNoteObject annNoteObj = new AnnNoteObject();
                   annNoteObj.Text = "This is my Text";
                   annNoteObj.TextBrush = Brushes.Red;
                   annNoteObj.Stroke = Brushes.Red;
                   annNoteObj.StrokeThickness = 3;
                   annNoteObj.Fill = Brushes.Yellow;
                   annNoteObj.FontFamily = new FontFamily("Arial");
                   annNoteObj.FontSize = 14;
                   annNoteObj.FontWeight = FontWeights.Bold;
                   annNoteObj.Hyperlink = "Notepad.exe";
                   annNoteObj.Left = boundingRect.Left;
                   annNoteObj.Top = boundingRect.Top;
                  annNoteObj.Width = boundingRect.Width;
                   annNoteObj.Height = boundingRect.Height;
                   return annNoteObj;
                }
                private void HandleHyperLink(AnnObject obj)
                {
                   if(obj.Hyperlink != string.Empty)
                   {
                      try
                      {
                         System.Diagnostics.Process.Start(obj.Hyperlink);
                      }
                     catch(Exception ex)
                     {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                      }
                 }
                 }
                
    
  11. Build, and Run the program to test it. Click on the Note and Button annotations. Since the User Mode is Design Mode, nothing happens. Select Run Mode Radio button then click on each annotation.