• 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())
                {
    //...

    就是这么简单!

  • 相关阅读:
    kafka 安装和基本操作
    IPv6表示方法及其简化方法
    Python print输出函数
    同步工具之Vector X
    golang之热加载Fresh&air X
    TOML 1.0格式语法 X
    PHPstorm配置webserver X
    编程辅助工具之Kite X
    golang项目之Makefile X
    高性能消息队列之nsq X
  • 原文地址:https://www.cnblogs.com/liping13599168/p/2400178.html
Copyright © 2020-2023  润新知