DrawUtils.Win32.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5. namespace FastReport.Utils
  6. {
  7. partial class DrawUtils
  8. {
  9. #if CROSSPLATFORM
  10. static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, DrawingOptions drawingOptions)
  11. {
  12. return IntPtr.Zero;
  13. }
  14. #else
  15. [DllImport("user32.dll")]
  16. static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, DrawingOptions drawingOptions);
  17. #endif
  18. const int WM_PRINT = 0x317;
  19. [Flags]
  20. enum DrawingOptions
  21. {
  22. PRF_CHECKVISIBLE = 0x01,
  23. PRF_NONCLIENT = 0x02,
  24. PRF_CLIENT = 0x04,
  25. PRF_ERASEBKGND = 0x08,
  26. PRF_CHILDREN = 0x10,
  27. PRF_OWNED = 0x20
  28. }
  29. /// <summary>
  30. /// Draws control to a bitmap.
  31. /// </summary>
  32. /// <param name="control">Control to draw.</param>
  33. /// <param name="children">Determines whether to draw control's children or not.</param>
  34. /// <returns>The bitmap.</returns>
  35. public static Bitmap DrawToBitmap(Control control, bool children)
  36. {
  37. Bitmap bitmap = new Bitmap(control.Width, control.Height);
  38. using (Graphics gr = Graphics.FromImage(bitmap))
  39. {
  40. IntPtr hdc = gr.GetHdc();
  41. DrawingOptions options = DrawingOptions.PRF_ERASEBKGND |
  42. DrawingOptions.PRF_CLIENT | DrawingOptions.PRF_NONCLIENT;
  43. if (children)
  44. options |= DrawingOptions.PRF_CHILDREN;
  45. SendMessage(control.Handle, WM_PRINT, hdc, options);
  46. gr.ReleaseHdc(hdc);
  47. }
  48. return bitmap;
  49. }
  50. }
  51. }