美丽桌面墙纸自动换
介紹
本文介紹使用C#編寫如何在指定的時(shí)間內(nèi)自動(dòng)更換已經(jīng)指定的墻紙。運(yùn)行代碼示例需要.net 2.0 以上版本支持。運(yùn)行程序后將顯示在系統(tǒng)托盤內(nèi)。雙擊可打開配置主窗體。你可以添加或刪除圖片,還可以設(shè)置墻紙更換的時(shí)間。
示例
設(shè)置桌面圖片
設(shè)置桌面的圖片我們需要用到WINAPI SystemParametersInfo才可以完成:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(
??????????????????????? int uAction, int uParam,
??????????????????????? string lpvParam, int fuWinIni);
public const int SPI_SETDESKWALLPAPER = 20;
public const int SPIF_SENDCHANGE = 0x2;
...
int result = SystemParametersInfo(SPI_SETDESKWALLPAPER,
?????????????????? 1, tempImageFilePath, SPIF_SENDCHANGE);
要設(shè)置壁紙的比例風(fēng)格,還必須設(shè)置注冊(cè)表鍵TileWallpaper WallpaperStyle。它們?cè)谧?cè)表的位置是HKEY_CURRENT_USER\Control Panel\Desktop。
Center : TileWallpaper=0, WallpaperStyle=1
Stretch : TileWallpaper=0, WallpaperStyle=2
Tile: TileWallpaper=1, WallpaperStyle=0
使用下面的代碼修改注冊(cè)表:
using Microsoft.Win32;
...
private static void SetRegistryKeyForWallpaper(
????????????????????????? string keyName, string value)
{
?? RegistryKey deskTopKey =
?????? Registry.CurrentUser.OpenSubKey(
????????????? @"Control Panel\Desktop", true);
?? deskTopKey.SetValue(keyName, value);
}
添加BestFit樣式
我們添加了一種墻紙?zhí)畛涞娘L(fēng)格BestFit。這種風(fēng)格尺度比例的圖像,使圖像不歪斜。周圍的圖像的部分可能是空白,這個(gè)區(qū)域的顏色可以選擇。
private static void ConvertSourceFileToBmp(
????????? string sourceFilePath, string tempBmpFilePath,
????????? ScaleStyles scaleStyle, Color backColor)
{
??? Image sourceImg = Image.FromFile(sourceFilePath);
??? if (scaleStyle != ScaleStyles.BestFit)
??? {
??????? sourceImg.Save(tempBmpFilePath,
???????????? System.Drawing.Imaging.ImageFormat.Bmp);
??? }
??? else
??? {
??????? //get the dimensions of the screen
??????? float H =
????????? System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
??????? float W =
????????? System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
??????? //get the image dimensions
??????? float h = sourceImg.Height;
??????? float w = sourceImg.Width;
??????? //dimensions of target
??????? float targetHeight = -1;
??????? float targetWidth = -1;
??????? //find the appropriate height and width
??????? if (H / h >= W / w)
??????? {
??????????? targetWidth = w;
??????? }
??????? else
??????? {
??????????? targetHeight = h;
??????? }
??????? if (targetHeight == -1)
??????? {
??????????? targetHeight = (H / W) * targetWidth;
??????? }
??????? if (targetWidth == -1)
??????? {
??????????? targetWidth = (W / H) * targetHeight;
??????? }
??????? //create a new image with the default back color
??????? //with the scaled dimensions w.r.t. image and screen
??????? Bitmap bmpImage = new Bitmap((int)targetWidth,
???????????????????????????????????????? (int)targetHeight);
??????? Graphics g = Graphics.FromImage(bmpImage);
??????? SolidBrush backgroundColorBrush =
????????????????????????????????? new SolidBrush(backColor);
??????? g.FillRectangle(backgroundColorBrush, 0, 0,
?????????????????????????? bmpImage.Width, bmpImage.Height);
???????
??????? //layout this image in the center
??????? g.DrawImage(sourceImg,
?????????????? Math.Abs(targetWidth-sourceImg.Width)/2,
?????????????? Math.Abs(targetHeight - sourceImg.Height)/2,
?????????????? sourceImg.Width, sourceImg.Height);
??????? //save it as bmp
??????? bmpImage.Save(tempBmpFilePath,
?????????????????? System.Drawing.Imaging.ImageFormat.Bmp);
??????? //dispose stuff
??????? backgroundColorBrush.Dispose();
??????? g.Dispose();
??? }
}
載入擴(kuò)展資源
為我們的主程序添加icon圖標(biāo)。當(dāng)墻紙被改變后系統(tǒng)托盤圖標(biāo)也隨之改變。
ApplicationIcon = Icon.ExtractAssociatedIcon("App.ico");
如果你不想因?yàn)槌绦虮粡?fù)制使用的時(shí)候,未復(fù)制走ico圖標(biāo)圖片而使程序運(yùn)行失敗,可以將ico圖片打包到程序的資源中,這樣ico圖片將編譯在exe文件中。步驟如下:
- ?為項(xiàng)目添加資源文件
- ?重命名你想要的資源文件(例如:Resource1.resx)
- ?打開資源文件,添加icons。
- ?在代碼中使用嵌入的資源,使用ResourceManager類。
ResourceManager resourceManager =
??? new ResourceManager("WallpaperChanger.Resource1",
?????????????????????? Assembly.GetExecutingAssembly());
資源文件的名稱是由項(xiàng)目的默認(rèn)命名控件和資源文件名稱組合而成的??梢允褂妹Q獲取想要的資源:
ResourceManager resourceManager =
?? new ResourceManager("WallpaperChanger.Resource1",
??????????????????????? Assembly.GetExecutingAssembly());
ApplicationIcon = (Icon)resourceManager.GetObject("App");
WallpaperCurrentlyChangingIcon =
?????????? (Icon)resourceManager.GetObject("Changing");
添加托盤圖標(biāo)
?
this.notifyIcon = new NotifyIcon();
this.notifyIcon.Text = "Wallpaper Changer";
this.notifyIcon.Icon = NotifyIconManager.ApplicationIcon;
this.notifyIcon.Visible = true;
this.notifyIcon.ShowBalloonTip(1, "Started " +
?? NotifyIconManager.APP_TITLE, "You can double click on this icon to
?? configure settings and choose pictures.", ToolTipIcon.Info);
this.notifyIcon.DoubleClick +=
??????????????? new EventHandler(this.configureMenuItem_Click);
定期更換墻紙
使用定時(shí)器
//create timer and set time interval
this.periodicTimer = new Timer();
this.periodicTimer.Interval =
? Int32.Parse(this.optionsDictionary["TimeIntervalInSeconds"])*1000;
this.periodicTimer.Tick += new EventHandler(periodicTimer_Tick);
this.periodicTimer.Start();
隨機(jī)抽取文件設(shè)置設(shè)置墻紙
private void periodicTimer_Tick(object sender, EventArgs e)
{
??? this.periodicTimer.Stop();
??? try
??? {
??????? int failCount = 0;
???????
??????? while (failCount < 3)
??????? {
??????????? ImageInfo nextFileImageInfo =
???????????????????? this.GetNextRandomImageFileInfo();???
??????????? //if file is not present then try again
??????????? if (nextFileImageInfo == null ||
?????????????????? !File.Exists(nextFileImageInfo.FilePath))
??????????? {
??????????????? failCount++;
??????????????? continue;
??????????? }
??????????? else
??????????? {
??????????????? this.SetAsWallpaperNow(nextFileImageInfo);
??????????????? break;
??????????? }
??????? }
??? }
??? catch (Exception ex)
??? {
??????? System.Windows.Forms.MessageBox.Show("Error! " + ex.Message +
???????????? "\r\n" + ex.StackTrace);
??? }
??? this.periodicTimer.Start();
}
代碼下載
WallpaperChanger_src.zip
參考資料
翻譯參考 http://www.codeproject.com/KB/cs/wallpaperchanger.aspx
關(guān)于托盤圖標(biāo)NotifyIcon知識(shí)? http://dev.mjxy.cn/a-Tray-icon-NotifyIcon.aspx
轉(zhuǎn)載于:https://www.cnblogs.com/xingquan/archive/2011/08/15/2139110.html
總結(jié)
- 上一篇: UE4 ACharacter部分方法介绍
- 下一篇: Mini MP3 Player播放器简介