您的位置:首页 > 其它

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

2012-03-16 13:57 393 查看
在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(0, 0, 0, 0, 400);
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())
{
//...就是这么简单!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: