日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

美丽桌面墙纸自动换

發布時間:2023/12/16 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 美丽桌面墙纸自动换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載請注明:敏捷學院-技術資源庫原文鏈接:http://dev.mjxy.cn/a-767.aspx

介紹
本文介紹使用C#編寫如何在指定的時間內自動更換已經指定的墻紙。運行代碼示例需要.net 2.0 以上版本支持。運行程序后將顯示在系統托盤內。雙擊可打開配置主窗體。你可以添加或刪除圖片,還可以設置墻紙更換的時間。




示例
設置桌面圖片

設置桌面的圖片我們需要用到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);

要設置壁紙的比例風格,還必須設置注冊表鍵TileWallpaper WallpaperStyle。它們在注冊表的位置是HKEY_CURRENT_USER\Control Panel\Desktop。

Center : TileWallpaper=0, WallpaperStyle=1
Stretch : TileWallpaper=0, WallpaperStyle=2
Tile: TileWallpaper=1, WallpaperStyle=0

使用下面的代碼修改注冊表:

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樣式
我們添加了一種墻紙填充的風格BestFit。這種風格尺度比例的圖像,使圖像不歪斜。周圍的圖像的部分可能是空白,這個區域的顏色可以選擇。

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();
??? }
}

載入擴展資源
為我們的主程序添加icon圖標。當墻紙被改變后系統托盤圖標也隨之改變。

ApplicationIcon = Icon.ExtractAssociatedIcon("App.ico");

如果你不想因為程序被復制使用的時候,未復制走ico圖標圖片而使程序運行失敗,可以將ico圖片打包到程序的資源中,這樣ico圖片將編譯在exe文件中。步驟如下:

  • ?為項目添加資源文件
  • ?重命名你想要的資源文件(例如:Resource1.resx)
  • ?打開資源文件,添加icons。
  • ?在代碼中使用嵌入的資源,使用ResourceManager類。

ResourceManager resourceManager =
??? new ResourceManager("WallpaperChanger.Resource1",
?????????????????????? Assembly.GetExecutingAssembly());

資源文件的名稱是由項目的默認命名控件和資源文件名稱組合而成的??梢允褂妹Q獲取想要的資源:

ResourceManager resourceManager =
?? new ResourceManager("WallpaperChanger.Resource1",
??????????????????????? Assembly.GetExecutingAssembly());
ApplicationIcon = (Icon)resourceManager.GetObject("App");
WallpaperCurrentlyChangingIcon =
?????????? (Icon)resourceManager.GetObject("Changing");


添加托盤圖標
?

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);

定期更換墻紙

使用定時器

//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();

隨機抽取文件設置設置墻紙

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
關于托盤圖標NotifyIcon知識? http://dev.mjxy.cn/a-Tray-icon-NotifyIcon.aspx

轉載于:https://www.cnblogs.com/xingquan/archive/2011/08/15/2139110.html

總結

以上是生活随笔為你收集整理的美丽桌面墙纸自动换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。