(Leadtools.Controls)

フレームを表示

ImageViewerInteractiveModeクラス




メンバ 
ImageViewerのリッチなユーザーエクスペリエンス機能の基本クラス。
オブジェクトモデル
構文
public abstract class ImageViewerInteractiveMode 
'Declaration
 
Public MustInherit Class ImageViewerInteractiveMode 
解説

ImageViewerInteractiveModeは、ユーザーがマウスやタッチでビューワを操作するときにImageViewerのInteractiveServiceオブジェクトと連携してリッチなユーザーインターフェースを提供します。

ImageViewerInteractiveModeは抽象基本クラスであり、このクラスからビューワのカスタム操作を処理する独自のクラスを派生できます。LEADTOOLSが提供し追加設定の必要ないモードのリストについては、「画像ビューワの対話モード」を参照してください。

ImageViewerInteractiveModeには、以下の機能性があります:

メンバ 説明
WorkingCursorIdleCursorHitTestStateCursor

対話モードがアイドル、作業ヒット-テスト状態(デスクトップブラウザについては)であるとき、使うカーソル。

MouseButtons

マウスボタンは、このモード(デスクトップブラウザについては)に関連づけられます

対話モードを使うために、派生クラスのどれのインスタンスでも作成して、以下のメソッドの1つを用いて、それをビューワに割り当てます:

同時に複数の対話モードを使うことができます。たとえば、右ボタンで左マウスボタンと拡大鏡でパニング/ズームをサポートするために、以下のcodeを使います:

サンプル

このサンプルは、ビューワ上でユーザーがクリックまたはタッチしてドラッグする際に画像を回転するカスタムImageViewerInteractiveModeを作成します。

Copy Code  
Imports Leadtools
Imports Leadtools.Controls
Imports Leadtools.Codecs
Imports Leadtools.Drawing
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Color

