主窗体加载后,在计时器刻度上移动子窗体

时间:2019-01-04 08:32:19

标签: vb.net forms winforms

我正在VB.net中启动一个程序,在那里我真的需要为打开和关闭表单制作漂亮的动画。

现在,我有一个父窗体,该窗体用作应用程序的“背景”,同时它可以在其中打开其他窗体。 将会在应用程序的开口处加载两种形式,我希望第一种形式向左移至所需位置,然后第二种形式从下方移至右侧,并偏移其位置。

目标是使它看起来像这样

_______________    _______________
|    _____    |    | _____  ____ |
|    |   |    |    | |   |  | B| |
|    | A |    | To | | A |  |__| |
|    |___|    |    | |___|       |
|_____________|    |_____________|

正确地知道,我试图立即将两个表单加载到父表单,然后将它们放置在屏幕中间并启动计时器,以便每个刻度都使表单向左漂移,当它处于适当位置时,另一个是的。

这是加载父表单时的代码

Dim WithEvents tmr As New Timer 

Private Sub Fr_Parent_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Me.WindowState = FormWindowState.Maximized
    IsMdiContainer = True

    Dim FrTicket As New FrTicket()
    FrTicket.MdiParent = Me
    FrTicket.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
    FrTicket.Show()


    Dim FrIdentite As New FrIdentite()
    FrIdentite.MdiParent = Me
    FrIdentite.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
    FrIdentite.Show()

    Dim HautEcran As Integer
    Dim LargEcran As Integer
    LargEcran = CInt(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width.ToString())
    HautEcran = CInt(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height.ToString())
    Dim LargFrmSite As Integer = FrIdentite.Width
    Dim hautFrmSite As Integer = FrIdentite.Height

    FrIdentite.Location = New Point((LargEcran / 2 - LargFrmSite / 2), (HautEcran / 2 - hautFrmSite / 2))
    FrTicket.Location = New Point((LargEcran / 2 - LargFrmSite / 2), (HautEcran / 2 - hautFrmSite / 2))

    tmr.Start()

End Sub

然后是Tick上使用的那个

Private Sub tmr_Tick()

    Dim HautEcran As Integer
    Dim LargEcran As Integer
    LargEcran = CInt(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width.ToString())
    HautEcran = CInt(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height.ToString())
    Dim LargFrmSite As Integer = FrIdentite.Width
    Dim hautFrmSite As Integer = FrIdentite.Height

    Dim xIdenLoc As Integer = CInt(LargEcran / 2 - (LargFrmSite * 1.01))
    Dim yIdenLoc As Integer = CInt(HautEcran / 2 - hautFrmSite / 2)
    Dim xTicketLoc As Integer = CInt(LargEcran / 2 + (LargFrmSite * 1.01) - LargFrmSite)
    Dim yTicketLoc As Integer = CInt(HautEcran / 2 - hautFrmSite / 2)
    Dim TheLocalisationOfIden As Integer = FrIdentite.Location.X

    If TheLocalisationOfIden >= xIdenLoc Then
        FrIdentite.Location = New Point(TheLocalisationOfIden - 1, yIdenLoc)
        FrTicket.Location = New Point(TheLocalisationOfIden - 1, yTicketLoc)
    Else
        If FrTicket.Location.X <= xTicketLoc Then
            FrTicket.Location = New Point(TheLocalisationOfIden + 1, yTicketLoc)
        Else
            tmr.Stop()
        End If
    End If

End Sub

但是实际上,它们在第一次本地化时就弹出了(图一),什么都没有。

编辑: 这是最后的Tick代码。现在,在子变量之前声明了变量

Private Sub tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tick

    TheLocalisationOfIden = TheLocalisationOfIden - 2
    TheLocalisationOfIden = TheLocalisationOfTicket + 2

    If TheLocalisationOfIden <= xIdenLoc Then
        FrIdentite.Location = New Point(TheLocalisationOfIden, yIdenLoc)
    Else
        If FrTicket.Location.X >= xTicketLoc Then
            FrTicket.Location = New Point(TheLocalisationOfTicket, yTicketLoc)
        Else
            tmr.Stop()
        End If
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

缺少两件事:tmr属性和tmr事件处理程序 在

Private Sub Fr_Parent_Load

添加tmr属性(如tmr.Enabled和tmr.Interval) 然后像这样修改tmr事件处理程序:

Private Sub tmr_Tick() Handles tmr.Tick