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

壁纸更换器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.83/5 (5投票s)

2012年4月20日

CPOL

3分钟阅读

viewsIcon

20780

downloadIcon

501

一个视觉化的登录界面和桌面背景“换纸器”(更改器),带有JPEG文件压缩功能。

说明

Windows7 ™, VS11™ Beta, VB11™ Beta. 这仅适用于Windows7 ™。该程序在Windows 8™中将过时,因为用户无需该程序即可完成此操作。 因此,目前该程序仅适用于Windows7 ™用户。由于需要注册表项,因此需要以管理员身份运行。 如果您不是管理员或您没有管理员权限,那么此应用程序将向您显示一条消息,要求您应用“以管理员身份运行”权限,然后该应用程序将正常退出,或者换句话说,关闭。

引言

这是一个视觉样式换纸器(更改器),可以更改登录用户界面背景,并且可以更改您的桌面壁纸。 我添加了一个文件压缩面板,因此您可以将一些JPEG压缩到合适的大小(<= 256 KB),因为Windows7 ™ 不使用大于256 KB的图片作为登录界面背景。 您可以通过单击表单顶部的DataGridView 控件中的缩略图来选择登录界面的背景图片。 然后单击“应用”。 DatagridView中的第一个缩略图是Windows7 ™使用的默认背景。 第二个缩略图是当前使用的图片。 这两个将始终是DatagridView中的第一个和第二个(分别),以及其余小于或等于 256 KB(DESIRED_SIZE)的图片。 DESIRED_SIZE 是一个等于 262144 (256 KB) 的常量。 这用于检查网格中加载的所有图像。 如果更大,则会被跳过。

背景

这个想法的背景来自 Mr. Wolfy 版本:2010年1月最佳VB.NET文章Julien Manici 的 WPF 版本,它们很棒。 我的版本模仿了Julien的版本,除了我使用WinForms和VS IDE 工具箱标准组件。 我已经使用我的应用程序一年多了,决定发布它。 但是,此应用程序是在 VS11 beta,VB11 beta 中创建的。 我将发布 VB9.0 和 VB10 的下载。 它们都基本相同,除了设计。

Julien的应用程序有一些不错的效果(MotionCircles 动画)和(即时)文件压缩(仅适用于JPEG),我认为它很棒。 我的有文件压缩,但我们手动完成。 它具有 PS CS3 的文件压缩的外观和感觉。 在压缩文件之前,我们需要给文件起一个保存的名称。 我只是在选定的图片名称中添加字母“a”。 这对我来说效果很好。

用于文件压缩的代码...

Private Sub DoImageCompression()
    If txtSaveAsJpeg.Text = "" Then
        MessageBox.Show("Please fill in the 'Save As *.jpg' Text box " & _ 
          "with '.jpg' at the end.", "Textbox err", _
          MessageBoxButtons.OK, MessageBoxIcon.Information)
        Exit Sub
    End If

    'Create a new bitmap image.
    Dim bmpToJpeg As New Bitmap(lstImages.SelectedItem.ToString())

    'Set a place holder for the memory stream length.
    Dim file_size As Long

    'Set the compression level.
    Dim compression_level As Long = CType(tbSize.Value, Long)

    ' Save the file into a memory stream.
    Dim memory_stream As MemoryStream = _
        SaveJpegIntoStream(bmpToJpeg, _
        compression_level)

    ' See how big it is.
    file_size = memory_stream.Length

    'Display the correct size with 2 decimal places.
    lblFileSize.Text = "File Size : " & _
      Math.Round((file_size / 1024), 2).ToString & " kb"

    'Set the trackbar value in the text box.
    txtTBValue.Text = tbSize.Value.ToString()

    'This boolean is used to keep the this Sub in check
    'until the desired_size is met by the User.
    'You should try and get as close to 256kb as possible.
    If isSaveAsButtonClicked = False Then
        'Dispose of this stuff as we have to start over
        'after exiting the sub.
        'Clean-Up.
        bmpToJpeg.Dispose()
        memory_stream.Dispose()
        Exit Sub
    End If

    If file_size <= DESIRED_SIZE Then
        ' Save the file.
        ' Display the final image.
        My.Computer.FileSystem.WriteAllBytes(mySaveLocation & txtSaveAsJpeg.Text, _
            memory_stream.ToArray(), False)
        picAfter.ImageLocation = mySaveLocation & txtSaveAsJpeg.Text

        'Show the User the most excellent job they are doing.
        MessageBox.Show("File saved!...")

        'Reset and dispose of some stuff.
        bmpToJpeg.Dispose()
        memory_stream.Dispose()
        btnSaveAs.Enabled = False
        isSaveAsButtonClicked = False
    End If

End Sub

当我们拖动滑块时,在MouseUp事件上,建议的大小将显示在滑块下方的文本框中。 您需要多次这样做,直到您尽可能接近256 KB,而不会超过DESIRED_SIZE。 一旦得到你想要的,你的图片就会显示在左侧的原始图片旁边,右侧是压缩文件。

我决定添加标准壁纸更换器,因为这是一个“换纸器”。

使用代码

要设置我们的标准壁纸,我们需要将一些图片添加到我们的列表框中。 *.bmp 曾经是MS的标准,但当Windows Vista推出时,情况发生了变化。 现在我们可以从各种各样的壁纸图片中选择,*.bmp, *.jpg, *.png 等等。

设置和保存我们的壁纸...

Private Sub SetWallpaper(ByVal img As Image)
    imageLocation = My.Computer.FileSystem.CombinePath(_
      My.Computer.FileSystem.SpecialDirectories.MyPictures, WallpaperPath)
    picStandardWP.ImageLocation = imageLocation

    'We could not do this with XP, 2000, or anything before Vista
    'because wallpaper could only be in *.bmp format.
    If imageLocation.EndsWith(".bmp") Then
        img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Bmp)
    ElseIf imageLocation.EndsWith(".jpg") Then
        img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Jpeg)
    ElseIf imageLocation.EndsWith(".png") Then
        img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Png)
    End If

    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0&, imageLocation, _
                         SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
End Sub

首次运行和加载

首次运行 The Paper Hanger 时,如果它没有管理员权限,它将显示一条消息说明这一事实,并让您知道它将在关闭后关闭,之后您可以进行右键单击操作并在“安全”选项卡下添加“以管理员身份运行”。 这是执行此操作的代码....

Private Sub frmPaperHanger_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
    Dim myPrincipal As WindowsPrincipal = _
        CType(System.Threading.Thread.CurrentPrincipal, WindowsPrincipal)

    If (myPrincipal.IsInRole(WindowsBuiltInRole.Administrator) = True) Then

        pnlLogonUI.Location = New Point(96, 179)
        pnlLogonUI.Show()

        pnlStandardWallpaper.Location = ptStandardWP
        pnlStandardWallpaper.Hide()

        pnlOptions.Location = ptOptions
        pnlOptions.Hide()

        pnlCompressFiles.Location = ptCompressionFiles
        pnlCompressFiles.Hide()

        pnlAbout.Location = ptAbout
        pnlAbout.Hide()

        LoadAndInitialize()
    End If
Catch ex As Exception
    MessageBox.Show("The Program does not currently have (runas admin) " & _ 
      "permission. Please set this.", "UAC Permission Needed", _
      MessageBoxButtons.OK, MessageBoxIcon.Information)
    Application.Exit()
End Try

关注点

历史

  • 上传 The Paper Hanger: 04·20·2012。
© . All rights reserved.