How to determine inactivity on a winform

后端 未结 2 961
长发绾君心
长发绾君心 2020-12-07 06:05

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

相关标签:
2条回答
  • 2020-12-07 06:13

    Here is a great example using the GetLastInputInfo from the user32.dll

    0 讨论(0)
  • 2020-12-07 06:15

    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
    
    0 讨论(0)
提交回复
热议问题