[Code Project].NETアプリケーションにマウスジェスチャーを追加する


Adding Mouse Gesture Functionality to Your .NET Application Within Seconds
http://www.codeproject.com/KB/cs/mouse_gesture.aspx
より。


The idea of finding a mouse gesture is as follows:

1.Finding the bounding rectangle of the mouse movement
2.Dividing this rectangle into the same number of rectangular fields of the same size (width and height are devided by the same number) — in other words: laying a square grid over this rectangle
3.Number the fields of the grid from top/left to bottom/down from left to right starting at 0
4.Tracking the path/fields in which the mouse moved, starting on MouseDown and stopping on MouseUp event
5.Convert the field numbers/the path into a key
6.Lookup the command for the found key


マウスジェスチャーを判定する方法は以下のとおり

1.マウスの動いた範囲を矩形で取得する
2.取得した範囲を正方形のグリッドに分割する
3.グリッドの左上から0から始まる番号でナンバリングする
4.MouseDownからMouseUpまでのあいだに通過したグリッドの番号を取得する
5.通過したグリッド番号をキーに変換する
6.キーに対応したコマンドを取得する



How to Use the Code
Using the DcamMouseGesture functionality just needs a few lines of code after adding a reference to DcamMouseGesture.dll in your project:

1.Refer to namespace DcamMouseGesture
2.Load a file with the commands and keys once in your application
3.For each Form you want to use mouse gestures, instantiate an object of class MouseGesture and register to the MouseGestureEvent event
4.In your registered MouseGestureEventHandler, handle the commands you want



使い方
DcamMouseGesture.dllへの参照設定と、数行のコードだけでDcamMouseGestureの機能を使用することができます。

1.DcamMouseGesture名前空間への参照を設定する
2.commandとkeyの設定ファイルアプリケーションの起動時に読み込む
3.マウスジェスチャーを使用するフォームごとに、MouseGestureクラスのインスタンスを生成し、MouseGestureEventを登録する
4.登録したMouseGestureEventHandlerで、すきなコマンドを実行する

// a) Refer to namespace "DcamMouseGesture"
using DcamMouseGesture;

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        MouseGesture _mg;

        public Form1()
        {
            InitializeComponent();

            // b) Load a file with the commands and keys once in your application
            MouseGestureData.Instance.Commands.ReadFile( 
                Environment.CurrentDirectory + @"\MouseGestureCommands.xml" );

            // c) For each Form you want to use mouse gestures...
            _mg = new MouseGesture( this, null ); 
            _mg.MouseGestureEntered += new MouseGestureEventHandler( 
                OnMouseGestureEntered );
        }

        private void OnMouseGestureEntered( object sender, MouseGestureEventArgs e )
        {
            // d) In your registered MouseGestureEventHandler, handle the commands
            // you want
            MessageBox.Show( string.Format( "OnMouseGestureEntered:\n" +
                                            "   Command:\t{0}\n" +
                                            "   Key:\t\t{1}\n" +
                                            "   Control:\t\t{2}\n" +
                                            "   Bounds:\t\t{3}", 
                                            e.Command, e.Key, e.Control,
                                            e.Bounds.ToString() ) );
        }

    }
}

詳細は、上記のサイトを参照