LEADTOOLS Annotations (Leadtools.Annotations assembly)

IAnnAutomationControl Interface

Show in webframe
Example 





Members 
Support for user controls with the LEADTOOLS Annotation Automation.
Object Model
Syntax
public interface IAnnAutomationControl 
'Declaration
 
Public Interface IAnnAutomationControl 
'Usage
 
Dim instance As IAnnAutomationControl

            

            
public interface class IAnnAutomationControl 
Remarks

The IAnnAutomationControl lets you easily use any control you choose with LEADTOOLS Annotation Automation. By default, the automation framework provides supports for using a LEADTOOLS Leadtools.WinForms.RasterImageViewer control as the surface where the automation is used. This is accomplished by creating the AnnAutomation object using the AnnAutomation(AnnAutomationManager, RasterImageViewer) method and optionally, using AnnAutomation.AttachViewer and AnnAutomation.DetachViewer.

To perform automation, the toolkit must interact with the control in different ways, for example, obtain information about the control size and resolution, subscribe to the various mouse and keyboard events for user-interface integration, invalidate all or portions of the control surface for re-painting to draw the annotation objects and translate values between annotation and client coordinates.

When the above mode is used, LEADTOOLS will create an internal helper class that implements IAnnAutomationControl and provides translation between the methods/properties/events of the interface and those of Leadtools.WinForms.RasterImageViewer.

To instead use automation on your own custom control, you must create a class that implements IAnnAutomationControl and pass an instance of this class to AnnAutomation(AnnAutomationManager, IAnnAutomationControl) and optionally to AnnAutomation.Attach and AnnAutomation.Detach if more advanced functionality is required.

IAnnAutomationControl is designed so that you can easily use it to create an automation control from a class derived from the standard System.Windows.Forms.Control class. The names of the methods/properties/events are such that in a lot of cases, you do not need to provide an implementation to them, instead, they will be wired in automatically as described in the table below. The other members that are not usually defined by System.Windows.Forms.Control have the prefix "Automation" in their names to easily distinguish them from the standard members.

To use IAnnAutomationControl, you must implement all of the following members:

Member Description
IAnnAutomationControl.AutomationControl The System.Windows.Forms.Control to use for automation. This value cannot be a null reference (Nothing in Visual Basic). The automation object will use this value to add/remove child controls, invalidate its surface and use it many other ways.

IAnnAutomationControl.AutomationEnabled

Value that indicates whether the automation is enabled. When the value of this property is true, automation is enabled and when the user clicks and drags with the mouse or press keys on the keyboard, the automation object will process these events and perform the necessary actions (such as drawing new annotations objects or moving existing ones). If the value of this property is false, automation is disabled and the automation object will not process these events. You can use this property to easily enable/disable automation on this object from your user interface. If you decide to always enable automation, simply return true. You must trigger the IAnnAutomationControl.AutomationEnabledChanged event when the value of this property is changed.

IAnnAutomationControl.AutomationEnabledChanged

An event of type System.EventHandler that must trigger whenever the value of AutomationEnabled is changed. If your implementation does require enabling/disabling the automation, then you do not have to trigger this event from your code. When this event triggers, the automation object will query the IAnnAutomationControl.AutomationEnabled property and consequently starts or stops hooking the mouse and keyboard events.

IAnnAutomationControl.AutomationRasterImage

The Leadtools.RasterImage instance this automation will use when the user performs actions that will change the image being annotated. This is only used when the following methods are called: AnnEncryptObject.Apply, AnnRedactionObject.Realize and AnnRedactionObject.Restore. If you do not need to support these methods (By not supported encrypt and redaction objects, for example if you are implementing a system where only rectangles, rulers, images, audio and other objects like that are supported), then you can safely return a null reference (Nothing in Visual Basic) from your implementation of IAnnAutomationControl.

IAnnAutomationControl.AutomationImageWidth

The width of the image being annotated in pixels. Usually you will return the width of the image you are using in AutomationRasterImage (RasterImage.ImageWidth), however, since you do not need to implement that property and can safely return null in some case, you must always manually implement AutomationImageWidth to return the image width in pixels. In this case, the width of the virtual image you will be using in automation. You must trigger the IAnnAutomationControl.AutomationImageChanged event when the value of this property is changed.

