CreateDcScope
struct CreateDcScope
Helper to scope lifetime of an HDC retrieved via CreateDC/CreateCompatibleDC.
Deletes the HDC (if any) when disposed.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Windows.Win32.Foundation;
namespace Windows.Win32.Graphics.Gdi
{
[CompilerFeatureRequired("RefStructs")]
internal readonly ref struct CreateDcScope
{
public HDC HDC { get; }
public bool IsNull => HDC.IsNull;
public CreateDcScope(HDC hdc)
{
HDC = PInvokeCore.CreateCompatibleDC(hdc);
}
public unsafe CreateDcScope([Nullable(1)] string driverName, [Nullable(2)] string deviceName = null, DEVMODEW* lpInitData = default(DEVMODEW*), bool informationOnly = true)
{
IntPtr intPtr;
if (driverName == null)
intPtr = (IntPtr)(void*)null;
else {
ref reference = ref driverName.GetPinnableReference();
intPtr = (IntPtr)(&reference);
}
char* value = (char*)(long)intPtr;
IntPtr intPtr2;
if (deviceName == null)
intPtr2 = (IntPtr)(void*)null;
else {
ref reference2 = ref deviceName.GetPinnableReference();
intPtr2 = (IntPtr)(&reference2);
}
char* value2 = (char*)(long)intPtr2;
HDC = (informationOnly ? PInvokeCore.CreateICW(value, value2, null, lpInitData) : PInvokeCore.CreateDCW(value, value2, null, lpInitData));
ref reference2 = ref *(char*)null;
ref reference = ref *(char*)null;
}
public static implicit operator HDC([In] [IsReadOnly] ref CreateDcScope scope)
{
return scope.HDC;
}
public unsafe static implicit operator HGDIOBJ([In] [IsReadOnly] ref CreateDcScope scope)
{
return (HGDIOBJ)scope.HDC.Value;
}
public static implicit operator IntPtr([In] [IsReadOnly] ref CreateDcScope scope)
{
return scope.HDC;
}
public unsafe static explicit operator WPARAM([In] [IsReadOnly] ref CreateDcScope scope)
{
return (UIntPtr)(void*)(long)(IntPtr)scope.HDC;
}
public void Dispose()
{
if (!HDC.IsNull)
PInvokeCore.DeleteDC(HDC);
}
}
}