[英]How to trigger the auto-hide icon in c#?
我有一個申請。 如果應用程序在一段時間內沒有被使用一段時間,它應該隱藏。 當應用程序被隱藏並且我們將鼠標懸停在圖標上時,它應該被恢復。
我怎樣才能做到這一點? 提前致謝。
您必須在應用程序中定義一個計時器,它將計算鼠標不在表單/窗口上的時間。 然后隱藏你的應用程序。
下載WPF NotifyIcon並處理將顯示表單/窗口的MouseOver事件
編輯:
如果您不需要將應用程序最小化到托盤並隱藏窗口保持在桌面上 - >使用相同的算法,但不要隱藏窗口,只需將透明度設置為0%或10%。 鼠標過度時將透明度設置為100%。
就像JesseJames說的那樣,使用計時器來測量應用程序的非活動時間並在一段時間后隱藏它。 當鼠標懸停在NotifyIcon上時重新激活它。 以下是完成工作的WindowsForms解決方案示例:
private Timer _timer;
private int _ticks;
public Form1()
{
_timer = new Timer { Interval = 1000, Enabled = true };
_timer.Tick += TimerTick;
Activated += Form1_Activated;
MouseMove += Form1_MouseMove;
//notifyIcon1 is an icon set through the designer
notifyIcon1.MouseMove += NotifyIcon1MouseMove;
}
protected void TimerTick(object sender, EventArgs e)
{
//After 5 seconds the app will be hidden
if (_ticks++ == 5)
{
WindowState = FormWindowState.Minimized;
Hide();
_timer.Stop();
_ticks = 0;
}
}
protected void NotifyIcon1MouseMove(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
Show();
_ticks = 0;
_timer.Start();
}
protected void Form1_MouseMove(object sender, MouseEventArgs e)
{
_ticks = 0;
}
也許可能存在一個更清潔的解決方案,我不知道,但它會讓你在路上。 WPF的原理相同,只是代碼略有不同。 希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.