基于C#实现设置桌面背景功能
作者:芝麻粒儿
这篇文章主要为大家详细介绍了如何利用C#实现设置桌面背景功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
实践过程
效果
代码
public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 调用API [DllImport("user32.dll", EntryPoint = "SystemParametersInfoA")] static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni); private const int SPI_SETDESKWALLPAPER = 20; #endregion Form2 frm2; private void toolStripButton2_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { listView1.Items.Clear(); string[] files = openFileDialog1.FileNames; string[] fileinfo = new string[3]; for (int i = 0; i < files.Length; i++) { string path = files[i].ToString(); string fileName = path.Substring(path.LastIndexOf("\\") + 1, path.Length - 1 - path.LastIndexOf("\\")); string filetype = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - 1 - fileName.LastIndexOf(".")); fileinfo[0] = fileName; fileinfo[1] = path; fileinfo[2] = filetype; ListViewItem lvi = new ListViewItem(fileinfo); listView1.Items.Add(lvi); } } } private void Form1_Load(object sender, EventArgs e) { int x = this.Location.X; int y = this.Location.Y; frm2 = new Form2(); frm2.x = x + this.Width; frm2.y = y; frm2.Hide(); openFileDialog1.Filter = "支持的图片格式|*.jpeg;*.png;*.bmp;*.jpg"; } private void toolStripButton1_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { listView1.Items.RemoveAt(listView1.SelectedItems[0].Index); } } private void 添加ToolStripMenuItem_Click(object sender, EventArgs e) { toolStripButton2_Click(sender, e); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { toolStripButton1_Click(sender, e); } private void 清空ToolStripMenuItem_Click(object sender, EventArgs e) { listView1.Items.Clear(); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void 设为桌面背景ToolStripMenuItem_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { string FPath = listView1.SelectedItems[0].SubItems[1].Text; //获取指定图片的扩展名 string SFileType = FPath.Substring(FPath.LastIndexOf(".") + 1, (FPath.Length - FPath.LastIndexOf(".") - 1)); //将扩展名转换成小写 SFileType = SFileType.ToLower(); //获取文件名 string SFileName = FPath.Substring(FPath.LastIndexOf("\\") + 1, (FPath.LastIndexOf(".") - FPath.LastIndexOf("\\") - 1)); //如果图片的类型是bmp则调用API中的方法将其设置为桌面背景 if (SFileType == "bmp") { SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, 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, 0, path, 1); } } } private void Form1_LocationChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { if (frm2 != null) { frm2.Close(); } frm2 = new Form2(); string path = listView1.SelectedItems[0].SubItems[1].Text; int x = this.Location.X; int y = this.Location.Y; frm2.x = x + this.Width; frm2.y = y; frm2.pictureBox1.Image = Image.FromFile(path); frm2.Show(); this.Focus(); } else { if (frm2 != null) { frm2.Close(); } frm2 = new Form2(); int x = this.Location.X; int y = this.Location.Y; frm2.x = x + this.Width; frm2.y = y; frm2.Hide(); this.Focus(); } } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { string path = listView1.SelectedItems[0].SubItems[1].Text; frm2.pictureBox1.Image = Image.FromFile(path); frm2.Show(); this.Focus(); } } }
partial class Form1 { /// <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.toolStrip1 = new System.Windows.Forms.ToolStrip(); this.toolStripButton2 = new System.Windows.Forms.ToolStripButton(); this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); this.listView1 = new System.Windows.Forms.ListView(); this.columnHeader1 = new System.Windows.Forms.ColumnHeader(); this.columnHeader2 = new System.Windows.Forms.ColumnHeader(); this.columnHeader3 = new System.Windows.Forms.ColumnHeader(); this.contextMenuStrip1 = 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.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.设为桌面背景ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.toolStrip1.SuspendLayout(); this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); // // toolStrip1 // this.toolStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden; this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripButton2, this.toolStripButton1}); this.toolStrip1.Location = new System.Drawing.Point(0, 0); this.toolStrip1.Name = "toolStrip1"; this.toolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System; this.toolStrip1.Size = new System.Drawing.Size(220, 25); this.toolStrip1.TabIndex = 0; this.toolStrip1.Text = "toolStrip1"; // // toolStripButton2 // this.toolStripButton2.Image = global::APIdesktop.Properties.Resources.图标__66_; this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta; this.toolStripButton2.Name = "toolStripButton2"; this.toolStripButton2.Size = new System.Drawing.Size(49, 22); this.toolStripButton2.Text = "增加"; this.toolStripButton2.Click += new System.EventHandler(this.toolStripButton2_Click); // // toolStripButton1 // this.toolStripButton1.Image = global::APIdesktop.Properties.Resources.图标__59_; this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta; this.toolStripButton1.Name = "toolStripButton1"; this.toolStripButton1.Size = new System.Drawing.Size(49, 22); this.toolStripButton1.Text = "删除"; this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click); // // listView1 // this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.None; this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.columnHeader1, this.columnHeader2, this.columnHeader3}); this.listView1.ContextMenuStrip = this.contextMenuStrip1; this.listView1.Dock = System.Windows.Forms.DockStyle.Fill; this.listView1.FullRowSelect = true; this.listView1.Location = new System.Drawing.Point(0, 25); this.listView1.MultiSelect = false; this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(220, 338); this.listView1.TabIndex = 1; this.listView1.UseCompatibleStateImageBehavior = false; this.listView1.View = System.Windows.Forms.View.Details; this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged); // // columnHeader1 // this.columnHeader1.Text = "名称"; this.columnHeader1.Width = 80; // // columnHeader2 // this.columnHeader2.Text = "路径"; this.columnHeader2.Width = 80; // // columnHeader3 // this.columnHeader3.Text = "类型"; // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.添加ToolStripMenuItem, this.删除ToolStripMenuItem, this.清空ToolStripMenuItem, this.toolStripSeparator1, this.设为桌面背景ToolStripMenuItem, this.退出ToolStripMenuItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional; this.contextMenuStrip1.Size = new System.Drawing.Size(143, 120); // // 添加ToolStripMenuItem // this.添加ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标__199_; this.添加ToolStripMenuItem.Name = "添加ToolStripMenuItem"; this.添加ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.添加ToolStripMenuItem.Text = "添加"; this.添加ToolStripMenuItem.Click += new System.EventHandler(this.添加ToolStripMenuItem_Click); // // 删除ToolStripMenuItem // this.删除ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标__96_; this.删除ToolStripMenuItem.Name = "删除ToolStripMenuItem"; this.删除ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.删除ToolStripMenuItem.Text = "删除"; this.删除ToolStripMenuItem.Click += new System.EventHandler(this.删除ToolStripMenuItem_Click); // // 清空ToolStripMenuItem // this.清空ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标__190_; this.清空ToolStripMenuItem.Name = "清空ToolStripMenuItem"; this.清空ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.清空ToolStripMenuItem.Text = "清空"; this.清空ToolStripMenuItem.Click += new System.EventHandler(this.清空ToolStripMenuItem_Click); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; this.toolStripSeparator1.Size = new System.Drawing.Size(139, 6); // // 设为桌面背景ToolStripMenuItem // this.设为桌面背景ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标__22_; this.设为桌面背景ToolStripMenuItem.Name = "设为桌面背景ToolStripMenuItem"; this.设为桌面背景ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.设为桌面背景ToolStripMenuItem.Text = "设为桌面背景"; this.设为桌面背景ToolStripMenuItem.Click += new System.EventHandler(this.设为桌面背景ToolStripMenuItem_Click); // // 退出ToolStripMenuItem // this.退出ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.图标__8_; this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem"; this.退出ToolStripMenuItem.Size = new System.Drawing.Size(142, 22); this.退出ToolStripMenuItem.Text = "退出"; this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click); // // openFileDialog1 // this.openFileDialog1.Multiselect = true; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(220, 363); this.Controls.Add(this.listView1); this.Controls.Add(this.toolStrip1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "设置桌面背景"; this.Load += new System.EventHandler(this.Form1_Load); this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged); this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); this.contextMenuStrip1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.ToolStrip toolStrip1; private System.Windows.Forms.ListView listView1; private System.Windows.Forms.ToolStripButton toolStripButton2; private System.Windows.Forms.ToolStripButton toolStripButton1; private System.Windows.Forms.ColumnHeader columnHeader1; private System.Windows.Forms.ColumnHeader columnHeader2; private System.Windows.Forms.ColumnHeader columnHeader3; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem 添加ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 删除ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 清空ToolStripMenuItem; private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; private System.Windows.Forms.ToolStripMenuItem 设为桌面背景ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem; }
public partial class Form2 : Form { public Form2() { InitializeComponent(); } public int x; public int y; private void pictureBox1_Click(object sender, EventArgs e) { } private void Form2_Load(object sender, EventArgs e) { this.Location = new Point(x,y); } }
partial class Form2 { /// <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.pictureBox1 = new System.Windows.Forms.PictureBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.BackColor = System.Drawing.Color.Black; this.pictureBox1.Location = new System.Drawing.Point(23, 24); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(349, 348); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click); // // Form2 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.White; this.BackgroundImage = global::APIdesktop.Properties.Resources.bg2; this.ClientSize = new System.Drawing.Size(395, 395); this.Controls.Add(this.pictureBox1); this.DoubleBuffered = true; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "Form2"; this.ShowIcon = false; this.ShowInTaskbar = false; this.Text = "Form2"; this.TransparencyKey = System.Drawing.Color.DarkGray; this.Load += new System.EventHandler(this.Form2_Load); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); } #endregion public System.Windows.Forms.PictureBox pictureBox1; }
以上就是基于C#实现设置桌面背景功能的详细内容,更多关于C#设置桌面背景的资料请关注脚本之家其它相关文章!