基于C#實(shí)現(xiàn)設(shè)置桌面背景功能
更新時(shí)間:2022年12月20日 08:46:24 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)設(shè)置桌面背景功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過程
效果

代碼
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 調(diào)用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 設(shè)為桌面背景ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
string FPath = listView1.SelectedItems[0].SubItems[1].Text;
//獲取指定圖片的擴(kuò)展名
string SFileType = FPath.Substring(FPath.LastIndexOf(".") + 1, (FPath.Length - FPath.LastIndexOf(".") - 1));
//將擴(kuò)展名轉(zhuǎn)換成小寫
SFileType = SFileType.ToLower();
//獲取文件名
string SFileName = FPath.Substring(FPath.LastIndexOf("\\") + 1, (FPath.LastIndexOf(".") - FPath.LastIndexOf("\\") - 1));
//如果圖片的類型是bmp則調(diào)用API中的方法將其設(shè)置為桌面背景
if (SFileType == "bmp")
{
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, FPath, 1);
}
else
{
string SystemPath = Environment.SystemDirectory;//獲取系統(tǒng)路徑
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>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </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.設(shè)為桌面背景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.圖標(biāo)__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.圖標(biāo)__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.設(shè)為桌面背景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.圖標(biāo)__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.圖標(biāo)__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.圖標(biāo)__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);
//
// 設(shè)為桌面背景ToolStripMenuItem
//
this.設(shè)為桌面背景ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.圖標(biāo)__22_;
this.設(shè)為桌面背景ToolStripMenuItem.Name = "設(shè)為桌面背景ToolStripMenuItem";
this.設(shè)為桌面背景ToolStripMenuItem.Size = new System.Drawing.Size(142, 22);
this.設(shè)為桌面背景ToolStripMenuItem.Text = "設(shè)為桌面背景";
this.設(shè)為桌面背景ToolStripMenuItem.Click += new System.EventHandler(this.設(shè)為桌面背景ToolStripMenuItem_Click);
//
// 退出ToolStripMenuItem
//
this.退出ToolStripMenuItem.Image = global::APIdesktop.Properties.Resources.圖標(biāo)__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 = "設(shè)置桌面背景";
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 設(shè)為桌面背景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#實(shí)現(xiàn)設(shè)置桌面背景功能的詳細(xì)內(nèi)容,更多關(guān)于C#設(shè)置桌面背景的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#操作數(shù)據(jù)庫總結(jié)(vs2005+sql2005)
C#操作數(shù)據(jù)庫總結(jié),每次做項(xiàng)目都會(huì)用到數(shù)據(jù)庫,對(duì)數(shù)據(jù)庫的操作都是糊里糊涂從書里找代碼用。通過昨天晚上與今天早上的努力,把數(shù)據(jù)庫的操作整理了一下,下面把整理結(jié)果做個(gè)小結(jié)2012-09-09
C#實(shí)現(xiàn)IDbConnection/IDbCommand等相關(guān)通用數(shù)據(jù)接口
ADO.NET?中的數(shù)據(jù)提供者對(duì)象提供了IDbConnection、IDbCommand、IDbDataParameter等通用數(shù)據(jù)接口,本文將利用這些對(duì)象實(shí)現(xiàn)一個(gè)通用方法以訪問和操作數(shù)據(jù)庫內(nèi)容,需要的朋友可以參考下2024-04-04
c#使用Dataset讀取XML文件動(dòng)態(tài)生成菜單的方法
這篇文章主要介紹了c#使用Dataset讀取XML文件動(dòng)態(tài)生成菜單的方法,涉及C#使用Dataset操作XML文件的相關(guān)技巧,需要的朋友可以參考下2015-05-05
C#項(xiàng)目中跨文件調(diào)用公共類的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于C#項(xiàng)目中如何跨文件調(diào)用公共類的知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-08-08

