当窗口首次出现时,在wpf中创建一个秒表

时间:2013-07-04 07:50:47

标签: wpf vb.net visual-studio-2012

我正在wpf(VB.NET- Visual Studio 2012)中构建一个幻灯片拼图,我已经为它编写了代码,但我需要有一个秒表,它将出现在第一次加载的窗口中。我还需要秒表将会过去并在用户洗牌时重新开始。请给我一些写正确代码的技巧。谢谢。 这是幻灯片拼图的代码:

公共等级2     Dim win(15)As BitmapImage     Dim wrong(15)As BitmapImage     Dim mess(15)As BitmapImage     昏暗的图像(15)作为图像     Private Sub MainWindow_Loaded(sender as Object,e As RoutedEventArgs)处理Me.Loaded

    Dim h1 As New BitmapImage
    h1.BeginInit()
    h1.UriSource = New Uri("images2/h1.gif", UriKind.RelativeOrAbsolute)
    h1.EndInit()

    Dim h2 As New BitmapImage
    h2.BeginInit()
    h2.UriSource = New Uri("images2/h2.gif", UriKind.RelativeOrAbsolute)
    h2.EndInit()

    Dim h3 As New BitmapImage
    h3.BeginInit()
    h3.UriSource = New Uri("images2/h3.gif", UriKind.RelativeOrAbsolute)
    h3.EndInit()

    Dim h4 As New BitmapImage
    h4.BeginInit()
    h4.UriSource = New Uri("images2/h4.gif", UriKind.RelativeOrAbsolute)
    h4.EndInit()

    Dim h5 As New BitmapImage
    h5.BeginInit()
    h5.UriSource = New Uri("images2/h5.gif", UriKind.RelativeOrAbsolute)
    h5.EndInit()

    Dim h6 As New BitmapImage
    h6.BeginInit()
    h6.UriSource = New Uri("images2/h6.gif", UriKind.RelativeOrAbsolute)
    h6.EndInit()

    Dim h7 As New BitmapImage
    h7.BeginInit()
    h7.UriSource = New Uri("images2/h7.gif", UriKind.RelativeOrAbsolute)
    h7.EndInit()

    Dim h8 As New BitmapImage
    h8.BeginInit()
    h8.UriSource = New Uri("images2/h8.gif", UriKind.RelativeOrAbsolute)
    h8.EndInit()

    Dim h9 As New BitmapImage
    h9.BeginInit()
    h9.UriSource = New Uri("images2/h9.gif", UriKind.RelativeOrAbsolute)
    h9.EndInit()

    Dim h10 As New BitmapImage
    h10.BeginInit()
    h10.UriSource = New Uri("images2/h10.gif", UriKind.RelativeOrAbsolute)
    h10.EndInit()

    Dim h11 As New BitmapImage
    h11.BeginInit()
    h11.UriSource = New Uri("images2/h11.gif", UriKind.RelativeOrAbsolute)
    h11.EndInit()

    Dim h12 As New BitmapImage
    h12.BeginInit()
    h12.UriSource = New Uri("images2/h12.gif", UriKind.RelativeOrAbsolute)
    h12.EndInit()

    Dim h13 As New BitmapImage
    h13.BeginInit()
    h13.UriSource = New Uri("images2/h13.gif", UriKind.RelativeOrAbsolute)
    h13.EndInit()

    Dim h14 As New BitmapImage
    h14.BeginInit()
    h14.UriSource = New Uri("images2/h14.gif", UriKind.RelativeOrAbsolute)
    h14.EndInit()

    Dim h15 As New BitmapImage
    h15.BeginInit()
    h15.UriSource = New Uri("images2/h15.gif", UriKind.RelativeOrAbsolute)
    h15.EndInit()

    win(0) = h1
    win(1) = h2
    win(2) = h3
    win(3) = h4
    win(4) = h5
    win(5) = h6
    win(6) = h7
    win(7) = h8
    win(8) = h9
    win(9) = h10
    win(10) = h11
    win(11) = h12
    win(12) = h13
    win(13) = h14
    win(14) = h15
    win(15) = Nothing

    wrong(0) = h4
    wrong(1) = h1
    wrong(2) = h7
    wrong(3) = h14
    wrong(4) = h5
    wrong(5) = h13
    wrong(6) = h8
    wrong(7) = h10
    wrong(8) = h6
    wrong(9) = h2
    wrong(10) = h12
    wrong(11) = h15
    wrong(12) = h3
    wrong(13) = h11
    wrong(14) = h9
    wrong(15) = Nothing



    images(0) = flower0
    images(1) = flower1
    images(2) = flower2
    images(3) = flower3
    images(4) = flower4
    images(5) = flower5
    images(6) = flower6
    images(7) = flower7
    images(8) = flower8
    images(9) = flower9
    images(10) = flower10
    images(11) = flower11
    images(12) = flower12
    images(13) = flower13
    images(14) = flower14
    images(15) = flower15

    For i As Integer = 0 To wrong.Length - 1
        images(i).Source = wrong(i)
    Next

End Sub

Sub checkwin()
    Dim flag As Boolean = False

    For i As Integer = 0 To win.Length - 1
        If mess(i) Is win(i) Then
            flag = True
        Else
            flag = False
            Exit For
        End If
    Next
    If flag = True Then
        lbl2.Content = "You Win!!"
    End If

