C#|Control|マウスジェスチャを実装するのWebBrowserコントロール版
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace Anis.Controls { class MouseGestureListenerForWebBrowser :IDisposable { WebBrowser webBrowser; ToolStripStatusLabel statusView; bool browserContextMenuEnabled; int sensitivity; string gestureStatus; Dictionary<string, string> actionList; Point mousePosition; bool isMouseDown = false; /// <param name="webBrowser">マウスジェスチャ機能を追加するWebBrowser</param> /// <param name="statusControl">マウスジェスチャの状態を表示させるToolStripItem</param> /// <param name="sensitivity">マウスジェスチャの感度</param> /// <param name="actionList">キーに"↑","→","↓","←",を組み合わせたマウスジェスチャを、 /// 値にアクション名を登録したDictionary</param> /// <remarks>マウスジェスチャが完了するとGestureCompleteイベントが発生するので、 /// e.Actionでアクション名を取得して下さい。</remarks> public MouseGestureListenerForWebBrowser( WebBrowser webBrowser, ToolStripStatusLabel statusView, int sensitivity, Dictionary<string, string> actionList) { this.webBrowser = webBrowser; this.statusView = statusView; this.sensitivity = sensitivity; this.actionList = actionList; this.browserContextMenuEnabled = webBrowser.IsWebBrowserContextMenuEnabled; this.webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted); this.statusView.TextChanged += new EventHandler(statusControl_TextChanged); } void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { webBrowser.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown); webBrowser.Document.MouseMove += new HtmlElementEventHandler(Document_MouseMove); webBrowser.Document.MouseUp += new HtmlElementEventHandler(Document_MouseUp); webBrowser.PreviewKeyDown += new PreviewKeyDownEventHandler(browser_PreviewKeyDown); } void browser_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { webBrowser.IsWebBrowserContextMenuEnabled = browserContextMenuEnabled; } void Document_MouseDown(object sender, HtmlElementEventArgs e) { if(Control.MouseButtons == MouseButtons.Right) { webBrowser.IsWebBrowserContextMenuEnabled = false; mousePosition = e.MousePosition; gestureStatus = ""; isMouseDown = true; } } void Document_MouseMove(object sender, HtmlElementEventArgs e) { if(Control.MouseButtons != MouseButtons.Right) { isMouseDown = false; statusView.Text = ""; gestureStatus = ""; } if(isMouseDown) { string str = ""; bool flag = false; if(mousePosition.X - e.MousePosition.X >= sensitivity && !gestureStatus.EndsWith("←")) { str = "←"; flag = true; } else if(mousePosition.X - e.MousePosition.X <= -sensitivity && !gestureStatus.EndsWith("→")) { str = "→"; flag = true; } else if(mousePosition.Y - e.MousePosition.Y >= sensitivity && !gestureStatus.EndsWith("↑")) { str = "↑"; flag = true; } else if(mousePosition.Y - e.MousePosition.Y <= -sensitivity && !gestureStatus.EndsWith("↓")) { str = "↓"; flag = true; } if(flag) { mousePosition = e.MousePosition; gestureStatus += str; statusView.Text = gestureStatus; } } } void Document_MouseUp(object sender, HtmlElementEventArgs e) { if(gestureStatus == "") { webBrowser.IsWebBrowserContextMenuEnabled = browserContextMenuEnabled; } if(actionList.ContainsKey(gestureStatus)) { GestureComplete(sender, new GestureCompleteEventArgs(actionList[gestureStatus])); } isMouseDown = false; statusView.Text = ""; } void statusControl_TextChanged(object sender, EventArgs e) { ToolStripItem ctrl = (ToolStripItem)sender; if(actionList.ContainsKey(ctrl.Text)) { ctrl.Text += "[" + actionList[ctrl.Text] + "]"; } } /// <summary>マウスジェスチャが完了すると発生するイベント</summary> public event GestureCompleteEventHandler GestureComplete; #region IDisposable メンバ public void Dispose() { webBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted); statusView.TextChanged -= new EventHandler(statusControl_TextChanged); webBrowser.Document.MouseDown -= new HtmlElementEventHandler(Document_MouseDown); webBrowser.Document.MouseMove -= new HtmlElementEventHandler(Document_MouseMove); webBrowser.Document.MouseUp -= new HtmlElementEventHandler(Document_MouseUp); webBrowser.PreviewKeyDown -= new PreviewKeyDownEventHandler(browser_PreviewKeyDown); } #endregion } ///<summary>GestureCompleteイベントを処理するメソッドを表します。</summary> public delegate void GestureCompleteEventHandler(object sender, GestureCompleteEventArgs e); /// <summary>GestureCompleteイベントのデータ</summary> public class GestureCompleteEventArgs :EventArgs { private string action_ = ""; /// <param name="action">マウスジェスチャのアクション名</param> public GestureCompleteEventArgs(string action) : base() { action_ = action; } /// <summary>マウスジェスチャのアクション名を取得します。</summary> public string Action { get { return action_; } } } }
使い方はほぼ同じ。以下の様に使用する。
using System; using System.Collections.Generic; using System.Windows.Forms; using Anis.Controls; namespace WindowsFormsApplication1 { public partial class Form1 :Form { public Form1() { InitializeComponent(); } MouseGestureListenerForWebBrowser mg; private void Form1_Load(object sender, EventArgs e) { Dictionary<string, string> list = new Dictionary<string, string>(); list.Add("←", "戻る"); list.Add("→", "進む"); mg = new MouseGestureListenerForWebBrowser(webBrowser1, toolStripStatusLabel1, 20, list); mg.GestureComplete += new GestureCompleteEventHandler(mg_GestureComplete); webBrowser1.GoHome(); } void mg_GestureComplete(object sender, GestureCompleteEventArgs e) { switch(e.Action) { case "戻る": webBrowser1.GoBack(); break; case "進む": webBrowser1.GoForward(); break; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { mg.Dispose(); } } }