silverlight滑鼠雙擊2

來源:互聯網
上載者:User

網友做的一個雙擊的例子

原理是
第一次點擊 開一個線程(sheep 200 且 lock)
如果在200ms內再點擊 線程繼續sheep 200  統計點擊次數
...........
............
timeout 後
線程執行  比較傳入的要求點擊數與統計點擊的次數
true 的話就執行

 

 public class MClick
    {
        public static void MulClick(int count, MouseButtonEventHandler handle, object sender, MouseButtonEventArgs e)
        {
            bolClicking = true;
            if (bolClicked)
            {
                intClickCount++;
            }
            else
            {
                bolClicked = true;
                intCount = count;
                handleClick = handle;
                objSender = sender;
                mbeE = e;
                asyncOperation = AsyncOperationManager.CreateOperation(null);
                objThreadLock = new object();
                Thread thread = new Thread(ResetThread);
                thread.Start();
                while (!thread.IsAlive) ;
            }
        }

        private static int intTimeOut = 200;
        public static int TimeOut
        {
            get
            {
                return intTimeOut;
            }

            set
            {
                intTimeOut = value < 1 ? 1 : value;
            }
        }

        private static int intCount;
        private static object objSender;
        private static MouseButtonEventArgs mbeE;
        private static MouseButtonEventHandler handleClick;
        private static int intClickCount = 0;
        private static bool bolClicked = false;
        private static bool bolClicking = false;
        private static object objThreadLock;
        private static AsyncOperation asyncOperation;

        private static void ResetThread()
        {
            while (bolClicking)
            {
                bolClicking = false;
                Thread.Sleep(TimeOut);
            }

            lock(objThreadLock)
            {
                if (intCount == ++intClickCount)
                {
                    asyncOperation.Post(callback, objSender);  
                }

                intClickCount = 0;
                bolClicked = false;
            }

        }

        private static void callback(object state)
        {
            handleClick(objSender, mbeE);
        }
    }

============================
只是測試性,滿亂的~~哈哈。有興趣的朋友可以改進~
改成
MulClick(點擊次數, 控制項, 函數)

使用
==============================
        public Page()
        {
            InitializeComponent();
            LayoutRoot.MouseLeftButtonUp += new MouseButtonEventHandler(HandleClick);
        }

        private void HandleClick(object sender, MouseButtonEventArgs e)
        {
           
            //MClick.MulClick(1, onClick, sender, e);

            MClick.MulClick(2, onDoubleClick, sender, e);

        }

        private void onClick(object sender, MouseButtonEventArgs e)
        {
            txtMsg.Text = "單擊";
        }

        private void onDoubleClick(object sender, MouseButtonEventArgs e)
        {
            txtMsg.Text = "雙擊";
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.