如何确定winform上的不活动

时间:2013-07-20 04:08:29

标签: vb.net

我有一个winform,显示在我的应用程序的顶部。我想要的是将表单设置为20%不透明度,如果它在一段时间内处于非活动状态。现在我点击按钮时会运行类似的事件,表单的大小会发生变化。我在执行转换时使用计时器设置表单不透明度。我现在可以使用类似的代码来设置不透明度,如果表单上没有活动,我只是不知道如何检测不活动。

这就是我所拥有的。

    Private Sub btnShowForm_Click(sender As Object, e As EventArgs) Handles btnShowForm.Click

    'This procedure runs when the btnShowForm
    'button is clicked. The procedure maximizes the size
    'of the form, hides the left right button and displays the button
    'to expand the form. It also moves the combobox down. 
    'It calls the viewButtons function to hide and
    'display the right buttons.     
    'The procedure also uses a timer to set the fade in and out the
    'form when is min or max

    Me.Height = 126

    Me.Opacity = 0.2                   'About 20%
    timNavigationPage.Interval = 100   'about one-tenth of a second
    timNavigationPage.Start()          'Start the timer


    cmbViewDataSheets.Location = New Point(741, 89)


    viewButtons(False, True)

End Sub

Private Sub timNavigationPage_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timNavigationPage.Tick

    'Initialize the timer to fade form.

    Dim x As Double = 0.075

    If Me.Opacity <= 1 Then
        Me.Opacity += x     'increment opacity with 7.5%

    ElseIf Me.Opacity + x > 1 Then
        timNavigationPage.Stop()       'Stop the timer then the opacity has reached a 100%

    End If

End Sub

2 个答案:

答案 0 :(得分:1)

使用GetLastInputInfo

中的user32.dll,这是一个很棒的example

答案 1 :(得分:1)

IMessageFilter界面适用于此,它可以让您查看所有鼠标和键盘消息。看到时启用计时器。滴答时更改不透明度属性。像这样:

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