Vb.net 从自定义计时器类更改窗体不透明度

Vb.net 从自定义计时器类更改窗体不透明度,vb.net,winforms,Vb.net,Winforms,我创建了一个继承timer类的类,因为我想自定义Tick函数,并且我想在许多类中使用这个特定函数,而不需要每次在所有计时器中更改函数 Public Class FadeInTimer Inherits Timer Public Sub New() MyBase.New() Me.Enabled = False Me.Interval = 75 End Sub Private Sub FadeInTimer_Tick(sender As Object, e As Ev

我创建了一个继承timer类的类,因为我想自定义Tick函数,并且我想在许多类中使用这个特定函数,而不需要每次在所有计时器中更改函数

Public Class FadeInTimer
Inherits Timer

Public Sub New()
    MyBase.New()

    Me.Enabled = False
    Me.Interval = 75
End Sub

Private Sub FadeInTimer_Tick(sender As Object, e As EventArgs) Handles Me.Tick
    Dim workingAreaWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
    Me.Opacity += 0.1

    If Not Me.Location.X <= workingAreaWidth Then
        Me.Location = New Point(Me.Location.X - 30, Me.Location.Y)
    End If

    Me.Refresh()
    If Me.Opacity = 1 Then
        Me.Stop()
    End If
End Sub
End Class
公共类FadeInTimer
继承计时器
公共分新()
MyBase.New()
Me.Enabled=False
Me.间隔=75
端接头
私有子FadeInTimer_Tick(发送者作为对象,e作为事件参数)处理我。Tick
Dim WORKINGAREAWITH As Integer=Screen.PrimaryScreen.WorkingArea.Width-Me.Width
Me.Opacity+=0.1

如果不是Me.Location.X首先要做的是在自定义计时器的构造函数中传递一个要淡入的表单实例,将该实例保存在一个全局类变量中,并使用AddHandler添加勾号处理程序,如下所示

现在,当您需要引用“parent”表单时,可以使用parent变量,而不是Me语句。此外,每次需要引用计时器时,都应该使用MyBase语句

Private Sub FadeInTimer_Tick(sender As Object, e As EventArgs) 
    Dim workingAreaWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width - Parent.Width
    parent.Opacity += 0.1

    If Not parent.Location.X <= workingAreaWidth Then
        parent.Location = New Point(parent.Location.X - 30, parent.Location.Y)
    End If

    parent.Refresh()
    If parent.Opacity = 1 Then
        MyBase.Stop()
    End If
End Sub

如果需要对表单对象的引用,请将其传递给构造函数。你不能让它增加到1.0,那会像廉价汽车旅馆一样闪烁。0.99是最大值。
Private Sub FadeInTimer_Tick(sender As Object, e As EventArgs) 
    Dim workingAreaWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width - Parent.Width
    parent.Opacity += 0.1

    If Not parent.Location.X <= workingAreaWidth Then
        parent.Location = New Point(parent.Location.X - 30, parent.Location.Y)
    End If

    parent.Refresh()
    If parent.Opacity = 1 Then
        MyBase.Stop()
    End If
End Sub
Sub Main
    Dim f As Form = New Form()
    Dim t As FadeInTimer = New FadeInTimer(f)

    f.Opacity = 0
    t.Interval = 150
    t.Start()
    f.ShowDialog()

End Sub