在notifyicon winforms c#上鼠标双击事件之前触发鼠标单击事件



使用我的通知图标我的鼠标点击和双击事件的功能是完全不同的鼠标单击事件-将打开一个窗体双击event -将打开一个文件夹

鼠标点击事件完成它的工作,但是当触发双击事件时,它仍然触发鼠标点击事件,我的表单仍然在双击时显示。

双击的实际结果:打开文件夹并打开表单双击预期结果:打开文件夹

我已经在点击时关闭了表单,但在隐藏之前仍然显示了一秒钟,因为鼠标单击事件在双击事件之前首先触发。

private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
//opens folder function
//close widget on double click
if (widgetForm != null)
widgetForm.Close();
}

我为wpf编写的代码也可以在winforms中使用。

DispatcherTimer timer;
int clickCount = 0;
public MainWindow()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(200);
}
private void MyButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
clickCount++;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
timer.Stop();
if (clickCount >= 2)
{
MessageBox.Show("doubleclick");
}
else
{
MessageBox.Show("click");
}
clickCount = 0;
}

winforms:

System.Timers.Timer timer;
int clickCount = 0;
public Form1()
{
InitializeComponent();
timer = new System.Timers.Timer();
timer.Elapsed += Timer_Elapsed;
//SystemInformation.DoubleClickTime default is 500 Milliseconds
timer.Interval = SystemInformation.DoubleClickTime;
//or
timer.Interval = 200;
}
private void myButton_MouseDown(object sender, MouseEventArgs e)
{
clickCount++;
timer.Start();
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Stop();
if (clickCount >= 2)
{
MessageBox.Show("doubleclick");
}
else
{
MessageBox.Show("click");
}
clickCount = 0;       
}
使用我在wpf示例中提到的代码,您可以将SystemInformation.DoubleClickTime更改为所需的时间

我想出了如何处理这是添加任务延迟在我的鼠标点击事件等待,如果它触发双击事件在同一时间。

谢谢你的回答!

private bool isSingleClicked;
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
isSingleClicked = false;
//double click process
}
private async void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
//delay click event to check if it trigger double click event
isSingleClicked = true;
await Task.Delay(100);
//check if it trigger double click event, will not proceed to the next line
if (!isSingleClicked) return; 
//process of mouse click
isSingleClicked = false;
}

最新更新