SafeDC.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.ConstrainedExecution;
  4. using System.Runtime.InteropServices;
  5. using Microsoft.Win32.SafeHandles;
  6. namespace Standard;
  7. internal sealed class SafeDC : SafeHandleZeroOrMinusOneIsInvalid
  8. {
  9. public IntPtr Hwnd
  10. {
  11. set
  12. {
  13. this._hwnd = new IntPtr?(value);
  14. }
  15. }
  16. private SafeDC() : base(true)
  17. {
  18. }
  19. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  20. protected override bool ReleaseHandle()
  21. {
  22. if (this._created)
  23. {
  24. return SafeDC.NativeMethods.DeleteDC(this.handle);
  25. }
  26. return this._hwnd == null || this._hwnd.Value == IntPtr.Zero || SafeDC.NativeMethods.ReleaseDC(this._hwnd.Value, this.handle) == 1;
  27. }
  28. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  29. [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
  30. public static SafeDC CreateDC(string deviceName)
  31. {
  32. SafeDC safeDC = null;
  33. try
  34. {
  35. safeDC = SafeDC.NativeMethods.CreateDC(deviceName, null, IntPtr.Zero, IntPtr.Zero);
  36. }
  37. finally
  38. {
  39. if (safeDC != null)
  40. {
  41. safeDC._created = true;
  42. }
  43. }
  44. if (safeDC.IsInvalid)
  45. {
  46. safeDC.Dispose();
  47. throw new SystemException("Unable to create a device context from the specified device information.");
  48. }
  49. return safeDC;
  50. }
  51. [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
  52. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  53. public static SafeDC CreateCompatibleDC(SafeDC hdc)
  54. {
  55. SafeDC safeDC = null;
  56. try
  57. {
  58. IntPtr hdc2 = IntPtr.Zero;
  59. if (hdc != null)
  60. {
  61. hdc2 = hdc.handle;
  62. }
  63. safeDC = SafeDC.NativeMethods.CreateCompatibleDC(hdc2);
  64. if (safeDC == null)
  65. {
  66. HRESULT.ThrowLastError();
  67. }
  68. }
  69. finally
  70. {
  71. if (safeDC != null)
  72. {
  73. safeDC._created = true;
  74. }
  75. }
  76. if (safeDC.IsInvalid)
  77. {
  78. safeDC.Dispose();
  79. throw new SystemException("Unable to create a device context from the specified device information.");
  80. }
  81. return safeDC;
  82. }
  83. public static SafeDC GetDC(IntPtr hwnd)
  84. {
  85. SafeDC safeDC = null;
  86. try
  87. {
  88. safeDC = SafeDC.NativeMethods.GetDC(hwnd);
  89. }
  90. finally
  91. {
  92. if (safeDC != null)
  93. {
  94. safeDC.Hwnd = hwnd;
  95. }
  96. }
  97. if (safeDC.IsInvalid)
  98. {
  99. HRESULT.E_FAIL.ThrowIfFailed();
  100. }
  101. return safeDC;
  102. }
  103. public static SafeDC GetDesktop()
  104. {
  105. return SafeDC.GetDC(IntPtr.Zero);
  106. }
  107. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  108. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  109. public static SafeDC WrapDC(IntPtr hdc)
  110. {
  111. return new SafeDC
  112. {
  113. handle = hdc,
  114. _created = false,
  115. _hwnd = new IntPtr?(IntPtr.Zero)
  116. };
  117. }
  118. private IntPtr? _hwnd;
  119. private bool _created;
  120. private static class NativeMethods
  121. {
  122. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  123. [DllImport("user32.dll")]
  124. public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  125. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  126. [DllImport("user32.dll")]
  127. public static extern SafeDC GetDC(IntPtr hwnd);
  128. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  129. [DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
  130. public static extern SafeDC CreateDC([MarshalAs(UnmanagedType.LPWStr)] string lpszDriver, [MarshalAs(UnmanagedType.LPWStr)] string lpszDevice, IntPtr lpszOutput, IntPtr lpInitData);
  131. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  132. [DllImport("gdi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  133. public static extern SafeDC CreateCompatibleDC(IntPtr hdc);
  134. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  135. [DllImport("gdi32.dll")]
  136. [return: MarshalAs(UnmanagedType.Bool)]
  137. public static extern bool DeleteDC(IntPtr hdc);
  138. }
  139. }