[英]mouse click event fires before mouse double click event on notifyicon winforms c#
使用我的通知圖標我的 function 鼠標單擊和雙擊事件是完全不同的鼠標單擊事件 - 將打開一個表單 雙擊事件 - 將打開一個文件夾
鼠標點擊事件完成了它的工作,但是當觸發雙擊事件時,它仍然觸發鼠標點擊事件,我的表單仍然在雙擊時顯示。
雙擊實際結果:打開文件夾並打開表單 雙擊預期結果:打開文件夾
我已經在點擊時關閉了表單,但在隱藏之前仍然顯示了一秒鍾,因為在雙擊事件之前首先觸發了鼠標點擊事件。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.