使用C#制作的更換桌面背景程序
今天是周末,可是沒什麼地方去,所以有上網來了,突然發現了一篇用
VB調用API來更換桌面的程序,我想既然VB可以C#也一定能行,所以就
試著做了一下,好吧,來看看我的代碼吧.一步一步來,你也能行.
那還是先讓我們來了解一個API吧,SystemParametersInfo,這個API的功能
很簡單就是通過一些參數的設置來完成對系統的一些外觀設置.
函數原型如下:
BOOL SystemParametersInfo(
UINT uiAction,
UINT uiParam,
PVOID pvParam,
UINT fWinIni
);
該函數返回一個Bool值.非0成功,否則當然是失敗了,那樣的話根據MSDN的說法
還將會設置GetLastError(關於這一點可以參考MSDN)
這裡還必須提到的一點是,關於uAction常數表,這張表裡面包括了很多關於這些參數
的設置工作.因為它將影響到.前面兩個參數.第三個參數在我們這裡的用法是得到
圖片的路徑.第四個參數看名字也猜的到.隨同這個函數設置的用戶配置參數保存在win.ini
或注冊表裡,或同時保存在這兩個地方.一般是0X1或者0X2就可以了.
下面我在給出有關該API變成C#的代碼如下:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ;
//圖片
看見上面的圖了嗎?我來主要說說那個兩個button,
首先叫到的是選擇按鈕代碼如下:
private void button1_Click(object sender, System.EventArgs e)
{
openFileDialog1.InitialDirectory = @"C:\";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
string[] strA=textBox1.Text.Split('.');
Bitmap bm=new Bitmap(textBox1.Text);
if(strA[1]!="bmp")
{
filepath=strA[0]+".bmp";
bm.Save(filepath);
}
else
filepath=textBox1.Text;
this.pictureBox1.Image=bm;
}
正如你看到的,那樣,由於只能將BMP圖象設置成桌面所以必須要轉化一下,上面是我的方法
也許你還有更好的,那就說說吧.
然後是更換按鈕,代碼如下:
private void button2_Click(object sender, System.EventArgs e)
{
int nResult ;
if (File.Exists(filepath))
{
nResult = SystemParametersInfo(20, 1, filepath, 0x1 | 0x2 );
if(nResult==0)
MessageBox.Show("沒有更新成功!");
else
MessageBox.Show("正在更換背景圖片...");
}
else
MessageBox.Show("文件不存在!");
}
這個實現起來在簡單不過了,僅僅是調用剛才上面講到的API就可以了.
好了,我把全部代碼都給你,很簡單,如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;
namespace desktopWalk
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private string filepath;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ;
public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label2 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(312, 62);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "選擇背景";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(312, 120);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "更換背景";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 64);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(272, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label2,
this.pictureBox1,
this.label1,
this.groupBox2,
this.button1,
this.button2,
this.textBox1});
this.groupBox1.Location = new System.Drawing.Point(16, 16);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(392, 240);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "更換背景圖片";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 128);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 23);
this.label2.TabIndex = 6;
this.label2.Text = "預覽圖片:";
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(104, 120);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(184, 104);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 5;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 4;
this.label1.Text = "背景圖片:";
//
// groupBox2
//
this.groupBox2.Location = new System.Drawing.Point(8, 104);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(376, 8);
this.groupBox2.TabIndex = 3;
this.groupBox2.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClIEntSize = new System.Drawing.Size(432, 269);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1});
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "設置背景";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button2_Click(object sender, System.EventArgs e)
{
int nResult ;
if (File.Exists(filepath))
{
nResult = SystemParametersInfo(20, 1, filepath, 0x1 | 0x2 );
if(nResult==0)
MessageBox.Show("沒有更新成功!");
else
MessageBox.Show("正在更換背景圖片...");
}
else
MessageBox.Show("文件不存在!");
}
private void button1_Click(object sender, System.EventArgs e)
{
openFileDialog1.InitialDirectory = @"C:\";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
string[] strA=textBox1.Text.Split('.');
Bitmap bm=new Bitmap(textBox1.Text);
if(strA[1]!="bmp")
{
filepath=strA[0]+".bmp";
bm.Save(filepath);
}
else
filepath=textBox1.Text;
this.pictureBox1.Image=bm;
}
}
}
}