Private Class TransformInteractiveMode : Inherits ImageViewerInteractiveMode
   Public Sub New()
      MyBase.New()
      Me.AutoItemMode = ImageViewerAutoItemMode.AutoSet
   End Sub
   Private _scaleKeyModifier As Keys = Keys.Control
   Public Overridable Property ScaleKeyModifier() As Keys
      Get
         Return _scaleKeyModifier
      End Get
      Set(value As Keys)
         ' Supported is none and any modifiers
         Select Case value
            Case Keys.None, Keys.Alt, Keys.Shift, Keys.Control
               _scaleKeyModifier = value

            Case Else
               Throw New ArgumentException("Invalid value", "ScaleKeyModifier")
         End Select
      End Set
   End Property

   Private _rotateKeyModifier As Keys = Keys.Alt
   Public Overridable Property RotateKeyModifier() As Keys
      Get
         Return _rotateKeyModifier
      End Get
      Set(value As Keys)
         ' Supported is none and any modifiers
         Select Case value
            Case Keys.None, Keys.Alt, Keys.Shift, Keys.Control
               _rotateKeyModifier = value

            Case Else
               Throw New ArgumentException("Invalid value", "PageKeyModifier")
         End Select
      End Set
   End Property

   Public Overrides ReadOnly Property Name() As String
      Get
         Return "Transform"
      End Get
   End Property

   Public Overrides ReadOnly Property Id() As Integer
      Get
         Return ImageViewerInteractiveMode.UserModeId + 3
      End Get
   End Property

   Public Overrides Sub Start(ByVal imageViewer As ImageViewer)
      MyBase.Start(imageViewer)
      Dim service As InteractiveService = MyBase.InteractiveService

      ' Pan required events
      AddHandler service.DragStarted, AddressOf service_DragStarted
      AddHandler service.DragDelta, AddressOf service_DragDelta
      AddHandler service.DragCompleted, AddressOf service_DragCompleted
      AddHandler service.DoubleTap, AddressOf service_DoubleTap
   End Sub

   Public Overrides Sub [Stop](ByVal imageViewer As ImageViewer)
      If IsStarted Then
         Dim service As InteractiveService = MyBase.InteractiveService

         RemoveHandler service.DragStarted, AddressOf service_DragStarted
         RemoveHandler service.DragDelta, AddressOf service_DragDelta
         RemoveHandler service.DragCompleted, AddressOf service_DragCompleted
         RemoveHandler service.DoubleTap, AddressOf service_DoubleTap

         MyBase.Stop(imageViewer)
      End If
   End Sub

   Private Sub service_DragStarted(ByVal sender As Object, ByVal e As InteractiveDragStartedEventArgs)
      ' Make sure pinch is not working, otherwise, ignore pan
      If (Not Me.CanStartWork(e)) Then
         Return
      End If

      ' Make sure we are on an item
      If Me.Item Is Nothing Then
         Return
      End If

      e.IsHandled = True
      Me.OnWorkStarted(EventArgs.Empty)
   End Sub

   Private Sub service_DragDelta(ByVal sender As Object, ByVal e As InteractiveDragDeltaEventArgs)
      If (Not Me.IsWorking) Then
         Return
      End If

      Dim item As ImageViewerItem = Me.Item
      If item Is Nothing Then
         Return
      End If

      ' Find out what to do
      Dim dx As Integer = e.Change.X
      Dim dy As Integer = e.Change.Y

      If dx = 0 AndAlso dy = 0 Then
         Return
      End If

      Dim scaleKey As Keys = Me.ScaleKeyModifier
      Dim rotateKey As Keys = Me.RotateKeyModifier
      Dim doScale As Boolean = (Control.ModifierKeys And scaleKey) = scaleKey
      Dim doRotate As Boolean = (Control.ModifierKeys And rotateKey) = rotateKey

      Dim imageViewer As ImageViewer = Me.ImageViewer

      If doScale Then
         Scale(imageViewer, item, dy, e.Origin)
      ElseIf doRotate Then
         Rotate(imageViewer, item, dx, e.Origin)
      Else
         Translate(imageViewer, item, dx, dy)
      End If

      e.IsHandled = True
   End Sub

   Private Sub service_DragCompleted(ByVal sender As Object, ByVal e As InteractiveDragCompletedEventArgs)
      If (Not Me.IsWorking) Then
         Return
      End If

      e.IsHandled = True
      OnWorkCompleted(EventArgs.Empty)
   End Sub

   Private Sub service_DoubleTap(ByVal sender As Object, ByVal e As InteractiveEventArgs)
      ' Make sure we not working already
      If Me.IsWorking OrElse (Not Me.CanStartWork(e)) Then
         Return
      End If

      ' Make sure we have an item
      Dim item As ImageViewerItem = Me.Item
      If item Is Nothing Then
         Return
      End If

      e.IsHandled = True

      Me.OnWorkStarted(EventArgs.Empty)
      Identity(Me.ImageViewer, item)
      Me.OnWorkCompleted(EventArgs.Empty)
   End Sub

   Private Sub Identity(ByVal imageViewer As ImageViewer, ByVal item As ImageViewerItem)
      item.Transform = LeadMatrix.Identity
   End Sub

   Private Sub Scale(ByVal imageViewer As ImageViewer, ByVal item As ImageViewerItem, ByVal dy As Integer, ByVal position As LeadPoint)
      ' ...
      ' set scale code
      ' ...
   End Sub

   Private Sub Rotate(ByVal imageViewer As ImageViewer, ByVal item As ImageViewerItem, ByVal dx As Integer, ByVal position As LeadPoint)
      ' ...
      ' set rotate code
      ' ...
   End Sub

   Private Sub Translate(ByVal imageViewer As ImageViewer, ByVal item As ImageViewerItem, ByVal dx As Integer, ByVal dy As Integer)
      ' ...
      ' set translate code
      ' ...
   End Sub
End Class

<TestMethod>
Public Sub ImageViewerInteractiveMode_Example()
   _imageViewer.InteractiveModes.BeginUpdate()
   Dim mode As New TransformInteractiveMode
   mode.IsEnabled = False
   _imageViewer.InteractiveModes.Add(mode)
   '...
   '...
   '...
   _imageViewer.InteractiveModes.EndUpdate()
End Sub
using Leadtools;
using Leadtools.Controls;
using Leadtools.Codecs;
using Leadtools.Drawing;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;

private class TransformInteractiveMode : ImageViewerInteractiveMode
{
   public TransformInteractiveMode() :
      base()
   {
      this.AutoItemMode = ImageViewerAutoItemMode.AutoSet;
   }
   private Keys _scaleKeyModifier = Keys.Control;
   public virtual Keys ScaleKeyModifier
   {
      get { return _scaleKeyModifier; }
      set
      {
         // Supported is none and any modifiers
         switch (value)
         {
            case Keys.None:
            case Keys.Alt:
            case Keys.Shift:
            case Keys.Control:
               _scaleKeyModifier = value;
               break;

            default:
               throw new ArgumentException("Invalid value", "ScaleKeyModifier");
         }
      }
   }

   private Keys _rotateKeyModifier = Keys.Alt;
   public virtual Keys RotateKeyModifier
   {
      get { return _rotateKeyModifier; }
      set
      {
         // Supported is none and any modifiers
         switch (value)
         {
            case Keys.None:
            case Keys.Alt:
            case Keys.Shift:
            case Keys.Control:
               _rotateKeyModifier = value;
               break;

            default:
               throw new ArgumentException("Invalid value", "PageKeyModifier");
         }
      }
   }

