您的位置:首页 > 大数据 > 人工智能

使用c#在vs2005下写的换桌面图片的小程序(from: http://www.wangchao.net.cn/bbsdetail_26766.html)

2008-07-09 15:30 711 查看
首先,建一个windows应用程序,所需控件有pictureBox, OpenFileDialog, textBox, 2个Button

其次,构造函数前添加

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

private string filePath;

添加两个Button的click事件//桌面只能显示bmp图片

private void button1_Click(object sender, EventArgs e)

{

openFileDialog1.InitialDirectory = @"C:/";  //打开的初始位置

pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //设置图片显示的大小

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

this.textBox1.Text = openFileDialog1.FileName;

string[] strA = this.textBox1.Text.Split('.');

Bitmap bm = new Bitmap(this.textBox1.Text);

//只选择bmp格式的图片

if (strA[1] != "bmp")

{

//filePath = strA[0] + ".bmp";

//bm.Save(filePath);

MessageBox.show("not bmp");

this.button2.enabled = false;

}

else

{

filePath = this.textBox1.Text;

this.pictureBox1.Image = bm;

}

}

}

//设置桌面,其实只用一句话

private void button2_Click(object sender, EventArgs e)

{

int nResult;

if (File.Exists(filePath))

{

nResult = SystemParametersInfo(20, 1, filePath, 0x1 | 0x2);

if (nResult == 0)

{

MessageBox.Show("picture is not changed");

}

else

{

MessageBox.Show("picture has been changed");

}

}

else

{

MessageBox.Show("pic is not existed");

}

}

个人觉得挺不错,所以拿来试试,结果还不错
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