IAnnAutomationControl.AutomationImageHeight

The height of the image being annotated in pixels. Usually you will return the height of the image you are using in AutomationRasterImage (RasterImage.ImageHeight), however, since you do not need to implement that property and can safely return null in some case, you must always manually implement AutomationImageHeight to return the image height in pixels. In this case, the height of the virtual image you will be using in automation. You must trigger the IAnnAutomationControl.AutomationImageChanged event when the value of this property is changed.

IAnnAutomationControl.AutomationImageDpiX

The horizontal resolution of the image being annotated in dots per inch. Usually you will return the resolution of the image you are using in AutomationRasterImage (RasterImage.XResolution), however, since you do not need to implement that property and can safely return null in some case, you must always manually implement AutomationImageDpiX to return the resolution in dots per inch. In this case, the resolution of the virtual image you will be using in automation or simply 0 to use the current screen resolution. You must trigger the IAnnAutomationControl.AutomationImageChanged event when the value of this property is changed.

IAnnAutomationControl.AutomationImageDpiY

The vertical resolution of the image being annotated in dots per inch. Usually you will return the resolution of the image you are using in AutomationRasterImage (RasterImage.YResolution), however, since you do not need to implement that property and can safely return null in some case, you must always manually implement AutomationImageDpiY to return the resolution in dots per inch. In this case, the resolution of the virtual image you will be using in automation or simply 0 to use the current screen resolution. You must trigger the IAnnAutomationControl.AutomationImageChanged event when the value of this property is changed.

IAnnAutomationControl.AutomationImageChanged

An event of type System.EventHandler that must trigger whenever the properties of the image being automated is changed. If you are using a Leadtools.RasterImage object, then you trigger this event whenever the RasterImage.Changed event occurs. Note that this event should occur when the image physical properties has changed, for example, its width/height or resolution or when a new image is set in your control (if supported) and not when the pixel data of the image has changed which only requires a re-paint. For these changes, use the IAnnAutomationControl.AutomationPaint event. If your requirement does not have an image that will be changed while being automated, then you do not have to trigger this event. When this event triggers, the automation object will query the IAnnAutomationControl.AutomationImageWidth, IAnnAutomationControl.AutomationImageHeightIAnnAutomationControl.AutomationImageDpiX and IAnnAutomationControl.AutomationImageDpiY properties to recalculate the size of the container.

IAnnAutomationControl.AutomationUseDpi

a value indicating whether the automation object should pay consideration to the image and control resolution when drawing annotation objects. The automation uses the resolution when translation values to pixels, for example, when calculating point size of a font. Also, objects such as rulers will use the resolution to correctly display the value in device independent coordinates such as inches and millimeters. If the value of this property is true, then the automation will use AutomationImageDpiX, AutomationImageDpiY, AutomationDpiX and AutomationDpiY to when calculating these values and ensure the result value on screen is accurate. If the value of this property is false, then the automation will not use any of these values and the calculations will be done using a default value of 96 dots per inch. You must trigger the IAnnAutomationControl.AutomationUseDpiChanged event when the value of this property is changed.

IAnnAutomationControl.AutomationUseDpiChanged

An event of type System.EventHandler that must trigger whenever the value of IAnnAutomationControl.AutomationUseDpi is changed. When this event triggers, the automation object will query this property to re-calculate the values needed.

IAnnAutomationControl.AutomationDpiX

The horizontal resolution of the control being annotated in dots per inch. Usually you will return the resolution of the control you are using in IAnnAutomationControl by obtaining it in a standard way in Windows.Forms. For example, using System.Windows.Forms.Control.CreateGraphics and returning Graphics.DpiX of the result object or simply 0 to use the current screen resolution.

IAnnAutomationControl.AutomationDpiY

The vertical resolution of the control being annotated in dots per inch. Usually you will return the resolution of the control you are using in IAnnAutomationControl by obtaining it in a standard way in Windows.Forms. For example, using System.Windows.Forms.Control.CreateGraphics and returning Graphics.DpiY of the result object or simply 0 to use the current screen resolution.

IAnnAutomationControl.AutomationTransform

