WinDrawTool.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Azylee.Core.DataUtils.StringUtils;
  2. using System;
  3. using System.Drawing;
  4. using System.IO;
  5. namespace Azylee.Core.WindowsUtils.APIUtils.WinDrawUtils
  6. {
  7. /// <summary>
  8. /// 桌面绘制工具
  9. /// </summary>
  10. public static class WindowsDrawTool
  11. {
  12. /// <summary>
  13. /// 将图片绘制到桌面上
  14. /// </summary>
  15. /// <param name="file"></param>
  16. /// <param name="x"></param>
  17. /// <param name="y"></param>
  18. /// <param name="width"></param>
  19. /// <param name="height"></param>
  20. public static void Paint(string file, int x, int y, int width, int height)
  21. {
  22. try
  23. {
  24. Image image = null;
  25. if (Str.Ok(file) && File.Exists(file)) image = Image.FromFile(file);
  26. if (image != null) Paint(image, x, y, width, height);
  27. }
  28. catch { }
  29. }
  30. /// <summary>
  31. /// 将图片绘制到桌面上
  32. /// </summary>
  33. /// <param name="image"></param>
  34. /// <param name="x"></param>
  35. /// <param name="y"></param>
  36. /// <param name="width"></param>
  37. /// <param name="height"></param>
  38. public static void Paint(Image image, int x, int y, int width, int height)
  39. {
  40. IntPtr workerw = IntPtr.Zero;
  41. WindowsDrawAPI.EnumWindows(new WindowsDrawAPI.EnumWindowsProc((tophandle, topparamhandle) =>
  42. {
  43. IntPtr p = WindowsDrawAPI.FindWindowEx(tophandle, IntPtr.Zero, "SHELLDLL_DefView", IntPtr.Zero);
  44. if (p != IntPtr.Zero) workerw = WindowsDrawAPI.FindWindowEx(IntPtr.Zero, tophandle, "WorkerW", IntPtr.Zero);
  45. return true;
  46. }), IntPtr.Zero);
  47. IntPtr dc = WindowsDrawAPI.GetDCEx(workerw, IntPtr.Zero, (WindowsDrawAPI.DeviceContextValues)0x403);
  48. if (dc != IntPtr.Zero)
  49. {
  50. using (Graphics g = Graphics.FromHdc(dc))
  51. {
  52. g.DrawImage(image, x, y, width, height);
  53. }
  54. WindowsDrawAPI.ReleaseDC(workerw, dc);
  55. }
  56. }
  57. }
  58. }