*/
Want to see what people are talking about? See the latest forum posts.
*/

View FuzzyScreen\FuzzyScreen\Main.vb

Vb.Net screensaver 1.0

Submitted By: FuzzyDaze
Rating: starstarstarstarstar (Rate It)


Imports FileWriter.FileWriter
Imports System.io

Module Startup
        '*****************************************
        '** The FileWriter DLL is capable of writing to files, creating files and folders and checking whether they exist.
        '** It has some very nice logging capabilities, which are not used here.
        '** This functionality used can be replaced by your custom filewriting.
        '** The DLL must be in the same folder as your executable.
        '**
        '** Rename the FuzzyScreen.exe file to FuzzySCreen.scr to be able to use it as a screensaver.
        '*****************************************
       
        '** Define the xml inputfile
        Public strInputFile As String = Application.StartupPath & "\FuzzyScreen.xml"
       
        '** Set the default options
        Public intDelay As Integer = 3
        Public booRandomize As Boolean = False
       
        Public datScreenData As New DataSet
        Public intImageCount As Integer = 0
        Public strImageList() As String
        Public strTableName As String
        Public strColumnName As String

        Public Sub Main(ByVal args As String())
                '** This is the first to start. Check to see if the file exists
                If CheckFile(strInputFile) Then
                        '** Load the inputfile and check it's integrity
                        If LoadInputFile() Then
                                '** Check the commandline parameters
                                If args.Length > 0 Then
                                        If args(0).ToLower.Trim().Substring(0, 2) = "/c" Then
                                                '** This parameter is used by Windows screensaver "Configure" option
                                                ShowOptions()
                                        End If

                                        If args(0).ToLower = "/s" Then
                                                '** This parameter is used by Windows screensaver execute action
                                                ShowScreenSaver()
                                        End If
                                Else
                                        '** If no parameters are given, the user must have clicked the executable.
                                        ShowScreenSaver()
                                End If
                        Else
                                '** There must be something wrong with the inputfile. Show options so the user can save a new one.
                                ShowOptions()
                        End If
                Else
                        '** The file was not found. Create a new one.
                        If CreateXmlFile(Nothing) = True Then
                                '** The new (empty) file was succesfully created. Start over again.
                                Main(args)
                        Else
                                MessageBox.Show("Error creating XML file. Exiting program")
                        End If
                End If
                Application.Exit()
        End Sub

        Private Sub ShowScreenSaver()
                '** Show the screensaver form
                Dim screenSaverForm As New FuzzyScreen.frmFuzzyScreen
                screenSaverForm.ShowDialog()
        End Sub

        Private Sub ShowOptions()
                '** Show the options form
                Dim userOptionsForm As New frmOptions
                userOptionsForm.ShowDialog()
        End Sub

        Public Function LoadInputFile() As Boolean
                Dim intAddImage As Integer

                '** read the xml Inputfile to a dataset
                datScreenData.ReadXml(strInputFile)
                'ShowData(datScreenData) '** use this option to show exactly what's in the dataset. Do NOT use in production.
               
                '** check the data in the dataset and if it's OK, do your stuff.
                If CheckData(datScreenData) = True Then
                        Try
                                '** load the settings into memory
                                intDelay = datScreenData.Tables("ScreenSaverData").Rows(0).Item("Delay")
                                booRandomize = datScreenData.Tables("ScreenSaverData").Rows(0).Item("Randomize")
                        Catch ex As Exception
                                MessageBox.Show("Error reading the Delay from the inputfile: " & strInputFile & vbCrLf & _
                                  "Delete the inputfile to have a new one automatically created")
                        End Try
                        intImageCount = datScreenData.Tables(strTableName).Rows.Count
                        ReDim strImageList(intImageCount)

                        '** create a new dataset to hold only the images that need to be displayed. this way you are able to randomize the images.
                        Dim objData As New DataSet
                        Dim Table1 As New DataTable("SortTable")
                        Dim Column1 As New DataColumn("ImageName")
                        Dim Column2 As New DataColumn("SortValue")

                        objData.Tables.Add(Table1)
                        objData.Tables("SortTable").Columns.Add(Column1)
                        objData.Tables("SortTable").Columns.Add(Column2)


                        Dim rndRandom As New Random
                        Dim intRandom As Integer
                        If intImageCount > 0 Then
                                If datScreenData.Tables(strTableName).Rows(0).Item(strColumnName) = Nothing Then
                                        Return False
                                Else
                                        For intAddImage = 0 To intImageCount - 1
                                                '** Add a new row for every image you need to display
                                                Dim tRow As DataRow = objData.Tables("SortTable").NewRow
                                                objData.Tables("SortTable").Rows.Add(tRow)
                                               
                                                '** Add the image to the new row
                                                objData.Tables("SortTable").Rows(intAddImage).Item("ImageName") = datScreenData.Tables(strTableName).Rows(intAddImage).Item(strColumnName)
                                               
                                                '** If randomize is set to True add a rondom value to the SortValue column, else use a sequential value
                                                If booRandomize Then
                                                        intRandom = rndRandom.Next(0, 5000)
                                                        objData.Tables("SortTable").Rows(intAddImage).Item("SortValue") = intRandom
                                                Else
                                                        objData.Tables("SortTable").Rows(intAddImage).Item("SortValue") = intAddImage
                                                End If
                                        Next
                                        '** Use a dataview to sort the table using the random values.
                                        Dim objDataView As New DataView
                                        objDataView.Table = objData.Tables("SortTable")
                                        objDataView.Sort = "SortValue"

                                        '** Add the images to the list of images to display
                                        For intAddImage = 0 To intImageCount - 1
                                                strImageList(intAddImage) = objDataView.Item(intAddImage).Item("ImageName")
                                        Next
                                        Return True
                                End If
                        Else
                                Return False
                        End If
                Else
                        Return False
                End If

        End Function

        Public Function CreateXmlFile(ByVal strImages As String) As Boolean
                '** Create or update the xml inputfile.
                Dim strXmlText As String
                strXmlText = "<?xml version=""1.0"" standalone=""yes""?>" & vbCrLf
                strXmlText &= "<ScreenSaverData>" & vbCrLf
                strXmlText &= " <Delay>" & intDelay & "</Delay>" & vbCrLf
                strXmlText &= " <Randomize>" & booRandomize & "</Randomize>" & vbCrLf
                strXmlText &= " <ImageList>" & vbCrLf
                If strImages = Nothing Then
                        strXmlText &= "  <ImageName></ImageName>" & vbCrLf
                Else
                        strXmlText &= strImages
                End If
                strXmlText &= " </ImageList>" & vbCrLf
                strXmlText &= "</ScreenSaverData>" & vbCrLf
                CreateXmlFile = CreateFile(strXmlText, strInputFile)
        End Function

        Private Sub ShowData(ByVal objData As DataSet)
                '** This sub shows the data that is in the original dtaset after reading from the xml inputfile.
                Dim intTableCount As Integer
                Dim intRowCount As Integer
                Dim intColumnCount As Integer

                For intTableCount = 0 To objData.Tables.Count - 1
                        For intRowCount = 0 To objData.Tables(intTableCount).Rows.Count - 1
                                For intColumnCount = 0 To objData.Tables(intTableCount).Columns.Count - 1
                                        MessageBox.Show("Table: " & vbTab & intTableCount & " Name: " & objData.Tables(intTableCount).TableName & vbCrLf _
                                        & "Row: " & vbTab & intRowCount & vbCrLf _
                                        & "Column: " & vbTab & intColumnCount & " Name: " & objData.Tables(intTableCount).Columns(intColumnCount).ColumnName & vbCrLf _
                                        & "Value: " & vbTab & objData.Tables(intTableCount).Rows(intRowCount).Item(intColumnCount))
                                Next
                        Next
                Next
        End Sub

        Public Function CheckData(ByVal objData As DataSet) As Boolean
                '** This function checks to see if the xml data has the correct format
                Dim intTableCount As Integer
                Dim intColumnCount As Integer

                For intTableCount = 0 To objData.Tables.Count - 1
                        For intColumnCount = 0 To objData.Tables(intTableCount).Columns.Count - 1
                                If objData.Tables(intTableCount).TableName = "ImageList" Then
                                        '** If there is no or only one image in the list, this option will be true
                                        strTableName = "ImageList"
                                        If objData.Tables("ImageList").Columns(0).ColumnName = "ImageName" Then
                                                strColumnName = "ImageName"
                                                Return True
                                                Exit Function
                                        End If
                                ElseIf objData.Tables(intTableCount).TableName = "ImageName" Then
                                        '** If there is more than one image in the list, this option will be true
                                        strTableName = "ImageName"
                                        If objData.Tables("ImageName").Columns(0).ColumnName = "ImageName_Text" Then
                                                strColumnName = "ImageName_Text"
                                                Return True
                                                Exit Function
                                        End If
                                End If
                        Next
                Next
                Return False
        End Function

End Module

corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.