A System.Drawing.Drawing2D.Matrix object that represents the current transformation for the annotations objects. This value cannot be a null reference (Nothing in Visual Basic) and the object returned will be owned by the automation object and will be disposed internally when it is no longer needed. You must set this matrix with the current scroll and zoom values of your control and set up your application to either create or return a new instance of this matrix on demand whenever this property is called. For more explanation, refer to the example source code of this class. If you will not support scrolling or zooming in your application, then simply return an identity matrix (a new System.Drawing.Drawing2D.Matrix object). You must trigger the IAnnAutomationControl.AutomationTransformChanged when the value of this property is changed.

IAnnAutomationControl.AutomationTransformChanged

An event of type System.EventHandler that must trigger whenever the value of IAnnAutomationControl.AutomationTransform is changed - that is, when your control is scrolled or zoomed in or out. When this event is triggered, the automation control will query the value of this property and might re-paint the annotations objects. Notice that you must trigger this event with the size of the control is changed so the automation object will use the new value of IAnnAutomationControl.ClientRectangle.

IAnnAutomationControl.AutomationPaint

An event of type System.Windows.Forms.PaintEventArgs that must trigger whenever the surface of the control is invalidated. The automation control will then automatically use the values of the event arguments to draw the annotations objects. In most cases, you simply have to trigger this event in your control System.Windows.Forms.Control.Paint event or System.Windows.Forms.Control.OnPaint method and pass the same arguments (of type System.Windows.Forms.PaintEventArgs) to IAnnAutomationControl.AutomationPaint.

IAnnAutomationControl.AutomationAttach

A method that will be called whenever AnnAutomation.Attach is called. You can add code to this method to handle any necessary code needed.

IAnnAutomationControl.AutomationDetach

A method that will be called whenever AnnAutomation.Detach is called. You can add code to this method to handle any necessary code needed.

IAnnAutomationControl.Invalidate

A mathod that will be called when the automation needs to re-paint the control. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this method (since the default System.Windows.Forms.Control.Invalidate will be used). If you have custom implementation, then simply invalidate your control surface when this method is called.

IAnnAutomationControl.PointToClient

A method that will be called when the automation needs to transform a point from screen to client coordinates. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this method (since the default System.Windows.Forms.Control.PointToClient will be used).

IAnnAutomationControl.PointToScreen

A method that will be called when the automation needs to transform a point from client to screen coordinates. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this method (since the default System.Windows.Forms.Control.PointToScreen will be used).

IAnnAutomationControl.RectangleToScreen

A method that will be called when the automation needs to transform a rectangle from client to screen coordinates. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this method (since the default System.Windows.Forms.Control.RectangleToScreen will be used).

IAnnAutomationControl.ClientRectangle

A rectangle that represents the client area of the control. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this property (since the default System.Windows.Forms.Control.ClientRectangle will be used). However, you must trigger the IAnnAutomationControl.AutomationTransformChanged event with the value of the client rectangle is changed, in most cases, by calling IAnnAutomationControl.AutomationTransformChanged from your System.Windows.Forms.Control.SizeChanged event or System.Windows.Forms.Control.OnSizeChanged method.

IAnnAutomationControl.Cursor

Gets or sets the cursor to be used in the control. The automation will use many different cursors depending on current operation (drawing new annotations, moving existing annotations, etc). If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this property (since the default System.Windows.Forms.Control.Cursor will be used)

IAnnAutomationControl.MouseDown

An event of type System.Windows.Forms.MouseEventHandler that must trigger when the mouse is pressed on the control. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this event (since the default System.Windows.Forms.Control.MouseDown will be used)

IAnnAutomationControl.MouseMove

An event of type System.Windows.Forms.MouseEventHandler that must trigger when the mouse is moved on the surface of the control. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this event (since the default System.Windows.Forms.Control.MouseMove will be used)

IAnnAutomationControl.MouseUp

An event of type System.Windows.Forms.MouseEventHandler that must trigger when the mouse is depressed on the surface of the control. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this event (since the default System.Windows.Forms.Control.MouseUp will be used)

IAnnAutomationControl.DoubleClick

An event of type System.EventHandler that must trigger when the mouse is double clicked on the surface of the control. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this event (since the default System.Windows.Forms.Control.DoubleClick will be used)

IAnnAutomationControl.KeyDown

