PaintManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. using Microsoft.Win32;
  6. namespace Microsoft.Windows.Forms
  7. {
  8. /// <summary>
  9. /// 渲染管理器
  10. /// </summary>
  11. public static class PaintManager
  12. {
  13. /// <summary>
  14. /// 先在缓冲区渲染再复制到目标设备(客户区)
  15. /// </summary>
  16. /// <param name="window">可双缓冲渲染的控件</param>
  17. /// <param name="e">原始渲染数据数据或称作目标设备渲染数据</param>
  18. public static void OnPaint(IUIWindow window, PaintEventArgs e)
  19. {
  20. //缓冲区准备
  21. if (!window.DoubleBufferedGraphics.Prepare())
  22. return;
  23. //使用缓冲区绘图
  24. Rectangle clip = e.ClipRectangle;
  25. using (PaintEventArgs b = new PaintEventArgs(window.DoubleBufferedGraphics.Graphics, window.ClientRectangle))
  26. {
  27. window.DoubleBufferedGraphics.Graphics.SetClip(clip, CombineMode.Replace);
  28. window.RenderCore(b);
  29. }
  30. //使用缓冲区渲染目标设备
  31. e.Graphics.SetClip(clip, CombineMode.Replace);
  32. window.DoubleBufferedGraphics.Render(e);
  33. }
  34. /// <summary>
  35. /// WM_PAINT消息处理 不进行渲染
  36. /// </summary>
  37. /// <param name="m">消息</param>
  38. public static void WmPaint(ref Message m)
  39. {
  40. //========No Drawing
  41. UnsafeNativeMethods.ValidateRect(m.HWnd, IntPtr.Zero);
  42. }
  43. /// <summary>
  44. /// WM_PAINT消息处理
  45. /// </summary>
  46. /// <param name="window">可双缓冲渲染的控件(客户区)</param>
  47. /// <param name="m">消息</param>
  48. public static void WmPaint(IUIWindow window, ref Message m)
  49. {
  50. WmPaint(window, ref m, null);
  51. }
  52. /// <summary>
  53. /// WM_PAINT消息处理
  54. /// </summary>
  55. /// <param name="window">可双缓冲渲染的控件(客户区)</param>
  56. /// <param name="m">消息</param>
  57. /// <param name="e">如果该值为null,则自动创建默认PaintEventArgs.否则将直接使用该值作为OnPaint的参数</param>
  58. public static void WmPaint(IUIWindow window, ref Message m, PaintEventArgs e)
  59. {
  60. //========Begin
  61. IntPtr hWnd = m.HWnd;
  62. NativeMethods.PAINTSTRUCT ps = new NativeMethods.PAINTSTRUCT();
  63. IntPtr hDC = UnsafeNativeMethods.BeginPaint(hWnd, ref ps);
  64. //========Drawing
  65. try
  66. {
  67. if (e == null)
  68. {
  69. using (Graphics g = Graphics.FromHdcInternal(hDC))
  70. {
  71. using (e = new PaintEventArgs(g, Rectangle.FromLTRB(ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom)))
  72. {
  73. OnPaint(window, e);
  74. }
  75. }
  76. }
  77. else
  78. {
  79. OnPaint(window, e);
  80. }
  81. }
  82. finally
  83. {
  84. //========End
  85. UnsafeNativeMethods.EndPaint(hWnd, ref ps);
  86. }
  87. }
  88. }
  89. }