CreateBrushScope
struct CreateBrushScope
Helper to scope the lifetime of a HBRUSH.
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Windows.Win32.Graphics.Gdi
{
[CompilerFeatureRequired("RefStructs")]
internal readonly ref struct CreateBrushScope
{
public HBRUSH HBRUSH { get; }
public bool IsNull => HBRUSH.IsNull;
public CreateBrushScope(Color color)
{
HBRUSH = (color.IsSystemColor ? PInvokeCore.GetSysColorBrush(color) : PInvokeCore.CreateSolidBrush(color));
}
public static implicit operator HBRUSH([In] [IsReadOnly] ref CreateBrushScope scope)
{
return scope.HBRUSH;
}
public static implicit operator HGDIOBJ([In] [IsReadOnly] ref CreateBrushScope scope)
{
return scope.HBRUSH;
}
public void Dispose()
{
if (!HBRUSH.IsNull)
PInvokeCore.DeleteObject(HBRUSH);
}
[Conditional("DEBUG")]
private void ValidateBrushHandle()
{
if (HBRUSH.IsNull)
throw new Win32Exception("Could not create a GDI brush.");
}
}
}