End Sub

Sub checkcells(ByRef pic1 As Image, ByRef pic2 As Image)

    If pic2.Source Is Nothing Then
        pic2.Source = pic1.Source
        pic1.Source = Nothing
    End If


End Sub


Sub update()

    For i As Integer = 0 To mess.Length - 1
        mess(i) = images(i).Source

    Next
End Sub


Private Sub flower0_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower0.MouseDown
    checkcells(flower0, flower1)
    checkcells(flower0, flower4)
    update()
    checkwin()
End Sub

Private Sub flower1_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower1.MouseDown
    checkcells(flower1, flower0)
    checkcells(flower1, flower2)
    checkcells(flower1, flower5)
    update()
    checkwin()
End Sub

Private Sub flower2_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower2.MouseDown
    checkcells(flower2, flower1)
    checkcells(flower2, flower3)
    checkcells(flower2, flower6)
    update()
    checkwin()
End Sub

Private Sub flower3_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower3.MouseDown
    checkcells(flower3, flower2)
    checkcells(flower3, flower7)

    update()
    checkwin()
End Sub

Private Sub flower4_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower4.MouseDown
    checkcells(flower4, flower0)
    checkcells(flower4, flower5)
    checkcells(flower4, flower8)

    update()
    checkwin()
End Sub

Private Sub flower5_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower5.MouseDown
    checkcells(flower5, flower1)
    checkcells(flower5, flower4)
    checkcells(flower5, flower6)
    checkcells(flower5, flower9)
    update()
    checkwin()
End Sub

Private Sub flower6_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower6.MouseDown
    checkcells(flower6, flower2)
    checkcells(flower6, flower5)
    checkcells(flower6, flower7)
    checkcells(flower6, flower10)
    update()
    checkwin()
End Sub

Private Sub flower7_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower7.MouseDown
    checkcells(flower7, flower3)
    checkcells(flower7, flower6)
    checkcells(flower7, flower11)
    update()
    checkwin()
End Sub

Private Sub flower8_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower8.MouseDown
    checkcells(flower8, flower4)
    checkcells(flower8, flower9)
    checkcells(flower8, flower12)
    update()
    checkwin()
End Sub

Private Sub flower9_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower9.MouseDown
    checkcells(flower9, flower5)
    checkcells(flower9, flower8)
    checkcells(flower9, flower10)
    checkcells(flower9, flower13)
    update()
    checkwin()
End Sub

Private Sub flower10_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower10.MouseDown
    checkcells(flower10, flower6)
    checkcells(flower10, flower9)
    checkcells(flower10, flower11)
    checkcells(flower10, flower14)
    update()
    checkwin()
End Sub

Private Sub flower11_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower11.MouseDown
    checkcells(flower11, flower7)
    checkcells(flower11, flower10)
    checkcells(flower11, flower15)
    update()
    checkwin()
End Sub

Private Sub flower12_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower12.MouseDown
    checkcells(flower12, flower8)
    checkcells(flower12, flower13)
    update()
    checkwin()
End Sub

Private Sub flower13_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower13.MouseDown
    checkcells(flower13, flower9)
    checkcells(flower13, flower12)
    checkcells(flower13, flower14)
    update()
    checkwin()
End Sub

Private Sub flower14_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower14.MouseDown
    checkcells(flower14, flower10)
    checkcells(flower14, flower13)
    checkcells(flower14, flower15)
    update()
    checkwin()
End Sub

Private Sub flower15_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles flower15.MouseDown
    checkcells(flower15, flower11)
    checkcells(flower15, flower14)
    update()
    checkwin()
End Sub

Private Sub btnmix_Click(sender As Object, e As RoutedEventArgs) Handles btnmix.Click
    Randomize()
    Dim num(15) As Integer
    Dim rnd1 As Integer

    For i As Integer = 0 To 15
        rnd1 = Int(Rnd() * (16 - 1) + 1)
        Dim flag As Boolean = False
        Do While flag = False
            For x As Integer = 0 To 15
                If rnd1 = num(x) Then
                    flag = True

                End If
            Next

            If flag = True Then
                flag = False
                rnd1 = Int(Rnd() * (16 - 1) + 1)
            Else
                flag = True
            End If


        Loop
        num(i) = rnd1
        Dim h16 As New BitmapImage
        h16.BeginInit()
        h16.UriSource = New Uri("images2/h" & rnd1 & ".gif", UriKind.RelativeOrAbsolute)
        h16.EndInit()
        images(i).Source = h16
    Next


End Sub

'Private Sub hint_Click(sender As Object, e As RoutedEventArgs) Handles hint.Click
'    Dim monaLisa As New BitmapImage
'    monaLisa.BeginInit()
'    monaLisa.UriSource = New Uri("images/monalisa.jpg", UriKind.RelativeOrAbsolute)
'    monaLisa.EndInit()
'    showMona.Source = monaLisa
'End Sub

结束班

1 个答案:

答案 0 :(得分:0)

如果您在类根目录中制作秒表声明,则在window.loaded事件中启动并启动它,然后在用户随机播放拼图时重置它