最大的網(wǎng)站源碼資源下載站,
使用c#制作的更換桌面背景程序
今天是周末,可是沒什么地方去,所以有上網(wǎng)來了,突然發(fā)現(xiàn)了一篇用
vb調用api來更換桌面的程序,我想既然vb可以c#也一定能行,所以就
試著做了一下,好吧,來看看我的代碼吧.一步一步來,你也能行.
那還是先讓我們來了解一個api吧,systemparametersinfo,這個api的功能
很簡單就是通過一些參數(shù)的設置來完成對系統(tǒng)的一些外觀設置.
函數(shù)原型如下:
bool systemparametersinfo(
uint uiaction,
uint uiparam,
pvoid pvparam,
uint fwinini
);
該函數(shù)返回一個bool值.非0成功,否則當然是失敗了,那樣的話根據(jù)msdn的說法
還將會設置getlasterror(關于這一點可以參考msdn)
這里還必須提到的一點是,關于uaction常數(shù)表,這張表里面包括了很多關于這些參數(shù)
的設置工作.因為它將影響到.前面兩個參數(shù).第三個參數(shù)在我們這里的用法是得到
圖片的路徑.第四個參數(shù)看名字也猜的到.隨同這個函數(shù)設置的用戶配置參數(shù)保存在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("文件不存在!");
}
這個實現(xiàn)起來在簡單不過了,僅僅是調用剛才上面講到的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 調用后添加任何構造函數(shù)代碼
//
}
/// <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;
}
}
}
}
新聞熱點
疑難解答