Silverlight 4以下版本模拟鼠标双击事件

在Silverlight 5的新特性中已经包含了鼠标双击的事件,看以下的代码

<Grid x:Name= " LayoutRoot " Background= " White ">
        <Ellipse Height= " 103 " HorizontalAlignment= " Left " Fill= " Green " Margin= " 117,56,0,0 "
                 Name= " ellipse1 " Stroke= " Black " StrokeThickness= " 1 " VerticalAlignment= " Top "
                 Width= " 158 " MouseLeftButtonDown= " ellipse1_MouseLeftButtonDown "  
                 MouseRightButtonDown= " ellipse2_MouseRightButtonDown " />
    </Grid>

在事件实现中:

private  void ellipse1_MouseLeftButtonDown( object sender, MouseButtonEventArgs e)
        {
             // 判断鼠标在系统设置的双击间隔时间之内被点击了两次则弹出窗口显示
             if (e.ClickCount ==  2)
            {
                MessageBox.Show( " 鼠标左键点击 "+e.ClickCount.ToString());
            }
        }

 

而在Silverlight4以下版本均不包含ClickCount的属性,那么只能自己实现,于是,实现了一个模拟鼠标双击的功能:

     public  class MouseTool
    {
         // 双击事件定时器
         private DispatcherTimer _timer;
         // 是否单击过一次
         private  bool _isFirst;

         public MouseTool()
        {
             this._timer =  new DispatcherTimer();
             this._timer.Interval =  new TimeSpan( 0000400);
             this._timer.Tick +=  new EventHandler( this._timer_Tick);
        }

         ///   <summary>
        
///  判断是否双击
        
///   </summary>
        
///   <returns></returns>
         public  bool IsDoubleClick()
        {
             if (! this._isFirst)
            {
                 this._isFirst =  true;
                 this._timer.Start();
                 return  false;
            }
             else
                 return  true;
        }

         // 间隔时间
         void _timer_Tick( object sender, EventArgs e)
        {
             this._isFirst =  false;
             this._timer.Stop();
        }
    }

在事件调用中如下:

private MouseTool _mouseTool =  new MouseTool();

public  void GridSplitter_MouseLeftButtonDown( object sender, MouseButtonEventArgs e)
        {
             if ( this._mouseTool.IsDoubleClick())
            {
// ...

就是这么简单!

转载于:https://www.cnblogs.com/liping13599168/archive/2012/03/16/2400178.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值