@@ -23,8 +23,8 @@ public class DXControl : Control
2323 protected readonly PeriodicTimer _timer = new ( TimeSpan . FromMilliseconds ( 10 ) ) ;
2424
2525 // Protected properties
26- protected readonly IComObject < ID2D1Factory1 > _d2DFactory = D2D1Functions . D2D1CreateFactory < ID2D1Factory1 > ( D2D1_FACTORY_TYPE . D2D1_FACTORY_TYPE_SINGLE_THREADED ) ;
27- protected readonly IComObject < IDWriteFactory5 > _dWriteFactory = DWriteFunctions . DWriteCreateFactory < IDWriteFactory5 > ( DWRITE_FACTORY_TYPE . DWRITE_FACTORY_TYPE_SHARED ) ;
26+ protected IComObject < ID2D1Factory1 > ? _d2DFactory = null ;
27+ protected IComObject < IDWriteFactory5 > ? _dWriteFactory = null ;
2828 protected IComObject < ID2D1HwndRenderTarget > ? _renderTarget ;
2929 protected IComObject < ID2D1DeviceContext6 > ? _device ;
3030 protected D2DGraphics ? _graphicsD2d ;
@@ -47,14 +47,14 @@ public class DXControl : Control
4747 /// Gets Direct2D factory.
4848 /// </summary>
4949 [ Browsable ( false ) ]
50- public IComObject < ID2D1Factory1 > Direct2DFactory => _d2DFactory ;
50+ public IComObject < ID2D1Factory1 > ? Direct2DFactory => _d2DFactory ;
5151
5252
5353 /// <summary>
5454 /// Gets DirectWrite factory.
5555 /// </summary>
5656 [ Browsable ( false ) ]
57- public IComObject < IDWriteFactory5 > DirectWriteFactory => _dWriteFactory ;
57+ public IComObject < IDWriteFactory5 > ? DirectWriteFactory => _dWriteFactory ;
5858
5959
6060 /// <summary>
@@ -218,9 +218,6 @@ protected override void DestroyHandle()
218218 {
219219 base . DestroyHandle ( ) ;
220220
221- _d2DFactory . Dispose ( ) ;
222- _dWriteFactory . Dispose ( ) ;
223-
224221 DisposeDevice ( ) ;
225222 }
226223
@@ -272,8 +269,6 @@ protected override void WndProc(ref Message m)
272269 break ;
273270
274271 case WM_DESTROY :
275- _d2DFactory . Dispose ( ) ;
276- _dWriteFactory . Dispose ( ) ;
277272 DisposeDevice ( ) ;
278273
279274 base . WndProc ( ref m ) ;
@@ -480,6 +475,9 @@ private async Task StartFramingTimerAsync()
480475 /// </summary>
481476 public void CreateDevice ( )
482477 {
478+ _d2DFactory = D2D1Functions . D2D1CreateFactory < ID2D1Factory1 > ( D2D1_FACTORY_TYPE . D2D1_FACTORY_TYPE_SINGLE_THREADED ) ;
479+ _dWriteFactory = DWriteFunctions . DWriteCreateFactory < IDWriteFactory5 > ( DWRITE_FACTORY_TYPE . DWRITE_FACTORY_TYPE_SHARED ) ;
480+
483481 var renderTargetProps = new D2D1_RENDER_TARGET_PROPERTIES ( )
484482 {
485483 dpiX = _dpi ,
@@ -517,12 +515,21 @@ public void CreateDevice()
517515 /// </summary>
518516 public void DisposeDevice ( )
519517 {
518+ _graphicsD2d ? . Dispose ( ) ;
519+ _graphicsD2d = null ;
520+
520521 _device ? . Dispose ( ) ;
521522 _device = null ;
522523
523524 _renderTarget ? . Dispose ( ) ;
524525 _renderTarget = null ;
525526
527+ _d2DFactory ? . Dispose ( ) ;
528+ _d2DFactory = null ;
529+
530+ _dWriteFactory ? . Dispose ( ) ;
531+ _dWriteFactory = null ;
532+
526533 GC . Collect ( ) ;
527534 }
528535
0 commit comments