<PackageReference Include="System.Drawing.Common" Version="10.0.0-preview.5.25277.114" />

SetTextAlignmentScope

Helper to scope selecting a given text alignment mode into a HDC. Restores the original text alignment mode into the HDC when disposed.
using System.Runtime.CompilerServices; namespace Windows.Win32.Graphics.Gdi { [CompilerFeatureRequired("RefStructs")] internal readonly ref struct SetTextAlignmentScope { private readonly TEXT_ALIGN_OPTIONS _previousTa; private readonly HDC _hdc; public SetTextAlignmentScope(HDC hdc, TEXT_ALIGN_OPTIONS ta) { _previousTa = (TEXT_ALIGN_OPTIONS)PInvokeCore.SetTextAlign(hdc, ta); _hdc = ((_previousTa == ta) ? default(HDC) : hdc); } public void Dispose() { if (!_hdc.IsNull) PInvokeCore.SetTextAlign(_hdc, _previousTa); } } }