Silverlight中捕获Click及DoubleClick鼠标事件

本文介绍了一种在Silverlight中实现Click与DoubleClick事件的方法。通过自定义MouseClickManager类,可以使FrameworkElement支持这两种鼠标事件。

Silverlight中没有鼠标的Click以及DoubleClick事件,至少到目前Silverlight Beta 2中还没有。希望在正式版中有相关事件处理(正式版中有太多期待了呵)。

参考了这篇blog,稍微修改了里面的一点代码,让所有FrameworkElement都可以产生Click及DoubleClick事件。由于不知道怎么添加附件,只好把所有代码贴这里,其中添加一些关键点的注释:

 

ContractedBlock.gif ExpandedBlockStart.gif MouseClickManager.cs
public class MouseClickManager
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public event MouseButtonEventHandler Click;
        
public event MouseButtonEventHandler DoubleClick;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Gets or sets a value indicating whether this <see cref="MouseClickManager"/> is clicked.
        
/// </summary>
        
/// <value><c>true</c> if clicked; otherwise, <c>false</c>.</value>

ExpandedSubBlockStart.gifContractedSubBlock.gif        private bool Clicked getset; }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Gets or sets the control.
        
/// </summary>
        
/// <value>The control.</value>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public FrameworkElement Control getset; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Gets or sets the timeout.
        
/// </summary>
        
/// <value>The timeout.</value>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public int Timeout getset; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Initializes a new instance of the <see cref="MouseClickManager"/> class.
        
/// </summary>
        
/// <param name="control">The control.</param>

        public MouseClickManager(FrameworkElement control, int timeout)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.Clicked = false;
            
this.Control = control;
            
this.Timeout = timeout;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Handles the click.
        
/// </summary>
        
/// <param name="sender">The sender.</param>
        
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>

        public void HandleClick(object sender, MouseButtonEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
lock(this)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (this.Clicked) //之前已经得到一个Click,再次得到视为DoubleClick
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    
this.Clicked = false;
                    OnDoubleClick(sender, e);
                }

                
else //捕获Click事件
ExpandedSubBlockStart.gifContractedSubBlock.gif
                {
                    
this.Clicked = true;
                    
//创建线程来对标志位进行置位
                    ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResetThread);
                    Thread thread 
= new Thread(threadStart);
                    thread.Start(e);
                }

            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Resets the thread.
        
/// </summary>
        
/// <param name="state">The state.</param>

        private void ResetThread(object state)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//就是说超过在第一个Click之后超过间隔时间没有收到第二个Click则认为是Click
            Thread.Sleep(this.Timeout);

            
lock (this)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (this.Clicked)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
this.Clicked = false;
                    OnClick(
this, (MouseButtonEventArgs)state);
                }

            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Called when [click].
        
/// </summary>
        
/// <param name="sender">The sender.</param>
        
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>

        private void OnClick(object sender, MouseButtonEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            MouseButtonEventHandler handler 
= Click;

            
if (handler != null)
                
this.Control.Dispatcher.BeginInvoke(handler, sender, e);
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Called when [double click].
        
/// </summary>
        
/// <param name="sender">The sender.</param>
        
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>

        private void OnDoubleClick(object sender, MouseButtonEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            MouseButtonEventHandler handler 
= DoubleClick;

            
if (handler != null)
                handler(sender, e);
        }

    }

 

 实际用的时候:

1. 把MouseClickManage.cs类引入到工程中;

2. 用需要获得Click或DoubleClick的FrameworkElement初始化MouseClickManager,并指定时间间隔(200ms挺不错)

3. 在FrameworkElement的MouseLeftButtonUp事件中调用MouseClickManager.HandleClick()方法

看代码:

ContractedBlock.gif ExpandedBlockStart.gif Application Use
public partial class Page : UserControl
ExpandedBlockStart.gifContractedBlock.gif    
{
        
private MouseClickManager mcm = null;

        
public Page()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
            
this.Loaded += new RoutedEventHandler(Page_Loaded);
        }


        
void Page_Loaded(object sender, RoutedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//初始化MouseClickManager
            this.mcm = new MouseClickManager(this.LayoutRoot, 200);
            mcm.Click 
+= new MouseButtonEventHandler(mcm_Click);
            mcm.DoubleClick 
+= new MouseButtonEventHandler(mcm_DoubleClick);
            
this.LayoutRoot.MouseLeftButtonUp += new MouseButtonEventHandler(LayoutRoot_MouseLeftButtonUp);
        }


        
void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//调用HandleClick方法
            mcm.HandleClick(sender, e);
        }


        
void mcm_DoubleClick(object sender, MouseButtonEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.ClickMessage.Text = "Double click!";
        }


        
void mcm_Click(object sender, MouseButtonEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.ClickMessage.Text = "Click!";
        }

    }

 

就这样用吧。

转载于:https://www.cnblogs.com/sayo/archive/2008/08/17/1269927.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值