2009-04-13 95 views
11

我想在空閒時間後隱藏鼠標光標,當我移動鼠標時它會顯示出來。我試圖使用一個計時器,但它不能很好地工作。有誰能夠幫助我?請!閒置時間後隱藏鼠標光標

+0

什麼樣的技術? Winforms,WPF,silverlight,ASP.Net – 2009-04-13 19:02:18

回答

10

下面是如何做到這一點的人爲的例子。你可能有一些失蹤的邏輯被重寫光標的可見性:

public partial class Form1 : Form 
{ 
    public TimeSpan TimeoutToHide { get; private set; } 
    public DateTime LastMouseMove { get; private set; } 
    public bool IsHidden { get; private set; } 

    public Form1() 
    { 
     InitializeComponent(); 
     TimeoutToHide = TimeSpan.FromSeconds(5); 
     this.MouseMove += new MouseEventHandler(Form1_MouseMove); 
    } 

    void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 
     LastMouseMove = DateTime.Now; 

     if (IsHidden) 
     { 
      Cursor.Show(); 
      IsHidden = false; 
     } 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     TimeSpan elaped = DateTime.Now - LastMouseMove; 
     if (elaped >= TimeoutToHide && !IsHidden) 
     { 
      Cursor.Hide(); 
      IsHidden = true; 
     } 
    } 
} 
+0

我找不到Cursor的`Cursor.Hide`類型。 – lindexi 2017-07-18 01:26:46

21

如果您正在使用的WinForms,只會在Windows機器上部署那麼它很容易使用user32 GetLastInputInfo處理鼠標和鍵盤空轉。

public static class User32Interop 
{ 
    public static TimeSpan GetLastInput() 
    { 
    var plii = new LASTINPUTINFO(); 
    plii.cbSize = (uint)Marshal.SizeOf(plii); 

    if (GetLastInputInfo(ref plii)) 
     return TimeSpan.FromMilliseconds(Environment.TickCount - plii.dwTime); 
    else 
     throw new Win32Exception(Marshal.GetLastWin32Error()); 
    } 

    [DllImport("user32.dll", SetLastError = true)] 
    static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); 

    struct LASTINPUTINFO 
    { 
    public uint cbSize; 
    public uint dwTime; 
    } 
} 

,然後在Form

public partial class MyForm : Form 
{ 
    Timer activityTimer = new Timer(); 
    TimeSpan activityThreshold = TimeSpan.FromMinutes(2); 
    bool cursorHidden = false; 

    public Form1() 
    { 
    InitializeComponent(); 

    activityTimer.Tick += activityWorker_Tick; 
    activityTimer.Interval = 100; 
    activityTimer.Enabled = true; 
    } 

    void activityWorker_Tick(object sender, EventArgs e) 
    { 
    bool shouldHide = User32Interop.GetLastInput() > activityThreshold; 
    if (cursorHidden != shouldHide) 
    { 
     if (shouldHide) 
     Cursor.Hide(); 
     else 
     Cursor.Show(); 

     cursorHidden = shouldHide; 
    } 
    } 
} 
+0

一個優雅的解決方案,謝謝 – frameworkninja 2009-10-28 12:19:37

3

需要考慮Environment.Tickcount是消極的:

public static class User32Interop 
{ 

    public static TimeSpan GetLastInput() 
    { 
     var plii = new LASTINPUTINFO(); 
     plii.cbSize = (uint)Marshal.SizeOf(plii); 

     if (GetLastInputInfo(ref plii)) 
     { 
      int idleTime = unchecked(Environment.TickCount - (int)plii.dwTime); 
      return TimeSpan.FromMilliseconds(idleTime); 
     } 
     else 
      throw new Win32Exception(Marshal.GetLastWin32Error()); 
    } 

    [DllImport("user32.dll", SetLastError = true)] 
    static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); 

    struct LASTINPUTINFO 
    { 
     public uint cbSize; 
     public uint dwTime; 
    } 
}