直接設定桌面桌布, 定時更新可以做動態桌布,基本實現類如下
public class DesktopDraw
{
static string imgFile = @"D:\";
static string imgName = "d_out.jpg";
[DllImport("User32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
static extern void ReleaseDC(IntPtr dc);
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
/// <summary>
/// 直接在螢幕上畫一個區域性圖片
/// </summary>
public static void DrawStart()
{
IntPtr desktop = GetDC(IntPtr.Zero);
Font font = new Font("Arial", 24);
using (Graphics g = Graphics.FromHdc(desktop))
{
g.FillRectangle(Brushes.Black, 0, 0, 430, 150);
g.DrawString(string.Format("RAM:{0} CPU:{1}", DeviceMonitor.getAvailableRAM(), DeviceMonitor.getCurrentCpuUsage()), font, Brushes.White, 5, 5);
g.DrawString(string.Format("網路:{0}", DeviceMonitor.GetInternetAvilable() ? "已連線" : "未連線"), font, Brushes.White, 5, 36);
TimeSpan ts = DeviceMonitor.GetSystemUpTime();
g.DrawString(string.Format("執行:{0}:{1}:{2}", ts.Hours, ts.Minutes, ts.Seconds), font, Brushes.White, 5, 67);
g.DrawString(string.Format("時間:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")), font, Brushes.White, 5, 98);
}
ReleaseDC(desktop);
}
static int sW = 1024, sH = 768,cW = 20,cH = 20;
/// <summary>
/// 畫一個圖片,返回圖片儲存的路徑
/// </summary>
/// <returns></returns>
public static string GetPathDrawImage()
{
Bitmap image = new Bitmap(sW, sH);
Font font = new Font("微軟雅黑", 16);
Pen penC = new Pen(Brushes.LightYellow);
Graphics g = null;
try
{
g = Graphics.FromImage(image);
}
catch (Exception e)
{
throw new Exception($"image create exception {e.Message}");
}
int i = 0, num = 0;
g.FillRectangle(Brushes.Black, 0, 0, sW, sH);
g.DrawString(string.Format("RAM:{0} CPU:{1}", DeviceMonitor.getAvailableRAM(), DeviceMonitor.getCurrentCpuUsage()), font, Brushes.White, 5, 5);
g.DrawString(string.Format("網路:{0}", DeviceMonitor.GetInternetAvilable() ? "已連線" : "未連線"), font, Brushes.White, 5, 36);
TimeSpan ts = DeviceMonitor.GetSystemUpTime();
g.DrawString(string.Format("執行:{0}:{1}:{2}", ts.Hours, ts.Minutes, ts.Seconds), font, Brushes.White, 5, 67);
g.DrawString(string.Format("時間:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")), font, Brushes.White, 5, 98);
int wW = 200;
int wH = 200;
var r = new Random();
int z = DateTime.Now.Second;
int cs = r.Next(0, 100);
for (int c=0; c<cs; c++)
{
Pen pen = null;
switch (r.Next(0, 10))
{
case 1: pen = new Pen(Brushes.Red); break;
case 2: pen = new Pen(Brushes.Orange); break;
case 3: pen = new Pen(Brushes.Yellow); break;
case 4: pen = new Pen(Brushes.Green); break;
case 5: pen = new Pen(Brushes.Peru); break;
case 6: pen = new Pen(Brushes.Gray); break;
case 7: pen = new Pen(Brushes.Blue); break;
case 8: pen = new Pen(Brushes.DarkBlue); break;
case 9: pen = new Pen(Brushes.YellowGreen); break;
default: pen = new Pen(Brushes.Purple); break;
}
if (r.Next(0, 2) == 0)
{
wW -= r.Next(0, z);
}
else
{
wW += r.Next(0, z);
}
if (r.Next(0, 2) == 0)
{
wH -= r.Next(0, z);
}
else
{
wH += r.Next(0, z);
}
g.DrawEllipse(penC, (sW - cW) / 2, (sH - cH) / 2, cW, cH);
g.DrawEllipse(pen, (sW - wW) / 2, (sH - wH) / 2, wW, wH);
}
try
{
g.BeginContainer();
var encoderInfo = GetEncoderInfo("image/jpeg");
var encoderParams = GetEncoderParameters();
image.Save(imgFile + imgName, encoderInfo, encoderParams);
image.Dispose();
}
catch (Exception e)
{
throw new Exception($"image save exception {e.Message}");
}
return imgFile + imgName;
}
/// <summary>
/// 設定拉伸圖片桌面桌布
/// https://blog.csdn.net/sonyicn/article/details/746280?utm_source=blogxgwz4
/// </summary>
/// <param name="path"></param>
public static void SetDesktopPicture(string path)
{
RegistryKey myRegKey = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
myRegKey.SetValue("TileWallpaper", "0");
myRegKey.SetValue("WallpagerStyle", "2");
myRegKey.Close();
SystemParametersInfo(20, 1, path, 1);
}
/// <summary>
/// 圖片編碼
/// https://blog.csdn.net/u012835905/article/details/41212819
/// </summary>
/// <param name="mimeType"></param>
/// <returns></returns>
public static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
public static EncoderParameters GetEncoderParameters()
{
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// for the Quality parameter category.
myEncoder = System.Drawing.Imaging.Encoder.Quality;
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
//設定質量 數字越大質量越好,但是到了一定程度質量就不會增加了,MSDN上沒有給範圍,只說是32為非負整數
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
return myEncoderParameters;
}
}
複製程式碼
參考來源: http://outofmemory.cn/code-snippet/1943/c-windows-zhuomianshang-huitu