注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

程序员驿站

淘宝店:http://shop106888457.taobao.com

 
 
 

日志

 
 

Silverlight 5: Supporting Double and Even Triple Click for the Mouse  

2013-09-09 13:52:43|  分类: Silverlight |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Double-click isn't as popular a mouse action as it once was. For many, especially the elderly, clicking twice in the same spot without moving the mouse in between, is actually quite challenging. Nevertheless, double-click (and even triple click in some specialized programs in 3d and music) is a necessary capability.

Please note that this article and the attached sample code was written using the Silverlight 5 Beta, available at MIX11 in April 2011, and updated for the Silverlight 5 RC.

Silverlight 5 has introduced the concept of a click count. Rather than create dedicated double, triple, or septuple click events, you can simply count the number of clicks using the ClickCount property of the MouseButtonEventArgs class. This works for both the left and right mouse buttons.

Implementing this yourself is non-trivial, as you have to take into account some fairly fiddly timings, ensuring that two consecutive single clicks aren't taken as a double-click unless the second click happens within a specific time period after the previous click. I'm happy to let the platform do that work for me.

Project Setup

I created a simple Silverlight web project. In the MainPage.xaml, I added a single red rectangle and wired up an event handler for MouseLeftButtonDown.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<UserControl x:Class="MouseClickCountDemo.MainPage"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">
  
    <Grid x:Name="LayoutRoot" Background="White">
        <Rectangle x:Name="ClickMe"
                   Width="80"
                   Height="80"
                   MouseLeftButtonDown="ClickMe_MouseLeftButtonDown"
                   Fill="Red" />
    </Grid>
</UserControl>

The only code-behind is simply an event handler that helps us count the clicks.

Implementing Double Click

Inside the ClickMe_MouseLeftButtonDown event, I added some debug code to write the number of clicks to the output window, and display a "Double Click" message if there are two clicks in a row.

1
2
3
4
5
6
7
8
private void ClickMe_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Click Count: " + e.ClickCount);
  
    if (e.ClickCount == 2)
        System.Diagnostics.Debug.WriteLine("Double Click");
  
}

That's all there is to it. I've been able to generate click counts up into the hundreds before my finger started to protest. There is no artificial limit on the number of clicks you can respond to. Try clicking slowly and clicking quickly to get a feel for how it responds.

Source code for the Silverlight 5 Beta version of this application may be downloaded via the link below.

  评论这张
 
阅读(426)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018