最大的网站源码资源下载站,
使用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;
}
}
}
}
新闻热点
疑难解答