網友做的一個雙擊的例子
原理是
第一次點擊 開一個線程(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 = "雙擊";
}
}