<PackageReference Include="System.Drawing.Common" Version="9.0.5" />

StringFormat

Encapsulates text layout information (such as alignment, orientation and tab stops) display manipulations (such as ellipsis insertion and national digit substitution) and OpenType features. This class cannot be inherited.
using System.ComponentModel; using System.Drawing.Text; using System.Runtime.CompilerServices; using Windows.Win32; using Windows.Win32.Graphics.GdiPlus; namespace System.Drawing { [NullableContext(1)] [Nullable(0)] public sealed class StringFormat : MarshalByRefObject, ICloneable, IDisposable { [Nullable(0)] internal unsafe GpStringFormat* _nativeFormat; public unsafe StringFormatFlags FormatFlags { get { StringFormatFlags result = default(StringFormatFlags); PInvoke.GdipGetStringFormatFlags(_nativeFormat, (int*)(&result)).ThrowIfFailed(); GC.KeepAlive(this); return result; } set { PInvoke.GdipSetStringFormatFlags(_nativeFormat, (int)value).ThrowIfFailed(); GC.KeepAlive(this); } } public unsafe StringAlignment Alignment { get { StringAlignment result = default(StringAlignment); PInvoke.GdipGetStringFormatAlign(_nativeFormat, (global::Windows.Win32.Graphics.GdiPlus.StringAlignment*)(&result)).ThrowIfFailed(); GC.KeepAlive(this); return result; } set { if ((value < StringAlignment.Near || value > StringAlignment.Far) ? true : false) throw new InvalidEnumArgumentException("value", (int)value, typeof(StringAlignment)); PInvoke.GdipSetStringFormatAlign(_nativeFormat, (global::Windows.Win32.Graphics.GdiPlus.StringAlignment)value).ThrowIfFailed(); GC.KeepAlive(this); } } public unsafe StringAlignment LineAlignment { get { StringAlignment result = default(StringAlignment); PInvoke.GdipGetStringFormatLineAlign(_nativeFormat, (global::Windows.Win32.Graphics.GdiPlus.StringAlignment*)(&result)).ThrowIfFailed(); GC.KeepAlive(this); return result; } set { if ((value < StringAlignment.Near || value > StringAlignment.Far) ? true : false) throw new InvalidEnumArgumentException("value", (int)value, typeof(StringAlignment)); PInvoke.GdipSetStringFormatLineAlign(_nativeFormat, (global::Windows.Win32.Graphics.GdiPlus.StringAlignment)value).ThrowIfFailed(); GC.KeepAlive(this); } } public unsafe HotkeyPrefix HotkeyPrefix { get { HotkeyPrefix result = default(HotkeyPrefix); PInvoke.GdipGetStringFormatHotkeyPrefix(_nativeFormat, (int*)(&result)).ThrowIfFailed(); GC.KeepAlive(this); return result; } set { if ((value < HotkeyPrefix.None || value > HotkeyPrefix.Hide) ? true : false) throw new InvalidEnumArgumentException("value", (int)value, typeof(HotkeyPrefix)); PInvoke.GdipSetStringFormatHotkeyPrefix(_nativeFormat, (int)value).ThrowIfFailed(); GC.KeepAlive(this); } } public unsafe StringTrimming Trimming { get { StringTrimming result = default(StringTrimming); PInvoke.GdipGetStringFormatTrimming(_nativeFormat, (global::Windows.Win32.Graphics.GdiPlus.StringTrimming*)(&result)).ThrowIfFailed(); GC.KeepAlive(this); return result; } set { if ((value < StringTrimming.None || value > StringTrimming.EllipsisPath) ? true : false) throw new InvalidEnumArgumentException("value", (int)value, typeof(StringTrimming)); PInvoke.GdipSetStringFormatTrimming(_nativeFormat, (global::Windows.Win32.Graphics.GdiPlus.StringTrimming)value).ThrowIfFailed(); GC.KeepAlive(this); } } public unsafe static StringFormat GenericDefault { get { GpStringFormat* format = default(GpStringFormat*); PInvoke.GdipStringFormatGetGenericDefault(&format).ThrowIfFailed(); return new StringFormat(format); } } public unsafe static StringFormat GenericTypographic { get { GpStringFormat* format = default(GpStringFormat*); PInvoke.GdipStringFormatGetGenericTypographic(&format).ThrowIfFailed(); return new StringFormat(format); } } public unsafe StringDigitSubstitute DigitSubstitutionMethod { get { StringDigitSubstitute result = default(StringDigitSubstitute); PInvoke.GdipGetStringFormatDigitSubstitution(_nativeFormat, null, (global::Windows.Win32.Graphics.GdiPlus.StringDigitSubstitute*)(&result)).ThrowIfFailed(); GC.KeepAlive(this); return result; } } public unsafe int DigitSubstitutionLanguage { get { ushort result = default(ushort); PInvoke.GdipGetStringFormatDigitSubstitution(_nativeFormat, &result, null).ThrowIfFailed(); GC.KeepAlive(this); return result; } } [NullableContext(0)] private unsafe StringFormat(GpStringFormat* format) { _nativeFormat = format; } public StringFormat() : this((StringFormatFlags)0, 0) { } public StringFormat(StringFormatFlags options) : this(options, 0) { } public unsafe StringFormat(StringFormatFlags options, int language) { GpStringFormat* nativeFormat = default(GpStringFormat*); PInvoke.GdipCreateStringFormat((int)options, (ushort)language, &nativeFormat).ThrowIfFailed(); _nativeFormat = nativeFormat; } public unsafe StringFormat(StringFormat format) { ArgumentNullException.ThrowIfNull(format, "format"); GpStringFormat* nativeFormat = default(GpStringFormat*); PInvoke.GdipCloneStringFormat(format._nativeFormat, &nativeFormat).ThrowIfFailed(); _nativeFormat = nativeFormat; GC.KeepAlive(format); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private unsafe void Dispose(bool disposing) { if (_nativeFormat != null) try { PInvoke.GdipDeleteStringFormat(_nativeFormat); } catch (Exception ex) { if (ClientUtils.IsCriticalException(ex)) throw; } finally { _nativeFormat = null; } } public object Clone() { return new StringFormat(this); } public unsafe void SetMeasurableCharacterRanges(CharacterRange[] ranges) { ArgumentNullException.ThrowIfNull(ranges, "ranges"); fixed (CharacterRange* ptr = ranges) { void* ptr2 = ptr; global::Windows.Win32.Graphics.GdiPlus.CharacterRange characterRange = default(global::Windows.Win32.Graphics.GdiPlus.CharacterRange); PInvoke.GdipSetStringFormatMeasurableCharacterRanges(_nativeFormat, ranges.Length, (global::Windows.Win32.Graphics.GdiPlus.CharacterRange*)((ptr2 == null) ? (&characterRange) : ptr2)).ThrowIfFailed(); } GC.KeepAlive(this); } public unsafe void SetTabStops(float firstTabOffset, float[] tabStops) { ArgumentNullException.ThrowIfNull(tabStops, "tabStops"); if (firstTabOffset < 0) throw new ArgumentException(System.SR.Format(System.SR.InvalidArgumentValue, "firstTabOffset", firstTabOffset)); fixed (float* ptr = tabStops) { float num = default(float); PInvoke.GdipSetStringFormatTabStops(_nativeFormat, firstTabOffset, tabStops.Length, (ptr == null) ? (&num) : ptr).ThrowIfFailed(); GC.KeepAlive(this); } } public unsafe float[] GetTabStops(out float firstTabOffset) { int num = default(int); PInvoke.GdipGetStringFormatTabStopCount(_nativeFormat, &num).ThrowIfFailed(); if (num != 0) { float[] obj = new float[num]; fixed (float* firstTabOffset2 = &firstTabOffset) { fixed (float* tabStops = obj) { PInvoke.GdipGetStringFormatTabStops(_nativeFormat, num, firstTabOffset2, tabStops).ThrowIfFailed(); GC.KeepAlive(this); return obj; } } } firstTabOffset = 0; return Array.Empty<float>(); } public unsafe void SetDigitSubstitution(int language, StringDigitSubstitute substitute) { PInvoke.GdipSetStringFormatDigitSubstitution(_nativeFormat, (ushort)language, (global::Windows.Win32.Graphics.GdiPlus.StringDigitSubstitute)substitute).ThrowIfFailed(); GC.KeepAlive(this); } internal unsafe int GetMeasurableCharacterRangeCount() { int result = default(int); PInvoke.GdipGetStringFormatMeasurableCharacterRangeCount(_nativeFormat, &result).ThrowIfFailed(); GC.KeepAlive(this); return result; } ~StringFormat() { Dispose(false); } public override string ToString() { DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(28, 1); defaultInterpolatedStringHandler.AppendLiteral("[StringFormat, FormatFlags="); defaultInterpolatedStringHandler.AppendFormatted(FormatFlags); defaultInterpolatedStringHandler.AppendLiteral("]"); return defaultInterpolatedStringHandler.ToStringAndClear(); } } }