当前位置:编程学习 > C#/ASP.NET >>

C#更换桌面

求用C#写的最简单的更换桌面小程序(如我点击按钮就自动换成指定的图片) --------------------编程问答-------------------- using System.Runtime.InteropServices;

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

        private void button1_Click(object sender, EventArgs e)
        {
            int nResult;
            if(File.Exists(""))
            {
                nResult = SystemParametersInfo(20, 1, "", 0x1 | 0x2);
                if(nResult==0)
                    MessageBox.Show("error");
                else
                    MessageBox.Show("ok");    
            }
        }
--------------------编程问答-------------------- 更改注册表里的值就可以了
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;

namespace RockLib
{
    public class Utils
    {
        /**//// <summary>
        /// Change user's desktop wallpaper
        /// </summary>
        /// <param name="picturePath"></param>
        /// <param name="style"></param>
        /// <returns></returns>
        public static bool ChangeWallPaper(string picturePath, WallPaperStyle style)
        {
            RegistryKey myRegKey = Registry.CurrentUser.OpenSubKey("Control Panel\\desktop", true);
            switch (style)
            {
                case WallPaperStyle.Center:
                    myRegKey.SetValue("TileWallpaper", "0");
                    myRegKey.SetValue("WallpaperStyle", "0");
                    break;
                case WallPaperStyle.Tile:
                    myRegKey.SetValue("TileWallpaper", "1");
                    myRegKey.SetValue("WallpaperStyle", "0");
                    break;
                case WallPaperStyle.Stretch:
                    myRegKey.SetValue("TileWallpaper", "0");
                    myRegKey.SetValue("WallpaperStyle", "2");
                    break;
                default:
                    throw new NotSupportedException("style");                    
            }

          
            myRegKey.Close();

            int intResult;
            
            if (!File.Exists(picturePath))
            {
                throw new ArgumentException(string.Format("Picture {0} cannot be found.", picturePath));
            }

            intResult = SystemParametersInfo(20, 3, picturePath, 0x1 | 0x2);

            return intResult > 0;

        }

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

    public enum WallPaperStyle
    {
        Center,
        Tile,
        Stretch
    }
}

--------------------编程问答-------------------- 对代码加点备注吧,有些不理解呢,谢谢了 --------------------编程问答-------------------- 学习了。。 --------------------编程问答-------------------- 收藏 --------------------编程问答-------------------- 学习了。。。 --------------------编程问答--------------------

using System.Runtime.InteropServices; 

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

        private void button1_Click(object sender, EventArgs e) 
        { 
            int nResult; 
            if(File.Exists("")) 
            { 
                nResult = SystemParametersInfo(20, 1, "", 0x1 | 0x2); 
                if(nResult==0) 
                    MessageBox.Show("error"); 
                else 
                    MessageBox.Show("ok");    
            } 
        } 


这个简洁些!把提示修改一下就可以用了。 --------------------编程问答-------------------- 学习吖 --------------------编程问答-------------------- 更换不了,图片不能成为背景,改是可以改了
 SystemParametersInfo(20, 1, @"..\\image\2.jpg", 0x1 | 0x2);不对
SystemParametersInfo(20, 1, @"C:\\Documents and Settings\Administrator\桌面\burst\burst\image\2.jpg", 0x1 | 0x2);也不对,
路径出问题了? --------------------编程问答-------------------- jf --------------------编程问答-------------------- 学习了。。 --------------------编程问答-------------------- 2楼的代码存在着这样的一个问题,
用注册表写入,这样固然可以,
但是,当用户点击按钮,只是改变了注册表数据,
而没有改变界面,也就是说,界面不会因为点击按钮就会改变界面UI,
而是还得需要更新一下界面才会有显示

我想了解一下如何更新界面?

--------------------编程问答-------------------- UP --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 你换个bmp格式的图片试试!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,