C#.Net 修改桌面背景图(一)

这里介绍的是利用API函数修改窗口的背景图。
程序代码 程序代码

[DllImport("user32.dll",   EntryPoint="SystemParametersInfo")]   
  public   static   extern   int   SystemParametersInfo   (   
  int   uAction,   
  int   uParam,   
  string   lpvParam,   
  int   fuWinIni   
  );   

  SystemParametersInfo(20,   0,   @"C:/temp.bmp",   0x2);  

在这里需要注意的是SystemParametersInfo只对bmp格式图片有效,如果直接吧jpg后缀改成bmp是无效的,所以需要把图片转换bmp。
程序代码 程序代码

    Bitmap bm=new Bitmap("temp.jpg");
    bm.Save("temp.bmp",System.Drawing.Imaging.ImageFormat.Bmp);



上传源码:
程序代码 程序代码

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 Desktop
{
    /// <summary>
    /// DeskT 的摘要说明。
    /// </summary>
    public class DeskT : 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 DeskT()
        {
            //
            // 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 DeskT());
//        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            int nResult ;
            if (File.Exists(filepath))
            {
                nResult = SystemParametersInfo(20, 0, 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,System.Drawing.Imaging.ImageFormat.Bmp);
                }
                else
                    filepath=textBox1.Text;
                this.pictureBox1.Image=bm;
            }

        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值