Codename.Windows.Input — 让Silverlight使用右键和滚轮事件更简单[2008-12-12更新]

源代码下载:Codename.Windows.Input v0.9.2

更新:

[2008-12-12]

在Silverlight 2正式版中可以运行。

[2008-7-23]

据一网友反映,鼠标按钮状态在MouseButtonDown和MouseButtonUp事件中实现的不正确,现已更正并添加了一点小功能,谢谢此网友提醒。

 

 众所周知,Silvelight现在不支持右键和滚轮事件,不过可以通过另类途径让Silverlight实现右键和滚轮事件。现在网上有好多关于实现右键和滚轮事件的文章,不过好像都没有将其封装成使用简洁便利的类库。为了方便大家使用,故本人将其封装成使用方便的类库。由于本人能力有限,类库封装的并不完美,如当前版本不支持事件路由等,不过不影响使用。

测试如下:

具体使用如下:

使用MouseEvent类中以下函数即可实现相应的功能。

public static bool AttachMouseButtonDownEvent(System.Windows.UIElement element, System.EventHandler<mousebuttoneventargs> handler) 
public static bool AttachMouseButtonUpEvent(System.Windows.UIElement element, System.EventHandler<MouseButtonEventArgs> handler)
public static bool AttachMouseWheelEvent(System.Windows.UIElement element, System.EventHandler<MouseWheelEventArgs> handler)
public static void DetachMouseButtonDownEvent(System.Windows.UIElement element, System.EventHandler<MouseButtonEventArgs> handler)
public static void DetachMouseRightButtonUpEvent(System.Windows.UIElement element, System.EventHandler<MouseButtonEventArgs> handler)
public static void DetachMouseWheelEvent(System.Windows.UIElement element, System.EventHandler<MouseWheelEventArgs> handler)

 

使用MouseEvent类中以下属性可以直接获得鼠标按钮的状态。

public static Codename.Windows.Input.MouseButtonState LeftButton { get; }
public static Codename.Windows.Input.MouseButtonState MiddleButton { get; }
public static Codename.Windows.Input.MouseButtonState RightButton { get; }
public static Codename.Windows.Input.MouseButton ChangedButton { get; }
public static Codename.Windows.Input.MouseButtonState ButtonState { get; }
public static bool PreventContextMenu { setget; }

 

 设计类图如下:

 

详细的设计请参考代码和代码中的注释

代码如下:

//////////////////////////////////////////////////////////////////////////////////////////
//作者:宋剑飞
//网名:Codename
//QQ:247823451
//邮箱:codename.net@hotmail.com
//声明:本代码可以无偿使用,如将本代码用于商业用途,必须经本人许可,否则视为侵权。
//////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Browser;
using System.Collections.Generic;

namespace Codename.Windows.Input
{
    
MouseEvent

    
UIElementMouseState

    
MouseWheelEventArgs

    
MouseButtonEventArgs

    
MouseButton

    
MouseButtonState
}

 

声明:本代码可以无偿使用,如将本代码用于商业用途,必须经本人许可,否则视为侵权。

posted @ 2008-07-22 04:53  codename.net  阅读(1148)  评论(8编辑  收藏  举报