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

WallPaper Changer for .NET

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.61/5 (12投票s)

2005年4月13日

3分钟阅读

viewsIcon

104811

downloadIcon

3408

一篇关于更改桌面壁纸的文章。

Sample Image

引言

这是该应用程序的第二个版本。我根据观看者的评论做了一些更改。

这是一个尝试创建一个简单的应用程序,它有一个简单的GUI,并且仍然具有所有所需的功能。我想要一个应用程序,它可以自行收集图像,而免去我这个功能。

本文讨论了以下主题

  1. 使用WinAPI设置壁纸。
  2. 使用xmlWriter进行文件写入。
  3. 图像文件类型更改。
  4. 从目录中递归获取所有文件。
  5. 在集合中分离文件和目录。
  6. 实现拖放场景。
  7. 在系统托盘中显示应用程序。

背景

要更改桌面背景,应用程序需要使用WinAPI SystemParametersInfo 并为其提供所需的图像参数。 唯一的问题是 API 仅接受 BMP 图像类型。 我的解决方案是从目录中的每个 JPG 或 JPEG 文件创建一个 BMP。

使用代码

  1. 使用WinAPI设置壁纸

    API 函数 "SystemParametersInfo" 有许多用途。 我将使用SPI_SETDESKWALLPAPER参数,该参数告诉系统更改壁纸。SPIF_SENDCHANGE 参数告诉系统向所有打开的窗口发送有关壁纸更改的确认消息。

    //
    //
    nResult = WinAPI.SystemParametersInfo(SPI_SETDESKWALLPAPER, 
                                 1, fileName, SPIF_SENDCHANGE);
    //
  2. 使用xmlTextWriter进行文件写入

    我的xml文件保存了上次选择的作为壁纸的图像。 每次更换壁纸后,xml文件都将被更新,以便下次应用程序使用正确的图像。 xmlTextwriter 必须以 writer.WriteStartElement 开头,以 writer.WriteEndElement 结尾。 在中间,每个元素都使用其名称和值添加:“picIndex”,tempPicIndex.ToString()flush 方法强制编写器立即写入数据。 Close 方法关闭编写器。

    XmlTextWriter writer = 
           new XmlTextWriter(m_settingsPath + "\\settings.xml", null);
    writer.Formatting = Formatting.Indented;
    writer.Indentation = 4;
    writer.WriteComment("Settings for WallPaper changer program");
    writer.WriteStartElement("settings");
    writer.WriteElementString("picIndex", tempPicIndex.ToString());
    writer.WriteElementString("Interval", tempGlobalInterval.ToString());
    writer.WriteEndElement();
    writer.Flush();
    writer.Close();
  3. 图像文件类型更改

    首先,您需要使用Bitmap构造函数在内存中创建一个 BMP 文件,然后您需要将新文件保存到本地 HD。

    Bitmap theImage = new Bitmap(fileName);
    theImage.Save (localPath + "\\new.bmp",
          System.Drawing.Imaging.ImageFormat.Bmp);
  4. 从目录中递归获取所有文件

    为了从所有子目录中获取所有文件,应用程序需要使用 Directory 类。 我使用了一个递归方法来收集所有文件。 我将所有文件从每个子目录获取到一个 string 数组中,并将它们中的每一个添加到 ListBox 中。

    string[] dirs = Directory.GetFiles(localPath , fileType);
    foreach (string dir in dirs){
        mycheckedListBox.Items.Add (dir ,CheckState.Checked);
    }
  5. 在集合中分离文件和目录

    对于每个文件,应用程序通过 FileAttributes 属性检查它是一个文件还是一个目录。 在以下代码中,应用程序转换名为 dir 的字符串,并通过其 fileattributes.Directory 标志检查它是一个文件还是一个目录

    if (File.GetAttributes(dir) == FileAttributes.Directory) bAns = true;
  6. 实现拖放场景

    首先,在Form Designer模式下,listbox.allowDragform.allowDrag 必须设置为 true。 下一步是更改 DragAndDrop 效果(和光标的图标)并允许删除场景继续。 否则,将永远不会到达 DragDrop 事件。

    private void mycheckedListBox_DragEnter(object sender, 
                       System.Windows.Forms.DragEventArgs e) 
    {
      if (e.Data.GetDataPresent(DataFormats.FileDrop, false)==true) 
          e.Effect = DragDropEffects.All;
    }

    最后,我实现了 DragDrop 事件以将所有文件获取到一个 string ArrayList

    private void mycheckedListBox_DragDrop(object sender, 
                                System.Windows.Forms.DragEventArgs e)
    {
       string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
       foreach (string dirfile in files) { ....}
    }
  7. 在系统托盘中显示应用程序

    C# 将所有过程封装成一个非常简单的过程。 您所需要做的就是将 notifyIcon 添加到您的应用程序的窗体,并将其与 contextMenu 相关联。 下一步是实现 Form.resize 并确保在最小化时窗口将消失

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      if (WindowState == FormWindowState.Minimized) 
        this.Hide();
    }

    您可能还想实现 notifyIcon.DoubleClick

    private void notifyIcon1_DoubleClick(object sender, System.EventArgs e) 
    {
      this.Show(); 
      this.WindowState = FormWindowState.Normal;
    }
    
© . All rights reserved.