   public override string Name
   {
      get { return "Transform"; }
   }

   public override int Id
   {
      get { return ImageViewerInteractiveMode.UserModeId + 3; }
   }

   public override void Start(ImageViewer imageViewer)
   {
      base.Start(imageViewer);
      var service = base.InteractiveService;

      // Pan required events
      service.DragStarted += new EventHandler<InteractiveDragStartedEventArgs>(service_DragStarted);
      service.DragDelta += new EventHandler<InteractiveDragDeltaEventArgs>(service_DragDelta);
      service.DragCompleted += new EventHandler<InteractiveDragCompletedEventArgs>(service_DragCompleted);
      service.DoubleTap += new EventHandler<InteractiveEventArgs>(service_DoubleTap);
   }

   public override void Stop(ImageViewer imageViewer)
   {
      if (IsStarted)
      {
         var service = base.InteractiveService;

         service.DragStarted -= new EventHandler<InteractiveDragStartedEventArgs>(service_DragStarted);
         service.DragDelta -= new EventHandler<InteractiveDragDeltaEventArgs>(service_DragDelta);
         service.DragCompleted -= new EventHandler<InteractiveDragCompletedEventArgs>(service_DragCompleted);
         service.DoubleTap -= new EventHandler<InteractiveEventArgs>(service_DoubleTap);

         base.Stop(imageViewer);
      }
   }

   private void service_DragStarted(object sender, InteractiveDragStartedEventArgs e)
   {
      // Make sure pinch is not working, otherwise, ignore pan
      if (!this.CanStartWork(e))
         return;

      // Make sure we are on an item
      if (this.Item == null)
         return;

      e.IsHandled = true;
      this.OnWorkStarted(EventArgs.Empty);
   }

   private void service_DragDelta(object sender, InteractiveDragDeltaEventArgs e)
   {
      if (!this.IsWorking)
         return;

      var item = this.Item;
      if (item == null)
         return;

      // Find out what to do
      var dx = e.Change.X;
      var dy = e.Change.Y;

      if (dx == 0 && dy == 0)
         return;

      var scaleKeyModifier = this.ScaleKeyModifier;
      var rotateKeyModifier = this.RotateKeyModifier;
      var scale = (Control.ModifierKeys & scaleKeyModifier) == scaleKeyModifier;
      var rotate = (Control.ModifierKeys & rotateKeyModifier) == rotateKeyModifier;

      var imageViewer = this.ImageViewer;

      if (scale)
      {
         Scale(imageViewer, item, dy, e.Origin);
      }
      else if (rotate)
      {
         Rotate(imageViewer, item, dx, e.Origin);
      }
      else
      {
         Translate(imageViewer, item, dx, dy);
      }

      e.IsHandled = true;
   }

   private void service_DragCompleted(object sender, InteractiveDragCompletedEventArgs e)
   {
      if (!this.IsWorking)
         return;

      e.IsHandled = true;
      OnWorkCompleted(EventArgs.Empty);
   }

   private void service_DoubleTap(object sender, InteractiveEventArgs e)
   {
      // Make sure we not working already
      if (this.IsWorking || !this.CanStartWork(e))
         return;

      // Make sure we have an item
      var item = this.Item;
      if (item == null)
         return;

      e.IsHandled = true;

      this.OnWorkStarted(EventArgs.Empty);
      Identity(this.ImageViewer, item);
      this.OnWorkCompleted(EventArgs.Empty);
   }

   private void Identity(ImageViewer imageViewer, ImageViewerItem item)
   {
      item.Transform = LeadMatrix.Identity;
   }

   private void Scale(ImageViewer imageViewer, ImageViewerItem item, int dy, LeadPoint position)
   {
      // ...
      // set scale code
      // ...
   }

   private void Rotate(ImageViewer imageViewer, ImageViewerItem item, int dx, LeadPoint position)
   {
      // ...
      // set rotate code
      // ...
   }

   private void Translate(ImageViewer imageViewer, ImageViewerItem item, int dx, int dy)
   {
      // ...
      // set translate code
      // ...
   }
}

[TestMethod]
public void ImageViewerInteractiveMode_Example()
{
   _imageViewer.InteractiveModes.BeginUpdate();
   _imageViewer.InteractiveModes.Add(new TransformInteractiveMode { IsEnabled = false });
   //...
   //...
   //...
   _imageViewer.InteractiveModes.EndUpdate();
}
必要条件

ターゲットプラットホーム

参照

参照

ImageViewerInteractiveModeメンバ
Leadtools.Controls名前空間