I have a winform that is displayed at the top of my application. What I would like to is have the form set to 20% opacity if it has been inactive for a certain amount of tim
Here is a great example using the GetLastInputInfo
from the user32.dll
The IMessageFilter interface is good for this, it lets you see all of the mouse and keyboard messages. Enable a timer when you see one. Change the Opacity property when it ticks. Like this:
Public Class Form1
Implements IMessageFilter
Public Sub New()
InitializeComponent()
Application.AddMessageFilter(Me)
Me.Opacity = 0.99
Timer1.Start()
End Sub
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
Application.RemoveMessageFilter(Me)
MyBase.OnFormClosed(e)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Me.Opacity = 0.3
End Sub
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
'' Bump timer on mouse or keyboard messages
If (m.Msg >= &H200 And m.Msg <= &H20E) Or (m.Msg >= &H100 And m.Msg <= &H109) Then
Timer1.Stop()
Timer1.Start()
Me.Opacity = 0.99
End If
Return False
End Function
End Class