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

本文介绍了一个简单的Windows应用程序,该程序允许用户从本地文件中选择一张.bmp格式的图片并将其设置为桌面背景。通过使用pictureBox和OpenFileDialog等控件,用户可以预览所选图片,并通过点击按钮完成设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,建一个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");

            }

        }
个人觉得挺不错,所以拿来试试,结果还不错
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值