65.9K
CodeProject 正在变化。 阅读更多。
Home

自动化桌面背景更换器

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (3投票s)

2007年12月15日

CPOL

2分钟阅读

viewsIcon

33207

downloadIcon

979

这个项目将向您展示如何制作一个程序,该程序可以在您登录时随机选择一张背景图片,并且可以让您简单地选择一个背景,或者每隔一分钟到一小时为您随机选择一个背景。

引言

是否曾经遇到过找不到合适的图片作为背景? 好吧,不用再找了。 这个项目将向您展示如何制作一个程序,该程序可以在您登录时随机选择一张背景图片,并且可以让您简单地选择一个背景,或者每隔一分钟到一小时为您随机选择一个背景。 此外,此项目还包括关于如何将图像转换为BMP格式的说明。

背景

对 VB 2005 及更高版本有完整且扎实的知识。

Using the Code

Sample Image

首先开始一个新项目,并将其命名为 "随机桌面"。
将窗体命名为 frmRD。现在创建一个新类并将其命名为 Wallpaper.vb,并创建一个名为 frmConverter 的新窗体。

好的。现在在 frmRD 上,放置并命名以下属性,使其看起来像上图所示。

  • 图片框 Preview
  • 组合框 styleComboBox
  • 按钮 btnConverter
  • 按钮 btnPlay
  • 按钮 btnNext(字体是 Marlett,然后要输入箭头,输入 44
  • 按钮 btnRefresh
  • 按钮 btnBackround
  • 列表框 PictureSelection
  • NumericUpDown ImageTime
  • 复选框 CountdownSwitch

frmRD 的代码如下

Public Class frmRD

'Variables
    Dim FILE As String
    Dim Delete As Byte
    Dim DIR As String = My.Computer.FileSystem.SpecialDirectories.MyPictures & _
        "\RandomBG\"
    Dim SS As Byte = 0
    Dim IntervalSS As Integer
    Dim RandomImage As Integer
    Dim CurrentImage As Integer
    Dim Max As Integer

    Private Sub frmRD_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles MyBase.Load

'On Load, find all files in the My Pictures\RandomBG\ folder
    For Each File As String In My.Computer.FileSystem.GetFiles_
        (My.Computer.FileSystem.SpecialDirectories.MyPictures & "\RandomBG\")
            PictureSelection.Items.Add(My.Computer.FileSystem.GetFileInfo(File).Name)
        Next

        styleComboBox.DataSource = System.Enum.GetNames(GetType(Wallpaper.Style))
        styleComboBox.SelectedIndex = 2

'On load, randomly select a background image from the My Pictures\RandomBG\ folder
    Try
            Max = PictureSelection.Items.Count

            RandomImage = GetRandomNumber(1, Max)

            PictureSelection.SelectedIndex = RandomImage - 1

            FILE = PictureSelection.Text

            Preview.Image = Image.FromFile_
                (My.Computer.FileSystem.SpecialDirectories.MyPictures & _
                "\RandomBG\" & FILE)

            Dim w As Wallpaper = New Wallpaper()
            w.SetWallpaper(My.Computer.FileSystem.SpecialDirectories.MyPictures & _
                "\RandomBG\" & FILE, CType(System.Enum.Parse_
                (GetType(Wallpaper.Style), styleComboBox.Text), Wallpaper.Style))
        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub PictureSelection_MouseClick(ByVal sender As System.Object, ByVal e _
        As System.Windows.Forms.MouseEventArgs) Handles PictureSelection.MouseClick

'Preview an image by clicking it's name in the listbox
    Try
            FILE = PictureSelection.Text

            Preview.Image = Image.FromFile(DIR & FILE)
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub PictureSelection_MouseDoubleClick(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles PictureSelection.MouseDoubleClick

'Remove a file from the list by double clicking it
    PictureSelection.Items.Remove(PictureSelection.SelectedItem)
    End Sub



    Private Sub btnBackround_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles btnBackround.Click

'Set the currently previewed wallpaper as the background
    Dim w As Wallpaper = New Wallpaper()
        w.SetWallpaper(My.Computer.FileSystem.SpecialDirectories.MyPictures & _
            "\RandomBG\" & FILE, CType(System.Enum.Parse(GetType(Wallpaper.Style), _
            styleComboBox.Text), Wallpaper.Style))

    End Sub


    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles btnRefresh.Click

'Refreshes the listbox, in case new files have been added by the converter
    PictureSelection.Items.Clear()

        For Each File As String In My.Computer.FileSystem.GetFiles(DIR)
            PictureSelection.Items.Add(My.Computer.FileSystem.GetFileInfo(File).Name)
        Next
    End Sub

'When the GetRandomNumber(min, max) function is called, it chooses a random number 
'from the minimum to the maximum specified
#Region "Random Number Generator"
    Private Function GetRandomNumber(ByVal vnMinimumNumber As Integer, _
        ByVal vnMaximumNumber As Integer)
        Randomize()
        GetRandomNumber = CInt(Int((vnMaximumNumber - vnMinimumNumber + 1) * _
            Rnd() + vnMinimumNumber))
    End Function
#End Region

    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles btnPlay.Click

'Sets up the timers, and prepares the Forms vital controls for the auto changer
    If SS = 0 Then
            Counter = ImageTime.Value * 60
            Speed = ImageTime.Value * 60
            IntervalSS = (ImageTime.Value * 60000)
            Slideshow.Interval = IntervalSS
            Countdown.Enabled = True
            Slideshow.Enabled = True
            ImageTime.Enabled = False
            btnBackround.Enabled = False
            styleComboBox.Enabled = False
            btnPlay.Text = "Stop"
            SS = 1
        ElseIf SS = 1 Then
            Slideshow.Enabled = False
            Countdown.Enabled = False
            ImageTime.Enabled = True
            btnBackround.Enabled = True
            styleComboBox.Enabled = True
            btnPlay.Text = "Play"
            SS = 0
        End If
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles btnNext.Click

'Randomly skips ahead one image in the sequence, and sets it as the background
    Try
            Max = PictureSelection.Items.Count
            CurrentImage = RandomImage
            RandomImage = GetRandomNumber(1, Max)

            If RandomImage = CurrentImage Then
                Exit Sub
            End If

            PictureSelection.SelectedIndex = RandomImage - 1

            FILE = PictureSelection.Text

            Dim w As Wallpaper = New Wallpaper()
            w.SetWallpaper(DIR & FILE, CType(System.Enum.Parse(GetType(Wallpaper.Style), _
            styleComboBox.Text), Wallpaper.Style))

        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub Slideshow_Tick(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Slideshow.Tick

'Randomly chooses a new image and applies it every pre-determined tick
    Try
            Max = PictureSelection.Items.Count
            CurrentImage = RandomImage
            RandomImage = GetRandomNumber(1, Max)

            If RandomImage = CurrentImage Then
                Exit Sub
            End If

            PictureSelection.SelectedIndex = RandomImage - 1

            FILE = PictureSelection.Text

            Dim w As Wallpaper = New Wallpaper()
            w.SetWallpaper(DIR & FILE, CType(System.Enum.Parse(GetType(Wallpaper.Style), _
            styleComboBox.Text), Wallpaper.Style))

        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles btnConvert.Click

'Runs the conversion form
    frmConverter.Show()
        Me.Hide()
    End Sub

'Variables just for the counter
    Dim Counter As Integer
    Dim Speed As Integer
    Dim Switch As Byte = 0

    Private Sub Countdown_Tick(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Countdown.Tick

'Determines the message when the counter is switched on
    Counter = Counter - 1
        If Switch = 1 Then
            If Counter > 0 Then
                Me.Text = "(" & Counter & " Seconds Remaining)"
            ElseIf Counter = 1 Then
                Me.Text = "(" & Counter & " Second Remaining)"
            ElseIf Counter = 0 Then
                Me.Text = "(" & Counter & " Seconds Remaining)"
                Counter = Speed
            End If
        Else

        End If
    End Sub

    Private Sub CountdownSwitch_CheckedChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles CountdownSwitch.CheckedChanged

'Determines whether the counter is switched on
    If CountdownSwitch.Checked = True Then
            Switch = 1
        Else
            Switch = 0
            Me.Text = " Random Desktop"
        End If
    End Sub
End Class

Wallpaper 类的代码如下

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports Microsoft.Win32

Public Class Wallpaper
    Const SPI_SETDESKWALLPAPER As Integer = 20
    Const SPIF_UPDATEINIFILE As Integer = &H1&
    Const SPIF_SENDWININICHANGE As Integer = &H2&

    "user32") /> _
    Public Shared Function SystemParametersInfo(ByVal uAction As Integer, _
        ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) _
            As Integer
    End Function

    Public Enum Style As Integer
        Tiled
        Centered
        Stretched
    End Enum

    Public Sub SetWallpaper(ByVal path As String, ByVal selectedStyle As Style)
        Dim key As RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey_
            ("Control Panel\Desktop", True)

        Select Case selectedStyle
            Case Style.Stretched
                key.SetValue("WallpaperStyle", "2")
                key.SetValue("TileWallpaper", "0")

            Case Style.Centered
                key.SetValue("WallpaperStyle", "1")
                key.SetValue("TileWallpaper", "0")

            Case Style.Tiled
                key.SetValue("WallpaperStyle", "1")
                key.SetValue("TileWallpaper", "1")
        End Select

        SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE _
            Or SPIF_SENDWININICHANGE)
    End Sub
End Class
Sample Image

我们需要转换器的原因是 Windows XP 不允许您使用任何类型的图像作为背景,除了 *.bmp。 通常,当您选择一个图像作为背景时,Windows 会将其转换为 *.bmp ,然后使用它。

现在在 frmConverter 上,放置并命名以下属性,使其看起来像上图所示。

  • 图片框 Preview
  • 组合框 FileType
  • 按钮 btnConvert
  • 按钮 btnFolder
  • 按钮 btnRefresh
  • 列表框 PictureSelection

frmConverter 的代码如下

Public Class frmConverter

'Variables for retrieving and saving files
    Dim DIR As String = My.Computer.FileSystem.SpecialDirectories.MyPictures & "\"
    Dim FILE As String

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles btnConvert.Click

'Saves the selected image as a .bmp to the My Pictures\RandomBG\ directory
    On Error Resume Next
        Dim d As New System.Drawing.Bitmap(Preview.Image)
        Dim tmp As String = PictureSelection.Text
        tmp = tmp.ToUpper
        If PictureSelection.SelectedIndex < 0 Then
            My.Computer.Audio.Play("C:\Windows\Media\ding.wav")
            Exit Sub
        End If
        tmp = Microsoft.VisualBasic.Replace(UCase(tmp), FileType.Text, "BMP")
        d.Save(My.Computer.FileSystem.SpecialDirectories.MyPictures & _
        "\RandomBG\" & tmp, System.Drawing.Imaging.ImageFormat.Bmp)
        Dim answer As MsgBoxResult
        answer = MsgBox("Are you done converting?", MsgBoxStyle.YesNo)
        If answer = MsgBoxResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub frmConverter_Load(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles MyBase.Load

'On Load, Read all the items in the (Default) My Pictures folder to the listbox
    For Each File As String In My.Computer.FileSystem.GetFiles_
        (My.Computer.FileSystem.SpecialDirectories.MyPictures)
            PictureSelection.Items.Add(My.Computer.FileSystem.GetFileInfo(File).Name)
        Next
    End Sub

    Private Sub PictureSelection_MouseClick(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles PictureSelection.MouseClick

'Preview an image for conversion
    Try
            FILE = PictureSelection.Text

            Preview.Image = Image.FromFile(DIR & FILE)
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub PictureSelection_MouseDoubleClick(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles PictureSelection.MouseDoubleClick

'Double click a file in the listbox to remove it
     PictureSelection.Items.Remove(PictureSelection.SelectedItem)
    End Sub

    Private Sub btnFolder_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles btnFolder.Click

'Select a folder to get images from, (the default is My Pictures)
    FolderBrowser.ShowDialog()
        DIR = FolderBrowser.SelectedPath & "\"

        PictureSelection.Items.Clear()

        For Each File As String In My.Computer.FileSystem.GetFiles(DIR)
            PictureSelection.Items.Add(My.Computer.FileSystem.GetFileInfo(File).Name)
        Next
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles btnRefresh.Click

'Refresh the items in the listbox, in case files have been added to the selected folder
    PictureSelection.Items.Clear()

        For Each File As String In My.Computer.FileSystem.GetFiles(DIR)
            PictureSelection.Items.Add(My.Computer.FileSystem.GetFileInfo(File).Name)
        Next
    End Sub

    Private Sub frmConverter_FormClosing(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) _
        Handles MyBase.FormClosing

'When the converter closes, frmRD will reappear
    frmRD.Show()
    End Sub
End Class

关注点

这个项目给我的家庭带来了好处。现在我的父母在登录他们的电脑时有了期待。

历史

  • 2007年12月15日:文章发布
© . All rights reserved.