An event of type System.Windows.Forms.KeyEventHandler that must trigger when a key is pressed when your control has focus. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this event (since the default System.Windows.Forms.Control.KeyDown will be used)

IAnnAutomationControl.GotFocus

An event of type System.EventHandler that must trigger when your control receives the focus. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this event (since the default System.Windows.Forms.Control.GotFocus will be used)

IAnnAutomationControl.LostFocus

An event of type System.EventHandler that must trigger when your control loses the focus. If your control is derived from the standard System.Windows.Forms.Control, then you do not need to implement this event (since the default System.Windows.Forms.Control.LostFocus will be used)

Example
Copy Code  
Imports Leadtools
Imports Leadtools.Annotations

' Our custom image viewer. It can view a System.Drawing.Image with
' the following options:
' 1. Scrolling
' 2. Zoom in/out
' 3. Let the user draw annotation objects on the image surface using
'   LEADTOOLS annotations in automation mode
Public Class MyImageViewer
   Inherits ScrollableControl
   Implements IAnnAutomationControl
   ' The LEADTOOLS automation manager object
   Private _automationManager As AnnAutomationManager
   ' The LEADTOOLS automation object for this control
   Private _automation As AnnAutomation
   ' The image we are viewing
   Private _image As Image
   ' Scale factor (for zooming)
   Private _scaleFactor As Single = 100.0F

   Public Sub New()
      ' Enable double buffering for smooth paint
      SetStyle( _
         ControlStyles.AllPaintingInWmPaint Or _
         ControlStyles.UserPaint Or _
         ControlStyles.ResizeRedraw Or _
         ControlStyles.UserPaint Or _
         ControlStyles.DoubleBuffer, True)

      ' Initialize LEADTOOLS annotations automation
      ' Default everything
      _automationManager = New AnnAutomationManager()
      _automationManager.RedactionRealizePassword = ""
      _automationManager.UseXPStyleToolBar = True
      _automationManager.CreateDefaultObjects()
      _automationManager.CreateToolBar()
      _automationManager.ToolBar.Dock = DockStyle.Right
      _automationManager.ToolBar.BringToFront()
      _automationManager.ToolBar.AutoSize = False
      _automationManager.ToolBar.Appearance = ToolBarAppearance.Flat

      ' Create the automation object for this viewer
      _automation = New AnnAutomation(_automationManager, Me)
      ' We are the only and only active automation in this demo
      _automation.Active = True
      ' Add it to the automation manager
      _automationManager.Automations.Add(_automation)
   End Sub

   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         ' Clean up
         If Not _automation Is Nothing Then
            _automation.Dispose()
         End If

         If Not _automationManager Is Nothing Then
            _automationManager.Dispose()
         End If

         If Not _image Is Nothing Then
            _image.Dispose()
         End If
      End If

      MyBase.Dispose(disposing)
   End Sub

   ' Access to the annotations toolbar
   <Browsable(False)> _
   Public ReadOnly Property AutomationToolbar() As ToolBar
      Get
         Return _automationManager.ToolBar
      End Get
   End Property

   ' The image to view
   <Description("The image to view"), Category("Appearance"), DefaultValue(CType(Nothing, Image))> _
   Public Property Image() As Image
      Get
         Return _image
      End Get
      Set(ByVal value As Image)
         If Not _image Is value Then
            If Not _image Is Nothing Then
               _image.Dispose()
            End If

            _image = value

            ' Re-set the scale factor
            _scaleFactor = 100.0F
            ' Re-calculate scrollbar sizes
            RecalculateScrollbars()
            ' Inform automation that the image has changed
            RaiseEvent AutomationImageChanged(Me, EventArgs.Empty)
            ' Re-paint
            Invalidate()
         End If
      End Set
   End Property

   ' Scale factor (zooming)
   <Description("Scale factor used for zooming."), Category("Appearance"), DefaultValue(100.0F)> _
   Public Property ScaleFactor() As Single
      Get
         Return _scaleFactor
      End Get
      Set(ByVal value As Single)
         If _scaleFactor <> value Then
            If _scaleFactor <= 0 Then Throw New ArgumentOutOfRangeException("ScaleFactor", "Must be a value greater than zero")

            _scaleFactor = value

            ' Re-calculate scrollbar sizes
            RecalculateScrollbars()
            ' Inform automation that transformation has changed
            RaiseEvent AutomationTransformChanged(Me, Nothing)
            ' Re-paint
            Invalidate()
         End If
      End Set
   End Property

   ' Calculate the scrollbar sizes
   Private Sub RecalculateScrollbars()
      Dim scrollSize As SizeF

      If Not _image Is Nothing Then
         Dim scale As Single = _scaleFactor / 100.0F
         scrollSize = New SizeF(_image.Width * scale, _image.Height * scale)
      Else
         scrollSize = SizeF.Empty
      End If

      AutoScrollMargin = Size
      AutoScrollMinSize = New Size(CType(scrollSize.Width + 0.5F, Integer), CType(scrollSize.Height + 0.5F, Integer))
   End Sub

   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
      If Not _image Is Nothing Then
         ' Paint the image
         Dim graphics As Graphics = e.Graphics
         Dim x As Single = AutoScrollPosition.X
         Dim y As Single = AutoScrollPosition.Y
         Dim width As Single = _image.Width * _scaleFactor / 100.0F
         Dim height As Single = _image.Height * _scaleFactor / 100.0F
         graphics.DrawImage(_image, x, y, width, height)

         ' Paint annotations
         RaiseEvent AutomationPaint(Me, e)
      End If
      MyBase.OnPaint(e)
   End Sub

   Const WM_HSCROLL As Integer = &H114
   Const WM_VSCROLL As Integer = &H115
   Const WM_MOUSEWHEEL As Integer = &H20A

   Protected Overrides Sub WndProc(ByRef m As Message)
      If m.Msg = WM_HSCROLL OrElse m.Msg = WM_VSCROLL OrElse m.Msg = WM_MOUSEWHEEL Then
         ' Inform automation that transformation has changed
         RaiseEvent AutomationTransformChanged(Me, Nothing)

         ' And paint
         Invalidate()
      End If

      MyBase.WndProc(m)
   End Sub

   ' IAnnAutomationControl Members

   Public ReadOnly Property AutomationRasterImage() As RasterImage Implements IAnnAutomationControl.AutomationRasterImage
      Get
         Return Nothing
      End Get
   End Property

   Public ReadOnly Property AutomationImageWidth() As Single Implements IAnnAutomationControl.AutomationImageWidth
      Get
         If Not _image Is Nothing Then
            Return _image.Width
         Else
            Return 0
         End If
      End Get
   End Property

   Public ReadOnly Property AutomationImageHeight() As Single Implements IAnnAutomationControl.AutomationImageHeight
      Get
         If Not _image Is Nothing Then
            Return _image.Height
         Else
            Return 0
         End If
      End Get
   End Property

   Public ReadOnly Property AutomationDpiX() As Single Implements IAnnAutomationControl.AutomationDpiX
      Get
         Return 96
      End Get
   End Property

   Public ReadOnly Property AutomationDpiY() As Single Implements IAnnAutomationControl.AutomationDpiY
      Get
         Return 96
      End Get
   End Property

   Public ReadOnly Property AutomationImageDpiX() As Single Implements IAnnAutomationControl.AutomationImageDpiX
      Get
         Return 96
      End Get
   End Property

   Public ReadOnly Property AutomationImageDpiY() As Single Implements IAnnAutomationControl.AutomationImageDpiY
      Get
         Return 96
      End Get
   End Property

   Public ReadOnly Property AutomationTransform() As Matrix Implements IAnnAutomationControl.AutomationTransform
      Get
         Dim m As New Matrix()

         If Not _image Is Nothing Then
            ' Create a Matrix for our transform (scroll position and current scale)
            m.Translate(AutoScrollPosition.X, AutoScrollPosition.Y)
            Dim scale As Single = _scaleFactor / 100.0F
            m.Scale(scale, scale)
         End If

         Return m
      End Get
   End Property

   Public ReadOnly Property AutomationUseDpi() As Boolean Implements IAnnAutomationControl.AutomationUseDpi
      Get
         Return False
      End Get
   End Property

   Public ReadOnly Property AutomationControl() As Control Implements IAnnAutomationControl.AutomationControl
      Get
         Return Me
      End Get
   End Property

   Public ReadOnly Property AutomationEnabled() As Boolean Implements IAnnAutomationControl.AutomationEnabled
      Get
         Return Me.Enabled
      End Get
   End Property

   Public Event AutomationImageChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAnnAutomationControl.AutomationImageChanged
   Public Event AutomationTransformChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAnnAutomationControl.AutomationTransformChanged
   Public Event AutomationUseDpiChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAnnAutomationControl.AutomationUseDpiChanged
   Public Event AutomationEnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAnnAutomationControl.AutomationEnabledChanged
   Public Event AutomationPaint(ByVal sender As Object, ByVal e As PaintEventArgs) Implements IAnnAutomationControl.AutomationPaint

   Public Sub AutomationAttach() Implements IAnnAutomationControl.AutomationAttach
   End Sub

   Public Sub AutomationDetach() Implements IAnnAutomationControl.AutomationDetach
   End Sub

   Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)
      RaiseEvent AutomationEnabledChanged(Me, e)
      MyBase.OnEnabledChanged(e)
   End Sub

   Public Sub Invalidate_Impl(ByVal rc As Rectangle) Implements IAnnAutomationControl.Invalidate
      Me.Invalidate()
   End Sub

   Public Function PointToClient_Impl(ByVal point As Point) As Point Implements IAnnAutomationControl.PointToClient
      Return Me.PointToClient(point)
   End Function

   Public Function PointToScreen_Impl(ByVal point As Point) As Point Implements IAnnAutomationControl.PointToScreen
      Return Me.PointToScreen(point)
   End Function

   Public Function RectangleToScreen_Impl(ByVal rect As Rectangle) As Rectangle Implements IAnnAutomationControl.RectangleToScreen
      Return Me.RectangleToScreen(rect)
   End Function

   Public ReadOnly Property ClientRectangle_Impl() As Rectangle Implements IAnnAutomationControl.ClientRectangle
      Get
         Return Me.ClientRectangle
      End Get
   End Property

   Public Property Cursor_Impl() As Cursor Implements IAnnAutomationControl.Cursor
      Get
         Return Me.Cursor
      End Get
      Set(ByVal value As Cursor)
         Me.Cursor = value
      End Set
   End Property

   Public Event MouseDown_Impl(ByVal sender As Object, ByVal e As MouseEventArgs) Implements IAnnAutomationControl.MouseDown
   Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
      RaiseEvent MouseDown_Impl(Me, e)
      MyBase.OnMouseDown(e)
   End Sub

   Public Event MouseUp_Impl(ByVal sender As Object, ByVal e As MouseEventArgs) Implements IAnnAutomationControl.MouseUp
   Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
      RaiseEvent MouseUp_Impl(Me, e)
      MyBase.OnMouseUp(e)
   End Sub

   Public Event MouseMove_Impl(ByVal sender As Object, ByVal e As MouseEventArgs) Implements IAnnAutomationControl.MouseMove
   Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
      RaiseEvent MouseMove_Impl(Me, e)
      MyBase.OnMouseMove(e)
   End Sub

   Public Event DoubleClick_Impl(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAnnAutomationControl.DoubleClick
   Protected Overrides Sub OnDoubleClick(ByVal e As System.EventArgs)
      RaiseEvent DoubleClick_Impl(Me, e)
      MyBase.OnDoubleClick(e)
   End Sub

   Public Event KeyDown_Impl(ByVal sender As Object, ByVal e As KeyEventArgs) Implements IAnnAutomationControl.KeyDown
   Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
      RaiseEvent KeyDown_Impl(Me, e)
      MyBase.OnKeyDown(e)
   End Sub

   Public Event GotFocus_Impl(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAnnAutomationControl.GotFocus
   Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
      RaiseEvent GotFocus_Impl(Me, e)
      MyBase.OnGotFocus(e)
   End Sub

   Public Event LostFocus_Impl(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAnnAutomationControl.LostFocus
   Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
      RaiseEvent LostFocus_Impl(Me, e)
      MyBase.OnLostFocus(e)
   End Sub
End Class
using Leadtools;
using Leadtools.Annotations;

// Our custom image viewer. It can view a System.Drawing.Image with
// the following options:
// 1. Scrolling
// 2. Zoom in/out
// 3. Let the user draw annotation objects on the image surface using
//   LEADTOOLS annotations in automation mode
public class MyImageViewer : ScrollableControl, IAnnAutomationControl
{
   // The LEADTOOLS automation manager object
   private AnnAutomationManager _automationManager;
   // The LEADTOOLS automation object for this control
   private AnnAutomation _automation;
   // The image we are viewing
   private Image _image;
   // Scale factor (for zooming)
   private float _scaleFactor = 100.0f;
   public MyImageViewer()
   {
      // Enable double buffering for smooth paint
      SetStyle(
         ControlStyles.AllPaintingInWmPaint |
         ControlStyles.UserPaint |
         ControlStyles.ResizeRedraw |
         ControlStyles.UserPaint |
         ControlStyles.DoubleBuffer, true);

      // Initialize LEADTOOLS annotations automation
      // Default everything
      _automationManager = new AnnAutomationManager();
      _automationManager.RedactionRealizePassword = "";
      _automationManager.UseXPStyleToolBar = true;
      _automationManager.CreateDefaultObjects();
      _automationManager.CreateToolBar();
      _automationManager.ToolBar.Dock = DockStyle.Right;
      _automationManager.ToolBar.BringToFront();
      _automationManager.ToolBar.AutoSize = false;
      _automationManager.ToolBar.Appearance = ToolBarAppearance.Flat;

      // Create the automation object for this viewer
      _automation = new AnnAutomation(_automationManager, this);
      // We are the only and only active automation in this demo
      _automation.Active = true;
      // Add it to the automation manager
      _automationManager.Automations.Add(_automation);

      // To get rid of a warning (not used)
      if(AutomationUseDpiChanged != null)
      {
         AutomationUseDpiChanged(this, EventArgs.Empty);
      }
   }

   protected override void Dispose(bool disposing)
   {
      if(disposing)
      {
         // Clean up
         if(_automation != null)
            _automation.Dispose();

         if(_automationManager != null)
            _automationManager.Dispose();

         if(_image != null)
            _image.Dispose();
      }

      base.Dispose(disposing);
   }

   // Access to the annotations toolbar
   [Browsable(false)]
   public ToolBar AutomationToolbar
   {
      get
      {
         return _automationManager.ToolBar;
      }
   }

   // The image to view
   [Description("The image to view"),
   Category("Appearance"),
   DefaultValue(null)]
   public Image Image
   {
      get
      {
         return _image;
      }
      set
      {
         if(_image != value)
         {
            if(_image != null)
               _image.Dispose();

            _image = value;

            // Re-set the scale factor
            _scaleFactor = 100.0f;
            // Re-calculate scrollbar sizes
            RecalculateScrollbars();
            // Inform automation that the image has changed
            if(AutomationImageChanged != null)
               AutomationImageChanged(this, EventArgs.Empty);
            // Re-paint
            Invalidate();
         }
      }
   }

   // Scale factor (zooming)
   [Description("Scale factor used for zooming."),
   Category("Appearance"),
   DefaultValue(100.0f)]
   public float ScaleFactor
   {
      get
      {
         return _scaleFactor;
      }
      set
      {
         if(_scaleFactor != value)
         {
            if(_scaleFactor <= 0)
               throw new ArgumentOutOfRangeException("ScaleFactor", "Must be a value greater than zero");

            _scaleFactor = value;

            // Re-calculate scrollbar sizes
            RecalculateScrollbars();
            // Inform automation that transformation has changed
            if(AutomationTransformChanged != null)
               AutomationTransformChanged(this, null);
            // Re-paint
            Invalidate();
         }
      }
   }

   // Calculate the scrollbar sizes
   private void RecalculateScrollbars()
   {
      SizeF scrollSize;

      if(_image != null)
      {
         float scale = _scaleFactor / 100.0f;
         scrollSize = new SizeF(_image.Width * scale, _image.Height * scale);
      }
      else
      {
         scrollSize = SizeF.Empty;
      }

      AutoScrollMargin = Size;
      AutoScrollMinSize = new Size((int)(scrollSize.Width + 0.5f), (int)(scrollSize.Height + 0.5F));
   }

   protected override void OnPaint(PaintEventArgs e)
   {
      if(_image != null)
      {
         // Paint the image
         Graphics graphics = e.Graphics;
         float x = AutoScrollPosition.X;
         float y = AutoScrollPosition.Y;
         float width = _image.Width * _scaleFactor / 100.0f;
         float height = _image.Height * _scaleFactor / 100.0f;
         graphics.DrawImage(_image, x, y, width, height);

         // Paint annotations
         if(AutomationPaint != null)
            AutomationPaint(this, e);
      }

      base.OnPaint(e);
   }

   private const int WM_HSCROLL = 0x0114;
   private const int WM_VSCROLL = 0x0115;
   private const int WM_MOUSEWHEEL = 0x020A;

   protected override void WndProc(ref Message m)
   {
      if(m.Msg == WM_HSCROLL || m.Msg == WM_VSCROLL || m.Msg == WM_MOUSEWHEEL)
      {
         // Inform automation that transformation has changed
         if(AutomationTransformChanged != null)
            AutomationTransformChanged(this, null);

         // And paint
         Invalidate();
      }

      base.WndProc(ref m);
   }

   #region IAnnAutomationControl Members

   RasterImage IAnnAutomationControl.AutomationRasterImage
   {
      get { return null; }
   }

   float IAnnAutomationControl.AutomationImageWidth
   {
      get { return (_image != null) ? _image.Width : 0; }
   }

   float IAnnAutomationControl.AutomationImageHeight
   {
      get { return (_image != null) ? _image.Height : 0; }
   }

   float IAnnAutomationControl.AutomationDpiX
   {
      get { return 96; }
   }

   float IAnnAutomationControl.AutomationDpiY
   {
      get { return 96; }
   }

   float IAnnAutomationControl.AutomationImageDpiX
   {
      get { return 96; }
   }

   float IAnnAutomationControl.AutomationImageDpiY
   {
      get { return 96; }
   }

   Matrix IAnnAutomationControl.AutomationTransform
   {
      get
      {
         Matrix m = new Matrix();

         if(_image != null)
         {
            // Create a Matrix for our transform (scroll position and current scale)
            m.Translate(AutoScrollPosition.X, AutoScrollPosition.Y);
            float scale = _scaleFactor / 100.0f;
            m.Scale(scale, scale);
         }

         return m;
      }
   }

   bool IAnnAutomationControl.AutomationUseDpi
   {
      get { return false; }
   }

   Control IAnnAutomationControl.AutomationControl
   {
      get { return this; }
   }

   bool IAnnAutomationControl.AutomationEnabled
   {
      get { return Enabled; }
   }

   public event EventHandler AutomationImageChanged;
   public event EventHandler AutomationTransformChanged;
   public event EventHandler AutomationUseDpiChanged;
   public event EventHandler AutomationEnabledChanged;
   public event PaintEventHandler AutomationPaint;

   void IAnnAutomationControl.AutomationAttach()
   {
   }

   void IAnnAutomationControl.AutomationDetach()
   {
   }

   protected override void OnEnabledChanged(EventArgs e)
   {
      if(AutomationEnabledChanged != null)
         AutomationEnabledChanged(this, e);

      base.OnEnabledChanged(e);
   }

   // The following members of IAnnAutomationControl
   // Must be provided by the implementer. Since the implementer
   // in our example derives from Control, these properties/methods/events
   // are already implemented by the base Control class. We do not have special
   // code to perform in this case, otherwise, we could have overridden
   // the base class implementation with ours.
   /*
   void Invalidate(Rectangle rc);
   Point PointToClient(Point point);
   Rectangle RectangleToScreen(Rectangle rect);
   Point PointToScreen(Point pt);
   Rectangle ClientRectangle { get; }
   Cursor Cursor { get; set; }
   event MouseEventHandler MouseDown;
   event MouseEventHandler MouseMove;
   event MouseEventHandler MouseUp;
   event EventHandler DoubleClick;
   event KeyEventHandler KeyDown;
   event EventHandler LostFocus;
   event EventHandler GotFocus;
   */

   #endregion IAnnAutomationControl Members
}
Requirements

Target Platforms

See Also

Reference

IAnnAutomationControl Members
Leadtools.Annotations Namespace

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.