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

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

更新:

[2008-7-23]

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

 

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

测试如下:

src="http://silverlight.services.live.com/invoke/4156/Codename.Windows.Input/iframe.html" scrolling="no" frameborder="0" style="width:600px; height:400px">

具体使用如下:

使用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 {  set get ; }

 

 设计类图如下:

 

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

代码如下:

//
// 作者:宋剑飞
// 网名: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
}

 

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值