Vb.net 如何确定winform上的非活动状态

Vb.net 如何确定winform上的非活动状态,vb.net,Vb.net,我有一个winform,它显示在我的应用程序的顶部。我想要的是,如果窗体在一段时间内处于非活动状态,则将其设置为20%不透明度。现在我有一个类似的事件在运行,当我点击一个按钮时,表单的大小会改变。在执行转换时,我使用计时器设置窗体不透明度。我现在可以使用类似的代码来设置不透明度,如果窗体上存在不活动,我只是不知道如何检测不活动 这是我的 Private Sub btnShowForm_Click(sender As Object, e As EventArgs) Handles btnS

我有一个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
Private Sub btnShowForm\u Click(发送者作为对象,e作为事件参数)处理btnShowForm。单击
'此过程在btnShowForm
'按钮被单击。该过程将最大化大小
'隐藏左右按钮并显示该按钮
'来扩展表单。它还会向下移动组合框。
'它调用viewButtons函数来隐藏和
'显示正确的按钮。
'该程序还使用计时器设置淡入淡出
'何时为最小值或最大值
身高=126
Me.不透明度=0.2'约20%
timNavigationPage.Interval=100'约十分之一秒
timNavigationPage.Start()'启动计时器
cmbViewDataSheets.Location=新点(741,89)
视图按钮(假、真)
端接头
私有子timNavigationPage_Tick(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理timNavigationPage.Tick
'将计时器初始化为淡入淡出窗体。
尺寸x为双精度=0.075

如果Me.Opacity这是一个很好的使用
user32.dll中的
GetLastInputInfo
的方法,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
公共类表单1
实现IMessageFilter
公共分新()
初始化组件()
Application.AddMessageFilter(Me)
Me.Opacity=0.99
Timer1.Start()
端接头
受保护的覆盖子OnFormClosed(如FormClosedEventArgs)
Application.RemoveMessageFilter(Me)
MyBase.OnFormClosed(e)
端接头
私有子Timer1\u Tick(发送方作为对象,e作为事件参数)处理Timer1.Tick
Timer1.Enabled=False
Me.Opacity=0.3
端接头
作为布尔值的公共函数PreFilterMessage(ByRef m作为消息)实现IMessageFilter.PreFilterMessage
“”鼠标或键盘上的凹凸计时器消息
如果(m.Msg>=&H200和m.Msg=&H100和m.Msg