WinDrawTool.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Drawing;
  3. namespace Azylee.Core.WindowsUtils.APIUtils.WinDrawUtils
  4. {
  5. /// <summary>
  6. /// 桌面绘制工具
  7. /// </summary>
  8. public static class WindowsDrawTool
  9. {
  10. /// <summary>
  11. /// 将图片绘制到桌面上
  12. /// </summary>
  13. /// <param name="image"></param>
  14. /// <param name="x"></param>
  15. /// <param name="y"></param>
  16. /// <param name="width"></param>
  17. /// <param name="height"></param>
  18. public static void Paint(Image image, int x, int y, int width, int height)
  19. {
  20. IntPtr workerw = IntPtr.Zero;
  21. WindowsDrawAPI.EnumWindows(new WindowsDrawAPI.EnumWindowsProc((tophandle, topparamhandle) =>
  22. {
  23. IntPtr p = WindowsDrawAPI.FindWindowEx(tophandle, IntPtr.Zero, "SHELLDLL_DefView", IntPtr.Zero);
  24. if (p != IntPtr.Zero) workerw = WindowsDrawAPI.FindWindowEx(IntPtr.Zero, tophandle, "WorkerW", IntPtr.Zero);
  25. return true;
  26. }), IntPtr.Zero);
  27. IntPtr dc = WindowsDrawAPI.GetDCEx(workerw, IntPtr.Zero, (WindowsDrawAPI.DeviceContextValues)0x403);
  28. if (dc != IntPtr.Zero)
  29. {
  30. using (Graphics g = Graphics.FromHdc(dc))
  31. {
  32. g.DrawImage(image, x, y, width, height);
  33. }
  34. WindowsDrawAPI.ReleaseDC(workerw, dc);
  35. }
  36. }
  37. }
  38. }