using System;
using System.Windows;
using System.Windows.Browser;
/// <summary>
/// 마우스 휠 도우미
/// </summary>
public class MouseWheelHelper
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Declaration
////////////////////////////////////////////////////////////////////////////////////////// Class
#region 작업자 - Worker
/// <summary>
/// 작업자
/// </summary>
private class Worker
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 이동시 - Moved
/// <summary>
/// 이동시
/// </summary>
public event EventHandler<MouseWheelEventArgs> Moved;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// 생성자
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - Worker()
/// <summary>
/// 생성자
/// </summary>
public Worker()
{
if(HtmlPage.IsEnabled)
{
HtmlPage.Window.AttachEvent ("DOMMouseScroll", Worker_MouseWheel);
HtmlPage.Window.AttachEvent ("onmousewheel" , Worker_MouseWheel);
HtmlPage.Document.AttachEvent("onmousewheel" , Worker_MouseWheel);
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 작업자 마우스 휠 처리하기 - Worker_MouseWheel(sender, e)
/// <summary>
/// 작업자 마우스 휠 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Worker_MouseWheel(object sender, HtmlEventArgs e)
{
double delta = 0d;
ScriptObject scriptObject = e.EventObject;
if(scriptObject.GetProperty("wheelDelta") != null)
{
delta = ((double)scriptObject.GetProperty("wheelDelta")) / 120d;
if(HtmlPage.Window.GetProperty("opera") != null)
{
delta = -delta;
}
}
else if(scriptObject.GetProperty("detail") != null)
{
delta = -((double)scriptObject.GetProperty("detail")) / 3d;
if(HtmlPage.BrowserInformation.UserAgent.IndexOf("Macintosh") != -1)
{
delta = delta * 3d;
}
}
if(delta != 0d && Moved != null)
{
MouseWheelEventArgs mouseWheelEventArgs = new MouseWheelEventArgs(delta);
Moved(this, mouseWheelEventArgs);
if(mouseWheelEventArgs.Handled)
{
e.PreventDefault();
}
}
}
#endregion
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 이동시 - Moved
/// <summary>
/// 이동시
/// </summary>
public event EventHandler<MouseWheelEventArgs> Moved;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 작업자
/// </summary>
private static Worker _worker;
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 마우스 상위 여부
/// </summary>
private bool isMouseOver = false;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MouseWheelHelper(frameworkElement)
/// <summary>
/// 생성자
/// </summary>
/// <param name="frameworkElement">FrameworkElement 객체</param>
public MouseWheelHelper(FrameworkElement frameworkElement)
{
if(_worker == null)
{
_worker = new Worker();
}
_worker.Moved += worker_Moved;
frameworkElement.MouseEnter += frameworkElement_MouseEnter;
frameworkElement.MouseLeave += frameworkElement_MouseLeave;
frameworkElement.MouseMove += frameworkElement_MouseMove;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 작업자 이동시 처리하기 - worker_Moved(sender, e)
/// <summary>
/// 작업자 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void worker_Moved(object sender, MouseWheelEventArgs e)
{
if(this.isMouseOver)
{
Moved(this, e);
}
}
#endregion
#region FrameworkElement 객체 마우스 진입시 처리하기 - frameworkElement_MouseEnter(sender, e)
/// <summary>
/// FrameworkElement 객체 마우스 진입시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void frameworkElement_MouseEnter(object sender, EventArgs e)
{
this.isMouseOver = true;
}
#endregion
#region FrameworkElement 객체 마우스 이탈시 처리하기 - frameworkElement_MouseLeave(sender, e)
/// <summary>
/// FrameworkElement 객체 마우스 이탈시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void frameworkElement_MouseLeave(object sender, EventArgs e)
{
this.isMouseOver = false;
}
#endregion
#region FrameworkElement 객체 마우스 이동시 처리하기 - frameworkElement_MouseMove(sender, e)
/// <summary>
/// FrameworkElement 객체 마우스 이동시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void frameworkElement_MouseMove(object sender, EventArgs e)
{
this.isMouseOver = true;
}
#endregion
}