基于C#实现设置桌面背景功能

.NET
268
0
0
2023-07-07
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

public partial class Form : Form
{
    public Form()
    {
        InitializeComponent();
    }

    #region 调用API
    [DllImport("user.dll", EntryPoint = "SystemParametersInfoA")]
    static extern Int SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni);
    private const int SPI_SETDESKWALLPAPER =;
    #endregion
    Form frm2;
    private void toolStripButton_Click(object sender, EventArgs e)
    {
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            listView.Items.Clear();
            string[] files = openFileDialog.FileNames;
            string[] fileinfo = new string[];
            for (int i =; i < files.Length; i++)
            {
                string path = files[i].ToString();
                string fileName = path.Substring(path.LastIndexOf("\\") +, path.Length - 1 - path.LastIndexOf("\\"));
                string filetype = fileName.Substring(fileName.LastIndexOf(".") +, fileName.Length - 1 - fileName.LastIndexOf("."));
                fileinfo[] = fileName;
                fileinfo[] = path;
                fileinfo[] = filetype;
                ListViewItem lvi = new ListViewItem(fileinfo);
                listView.Items.Add(lvi);
            }
        }
    }

    private void Form_Load(object sender, EventArgs e)
    {
        int x = this.Location.X;
        int y = this.Location.Y;
        frm = new Form2();
        frm.x = x + this.Width;
        frm.y = y;
        frm.Hide();
        openFileDialog.Filter = "支持的图片格式|*.jpeg;*.png;*.bmp;*.jpg";
    }

    private void toolStripButton_Click(object sender, EventArgs e)
    {
        if (listView.SelectedItems.Count > 0)
        {
            listView.Items.RemoveAt(listView1.SelectedItems[0].Index);
        }
    }

    private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        toolStripButton_Click(sender, e);
    }

    private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        toolStripButton_Click(sender, e);
    }

    private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        listView.Items.Clear();
    }

    private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void 设为桌面背景ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (listView.SelectedItems.Count > 0)
        {
            string FPath = listView.SelectedItems[0].SubItems[1].Text;
            //获取指定图片的扩展名
            string SFileType = FPath.Substring(FPath.LastIndexOf(".") +, (FPath.Length - FPath.LastIndexOf(".") - 1));
            //将扩展名转换成小写
            SFileType = SFileType.ToLower();
            //获取文件名
            string SFileName = FPath.Substring(FPath.LastIndexOf("\\") +, (FPath.LastIndexOf(".") - FPath.LastIndexOf("\\") - 1));
            //如果图片的类型是bmp则调用API中的方法将其设置为桌面背景
            if (SFileType == "bmp")
            {
                SystemParametersInfo(SPI_SETDESKWALLPAPER,, FPath, 1);
            }
            else
            {
                string SystemPath = Environment.SystemDirectory;//获取系统路径
                string path = SystemPath + "\\" + SFileName + ".bmp";
                FileInfo fi = new FileInfo(path);
                if (fi.Exists)
                {
                    fi.Delete();
                    PictureBox pb = new PictureBox();
                    pb.Image = Image.FromFile(FPath);
                    pb.Image.Save(SystemPath + "\\" + SFileName + ".bmp", ImageFormat.Bmp);
                }
                else
                {
                    PictureBox pb = new PictureBox();
                    pb.Image = Image.FromFile(FPath);
                    pb.Image.Save(SystemPath + "\\" + SFileName + ".bmp", ImageFormat.Bmp);
                }
                SystemParametersInfo(SPI_SETDESKWALLPAPER,, path, 1);
            }
        }
    }
    
    private void Form_LocationChanged(object sender, EventArgs e)
    {
        if (listView.SelectedItems.Count > 0)
        {
            if (frm != null)
            {
                frm.Close();
            }
            frm = new Form2();
            string path = listView.SelectedItems[0].SubItems[1].Text;
            int x = this.Location.X;
            int y = this.Location.Y;
            frm.x = x + this.Width;
            frm.y = y;
            frm.pictureBox1.Image = Image.FromFile(path);
            frm.Show();
            this.Focus();
        }
        else
        {
            if (frm != null)
            {
                frm.Close();
            }
            frm = new Form2();
            int x = this.Location.X;
            int y = this.Location.Y;
            frm.x = x + this.Width;
            frm.y = y;
            frm.Hide();
            this.Focus();
        }
    }
    private void listView_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listView.SelectedItems.Count > 0)
        {
            string path = listView.SelectedItems[0].SubItems[1].Text;
            frm.pictureBox1.Image = Image.FromFile(path);
            frm.Show();
            this.Focus();
        }
    }
}
partial class Form
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.toolStrip = new System.Windows.Forms.ToolStrip();
        this.toolStripButton = new System.Windows.Forms.ToolStripButton();
        this.toolStripButton = new System.Windows.Forms.ToolStripButton();
        this.listView = new System.Windows.Forms.ListView();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
        this.添加ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.删除ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.清空ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripSeparator = new System.Windows.Forms.ToolStripSeparator();
        this.设为桌面背景ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
        this.toolStrip.SuspendLayout();
        this.contextMenuStrip.SuspendLayout();
        this.SuspendLayout();
        // 
        // toolStrip
        // 
        this.toolStrip.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
        this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.toolStripButton,
        this.toolStripButton});
        this.toolStrip.Location = new System.Drawing.Point(0, 0);
        this.toolStrip.Name = "toolStrip1";
        this.toolStrip.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
        this.toolStrip.Size = new System.Drawing.Size(220, 25);
        this.toolStrip.TabIndex = 0;
        this.toolStrip.Text = "toolStrip1";
        // 
        // toolStripButton
        // 
        this.toolStripButton.Image = global::APIdesktop.Properties.Resources.图标__66_;
        this.toolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.toolStripButton.Name = "toolStripButton2";
        this.toolStripButton.Size = new System.Drawing.Size(49, 22);
        this.toolStripButton.Text = "增加";
        this.toolStripButton.Click += new System.EventHandler(this.toolStripButton2_Click);
        // 
        // toolStripButton
        // 
        this.toolStripButton.Image = global::APIdesktop.Properties.Resources.图标__59_;
        this.toolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.toolStripButton.Name = "toolStripButton1";
        this.toolStripButton.Size = new System.Drawing.Size(49, 22);
        this.toolStripButton.Text = "删除";
        this.toolStripButton.Click += new System.EventHandler(this.toolStripButton1_Click);
        // 
        // listView
        // 
        this.listView.BorderStyle = System.Windows.Forms.BorderStyle.None;
        this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnHeader,
        this.columnHeader,
        this.columnHeader});
        this.listView.ContextMenuStrip = this.contextMenuStrip1;
        this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
        this.listView.FullRowSelect = true;
        this.listView.Location = new System.Drawing.Point(0, 25);
        this.listView.MultiSelect = false;
        this.listView.Name = "listView1";
        this.listView.Size = new System.Drawing.Size(220, 338);
        this.listView.TabIndex = 1;
        this.listView.UseCompatibleStateImageBehavior = false;
        this.listView.View = System.Windows.Forms.View.Details;
        this.listView.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "名称";
        this.columnHeader.Width = 80;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "路径";
        this.columnHeader.Width = 80;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "类型";
        // 
        // contextMenuStrip
        // 
        this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.添加ToolStripMenuItem,
        this.删除ToolStripMenuItem,
        this.清空ToolStripMenuItem,
        this.toolStripSeparator,
        this.设为桌面背景ToolStripMenuItem,
        this.退出ToolStripMenuItem});
        this.contextMenuStrip.Name = "contextMenuStrip1";
        this.contextMenuStrip.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional;
        this.contextMenuStrip.Size = new System.Drawing.Size(143, 120);
        // 
        // 添加ToolStripMenuItem
        // 
        this.添加ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标___;
        this.添加ToolStripMenuItem.Name = "添加ToolStripMenuItem";
        this.添加ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.添加ToolStripMenuItem.Text = "添加";
        this.添加ToolStripMenuItem.Click += new System.EventHandler(this.添加ToolStripMenuItem_Click);
        // 
        // 删除ToolStripMenuItem
        // 
        this.删除ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标___;
        this.删除ToolStripMenuItem.Name = "删除ToolStripMenuItem";
        this.删除ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.删除ToolStripMenuItem.Text = "删除";
        this.删除ToolStripMenuItem.Click += new System.EventHandler(this.删除ToolStripMenuItem_Click);
        // 
        // 清空ToolStripMenuItem
        // 
        this.清空ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标___;
        this.清空ToolStripMenuItem.Name = "清空ToolStripMenuItem";
        this.清空ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.清空ToolStripMenuItem.Text = "清空";
        this.清空ToolStripMenuItem.Click += new System.EventHandler(this.清空ToolStripMenuItem_Click);
        // 
        // toolStripSeparator
        // 
        this.toolStripSeparator.Name = "toolStripSeparator1";
        this.toolStripSeparator.Size = new System.Drawing.Size(139, 6);
        // 
        // 设为桌面背景ToolStripMenuItem
        // 
        this.设为桌面背景ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标___;
        this.设为桌面背景ToolStripMenuItem.Name = "设为桌面背景ToolStripMenuItem";
        this.设为桌面背景ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.设为桌面背景ToolStripMenuItem.Text = "设为桌面背景";
        this.设为桌面背景ToolStripMenuItem.Click += new System.EventHandler(this.设为桌面背景ToolStripMenuItem_Click);
        // 
        // 退出ToolStripMenuItem
        // 
        this.退出ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标___;
        this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem";
        this.退出ToolStripMenuItem.Size = new System.Drawing.Size(, 22);
        this.退出ToolStripMenuItem.Text = "退出";
        this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click);
        // 
        // openFileDialog
        // 
        this.openFileDialog.Multiselect = true;
        // 
        // Form
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(, 363);
        this.Controls.Add(this.listView);
        this.Controls.Add(this.toolStrip);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "Form";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "设置桌面背景";
        this.Load += new System.EventHandler(this.Form_Load);
        this.LocationChanged += new System.EventHandler(this.Form_LocationChanged);
        this.toolStrip.ResumeLayout(false);
        this.toolStrip.PerformLayout();
        this.contextMenuStrip.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.ToolStrip toolStrip;
    private System.Windows.Forms.ListView listView;
    private System.Windows.Forms.ToolStripButton toolStripButton;
    private System.Windows.Forms.ToolStripButton toolStripButton;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.OpenFileDialog openFileDialog;
    private System.Windows.Forms.ContextMenuStrip contextMenuStrip;
    private System.Windows.Forms.ToolStripMenuItem 添加ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 删除ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 清空ToolStripMenuItem;
    private System.Windows.Forms.ToolStripSeparator toolStripSeparator;
    private System.Windows.Forms.ToolStripMenuItem 设为桌面背景ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;
}
public partial class Form : Form
{
    public Form()
    {
        InitializeComponent();
    }
    public int x;
    public int y;
    private void pictureBox_Click(object sender, EventArgs e)
    {

    }

    private void Form_Load(object sender, EventArgs e)
    {
        this.Location = new Point(x,y);
    }
}
partial class Form
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.pictureBox = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox
        // 
        this.pictureBox.BackColor = System.Drawing.Color.Black;
        this.pictureBox.Location = new System.Drawing.Point(23, 24);
        this.pictureBox.Name = "pictureBox1";
        this.pictureBox.Size = new System.Drawing.Size(349, 348);
        this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
        this.pictureBox.TabIndex = 0;
        this.pictureBox.TabStop = false;
        this.pictureBox.Click += new System.EventHandler(this.pictureBox1_Click);
        // 
        // Form
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.White;
        this.BackgroundImage = global::APIdesktop.Properties.Resources.bg;
        this.ClientSize = new System.Drawing.Size(, 395);
        this.Controls.Add(this.pictureBox);
        this.DoubleBuffered = true;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Form";
        this.ShowIcon = false;
        this.ShowInTaskbar = false;
        this.Text = "Form";
        this.TransparencyKey = System.Drawing.Color.DarkGray;
        this.Load += new System.EventHandler(this.Form_Load);
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    public System.Windows.Forms.PictureBox pictureBox;

}