NullGraphics.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SmartCoalApplication.SystemLayer
  8. {
  9. public sealed class NullGraphics
  10. : IDisposable
  11. {
  12. private IntPtr hdc = IntPtr.Zero;
  13. private Graphics graphics = null;
  14. private bool disposed = false;
  15. public Graphics Graphics
  16. {
  17. get
  18. {
  19. return graphics;
  20. }
  21. }
  22. public NullGraphics()
  23. {
  24. this.hdc = SafeNativeMethods.CreateCompatibleDC(IntPtr.Zero);
  25. if (this.hdc == IntPtr.Zero)
  26. {
  27. NativeMethods.ThrowOnWin32Error("CreateCompatibleDC returned NULL");
  28. }
  29. this.graphics = Graphics.FromHdc(this.hdc);
  30. }
  31. ~NullGraphics()
  32. {
  33. Dispose(false);
  34. }
  35. public void Dispose()
  36. {
  37. Dispose(true);
  38. GC.SuppressFinalize(this);
  39. }
  40. private void Dispose(bool disposing)
  41. {
  42. if (!disposed)
  43. {
  44. if (disposing)
  45. {
  46. this.graphics.Dispose();
  47. this.graphics = null;
  48. }
  49. SafeNativeMethods.DeleteDC(this.hdc);
  50. disposed = true;
  51. }
  52. }
  53. }
  54. }