网友做的一个双击的例子
原理是
第一次点击 开一个线程(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 = "双击";
}
}