比如Winform控件ListView,我想单击Item时执行一个操作,双击Item时执行另一个操作,可是双击Item时总是先出发了单击事件,不知道大家有何良策。
我这里有一个鼠标类,感觉可以处理你这个问题。
public class MouseClickManager
{
public event MouseButtonEventHandler Click;
public event MouseButtonEventHandler DoubleClick;
/// <summary>
/// Gets or sets a value indicating whether this <see cref="MouseClickManager"/> is clicked.
/// </summary>
/// <value><c>true</c> if clicked; otherwise, <c>false</c>.</value>
private bool Clicked { get; set; }
/// <summary>
/// Gets or sets the control.
/// </summary>
/// <value>The control.</value>
public Control Control { get; set; }
/// <summary>
/// Gets or sets the timeout.
/// </summary>
/// <value>The timeout.</value>
public int Timeout { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="MouseClickManager"/> class.
/// </summary>
/// <param name="control">The control.</param>
public MouseClickManager(Control control, int timeout)
{
this.Clicked = false;
this.Control = control;
this.Timeout = timeout;
}
/// <summary>
/// Handles the click.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
public void HandleClick(object sender, MouseButtonEventArgs e)
{
lock (this)
{
if (this.Clicked)
{
this.Clicked = false;
OnDoubleClick(sender, e);
}
else
{
this.Clicked = true;
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResetThread);
Thread thread = new Thread(threadStart);
thread.Start(e);
}
}
}
/// <summary>
/// Resets the thread.
/// </summary>
/// <param name="state">The state.</param>
private void ResetThread(object state)
{
Thread.Sleep(this.Timeout);
lock (this)
{
if (this.Clicked)
{
this.Clicked = false;
OnClick(this, (MouseButtonEventArgs)state);
}
}
}
/// <summary>
/// Called when [click].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private void OnClick(object sender, MouseButtonEventArgs e)
{
MouseButtonEventHandler handler = Click;
if (handler != null)
this.Control.Dispatcher.BeginInvoke(handler, sender, e);
}
/// <summary>
/// Called when [double click].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private void OnDoubleClick(object sender, MouseButtonEventArgs e)
{
MouseButtonEventHandler handler = DoubleClick;
if (handler != null)
handler(sender, e);
}
}
用法:
public MyClass()
{
InitializeComponent();
mcm = new MouseClickManager(yourControlInstance, 300);
mcm.Click+=new MouseButtonEventHandler(mcm_Click);
mcm.DoubleClick+=new MouseButtonEventHandler(mcm_DoubleClick);
}
void mcm_DoubleClick(object sender, MouseButtonEventArgs e)
{
throw new NotImplementedException();
}
void mcm_Click(object sender, MouseButtonEventArgs e)
{
throw new NotImplementedException();
}
private void Button1_Click(object sender, MouseButtonEventArgs e)
{
mcm.HandleClick(sender, e);
}
我这是silverlight中的,不知道在winform中可以用不,你试试吧,祝你好运!
让单击事件直接return false;
比如单击时弹出A窗口。双击时弹出B窗口。通常两个事件都加上的话,双击时两个窗口都弹出了。当然也不能在单击是返回,这样的话单击时不能弹出窗体了。后来找到而来解决方法。判断两次鼠标点击的事件间隔。第一次点击鼠标时开始计时,如果到一定事件还没有点击第二次,则认为是单击;反之是双击。
楼上的是正确的