您的位置:首页 > 编程语言 > C#

c#调用API更改桌面壁纸,同时修改注册表以避免电脑重启后壁纸还原

2009-06-10 13:56 1296 查看
针对上一篇文章的问题,我自己回去冲写了一下。解决了题目所述的问题。一下是我的源代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Drawing;
using System.Drawing.Imaging;

namespace test
{
class DeskWallpaper
{
#region 导入API
[DllImport("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest,   //目标设备的句柄
int nXDest,   //   目标对象的左上角的X坐标
int nYDest,   //   目标对象的左上角的X坐标
int nWidth,   //   目标对象的矩形的宽度
int nHeight,   //   目标对象的矩形的长度
IntPtr hdcSrc,   //   源设备的句柄
int nXSrc,   //   源对象的左上角的X坐标
int nYSrc,   //   源对象的左上角的X坐标
System.Int32 dwRop   //   光栅的操作值
);

[DllImport("gdi32.dll")]
private static extern IntPtr CreateDC(
string DriverName,
string Device,
string lpszOutPut,
IntPtr lpInitdata
);
[DllImport("user32.dll")]
private static extern int SystemParametersInfo(
int uAction,
int uParam,
string lpvParam,
int fuWinIni);
[DllImport("user32.dll")]
private static extern int SystemParametersInfo(
int uAction,
int uParam,
StringBuilder lpvParam,
int fuWinIni);

#endregion

/// <summary>
///
/// </summary>
/// <param name="namePic"></param>
private static void CenterShowPicture(string namePic)
{
RegistryKey myRegKey = Registry.CurrentUser.OpenSubKey("Control Panel//Desktop", true);
myRegKey.SetValue("TileWallpaper","0");
myRegKey.SetValue("WallpaperStyle", "0");
namePic = AppDomain.CurrentDomain.BaseDirectory + namePic;
myRegKey.SetValue("Wallpaper", namePic);
myRegKey.Close();

SystemParametersInfo(20, 1, namePic, 1);
}
/// <summary>
///
/// </summary>
/// <param name="namePic"></param>
private static void TileShowPicture(string namePic)
{
RegistryKey myRegKey = Registry.CurrentUser.OpenSubKey("Control Panel//Desktop", true);
myRegKey.SetValue("TileWallpaper", "1");
myRegKey.SetValue("WallpaperStyle", "0");
namePic = AppDomain.CurrentDomain.BaseDirectory + namePic;
myRegKey.SetValue("Wallpaper", namePic);
myRegKey.Close();

SystemParametersInfo(20, 1, namePic, 1);
}
/// <summary>
///
/// </summary>
/// <param name="namePic"></param>
private static void StrechShowPicture(string namePic)
{
RegistryKey myRegKey = Registry.CurrentUser.OpenSubKey("Control Panel//Desktop", true);
myRegKey.SetValue("TileWallpaper", "0");
myRegKey.SetValue("WallpaperStyle", "2");
namePic = AppDomain.CurrentDomain.BaseDirectory + namePic;
myRegKey.SetValue("Wallpaper", namePic);
myRegKey.Close();

int i= SystemParametersInfo(20, 1, namePic, 0x2|0x1);
}
/// <summary>
/// set the deskPicture
/// </summary>
/// <param name="namePic">the name of  the picture</param>
/// <param name="ShowKind">the flag of  show the Picture ,0-center,1-Tile,2-strech</param>
public static void SetDeskPicture(string namePic, int ShowKind)
{
switch (ShowKind)
{
case 0:
CenterShowPicture(namePic);
break;
case 1:
TileShowPicture(namePic);
break;
case 2:
StrechShowPicture(namePic);
break;
default:
break;
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  api c# string user class
相关文章推荐