1- using DirectN ;
1+ /*
2+ MIT License
3+ Copyright (C) 2022 DUONG DIEU PHAP
4+ Project & license info: https://github.com/d2phap/DXControls
5+ */
6+ using DirectN ;
27using System . ComponentModel ;
38using System . Runtime . InteropServices ;
49
5- namespace DXControls ;
10+ namespace D2Phap ;
611
12+ /// <summary>
13+ /// Defines the base class for hybrid control with Direct2D and GDI+ graphics support.
14+ /// </summary>
715public class DXControl : Control
816{
917 private float _dpi = 96.0f ;
@@ -15,31 +23,41 @@ public class DXControl : Control
1523 private DateTime _lastFpsUpdate = DateTime . UtcNow ;
1624
1725
26+ // Protected properties
27+ #region Protected properties
1828 protected IComObject < ID2D1Factory > Direct2DFactory ;
1929 protected IComObject < IDWriteFactory > DWriteFactory ;
2030 protected ID2D1HwndRenderTarget ? RenderTarget ;
2131 protected ID2D1DeviceContext ? DeviceContext ;
32+
33+
34+ /// <summary>
35+ /// Gets the <see cref='DXGraphics'/> object used to draw in <see cref="OnRender(DXGraphics)"/>.
36+ /// </summary>
2237 protected DXGraphics ? D2Graphics ;
2338
39+
2440 /// <summary>
2541 /// Request to update frame by <see cref="OnFrame"/> event.
2642 /// </summary>
2743 protected bool RequestUpdateFrame { get ; set ; } = true ;
2844
45+
2946 /// <summary>
3047 /// Enable FPS measurement.
3148 /// </summary>
3249 protected bool CheckFPS { get ; set ; } = false ;
3350
51+ #endregion // Protected properties
3452
35- public event EventHandler < RenderEventArgs > ? RenderDX ;
36- public event EventHandler < PaintEventArgs > ? RenderGDIPlus ;
37- public event EventHandler < FrameEventArgs > ? Frame ;
3853
54+ // Public properties
55+ #region Public properties
3956
4057 /// <summary>
4158 /// Gets, sets the DPI for drawing when using <see cref="DXGraphics"/>.
4259 /// </summary>
60+ [ Browsable ( false ) ]
4361 public float BaseDpi
4462 {
4563 get => _dpi ;
@@ -56,12 +74,14 @@ public float BaseDpi
5674 /// <summary>
5775 /// Gets, sets the DPI for text drawing when using <see cref="DXGraphics"/>.
5876 /// </summary>
77+ [ Browsable ( false ) ]
5978 public float TextDpi { get ; set ; } = 96.0f ;
6079
6180
6281 /// <summary>
6382 /// Gets FPS info when the <see cref="CheckFPS"/> is set to <c>true</c>.
6483 /// </summary>
84+ [ Browsable ( false ) ]
6585 public int FPS => _lastFps ;
6686
6787
@@ -93,17 +113,41 @@ public bool EnableAnimation
93113 }
94114
95115
116+ /// <summary>
117+ /// Occurs when the control is redrawn with <see cref="DXGraphics"/>.
118+ /// </summary>
119+ public event EventHandler < RenderDXEventArgs > ? RenderDX ;
120+
121+
122+ /// <summary>
123+ /// Occurs when the control is redrawn with <see cref="Graphics"/>.
124+ /// </summary>
125+ public event EventHandler < PaintEventArgs > ? RenderGDIPlus ;
126+
127+
128+ /// <summary>
129+ /// Occurs when the animation frame logics need to update.
130+ /// </summary>
131+ public event EventHandler < FrameEventArgs > ? Frame ;
132+
133+ #endregion // Public properties
134+
96135
136+
137+ /// <summary>
138+ /// Initializes new instance of <see cref="DXControl"/>.
139+ /// </summary>
97140 public DXControl ( )
98141 {
99142 SetStyle ( ControlStyles . OptimizedDoubleBuffer | ControlStyles . SupportsTransparentBackColor , true ) ;
100143
101144 Direct2DFactory = D2D1Functions . D2D1CreateFactory ( D2D1_FACTORY_TYPE . D2D1_FACTORY_TYPE_SINGLE_THREADED ) ;
102-
145+
103146 DWriteFactory = DWriteFunctions . DWriteCreateFactory ( DWRITE_FACTORY_TYPE . DWRITE_FACTORY_TYPE_SHARED ) ;
104147 }
105148
106149
150+ // Override functions
107151 #region Override functions
108152
109153 protected override void CreateHandle ( )
@@ -114,52 +158,11 @@ protected override void CreateHandle()
114158 DoubleBuffered = false ;
115159 CreateGraphicsResources ( ) ;
116160
117-
118161 _ticker . Tick += Ticker_Tick ;
119162 _ticker . Start ( ) ;
120163 }
121164
122-
123- protected override void WndProc ( ref Message m )
124- {
125- const int WM_ERASEBKGND = 0x0014 ;
126- const int WM_SIZE = 0x0005 ;
127- const int WM_DESTROY = 0x0002 ;
128165
129- switch ( m . Msg )
130- {
131- //case WM_ERASEBKGND:
132-
133- // // to fix background is delayed to paint on launch
134- // if (_firstPaintBackground)
135- // {
136- // _firstPaintBackground = false;
137- // if (!_useHardwardAcceleration)
138- // {
139- // base.WndProc(ref m);
140- // }
141- // else
142- // {
143- // _graphics?.BeginRender(D2DColor.FromGDIColor(BackColor));
144- // _graphics?.EndRender();
145- // }
146- // }
147- // break;
148-
149- case WM_SIZE :
150- base . WndProc ( ref m ) ;
151-
152- //RenderTarget?.Resize(new(ClientSize.Width, ClientSize.Height));
153- break ;
154-
155-
156- default :
157- base . WndProc ( ref m ) ;
158- break ;
159- }
160- }
161-
162-
163166 protected override void Dispose ( bool disposing )
164167 {
165168 _ticker . Stop ( 1000 ) ;
@@ -241,19 +244,20 @@ protected override void OnPaint(PaintEventArgs e)
241244 CreateGraphicsResources ( ) ;
242245 if ( DeviceContext == null || D2Graphics == null ) return ;
243246
244-
245- // Use Direct2D graphics to draw
246- // start drawing session
247247 var bgColor = BackColor . Equals ( Color . Transparent ) ? Parent . BackColor : BackColor ;
248- D2Graphics . BeginDraw ( new ( bgColor . ToArgb ( ) ) ) ;
248+ DoubleBuffered = false ; // must be false
249+
250+
251+ // start Direct2D graphics drawing session
252+ DeviceContext . BeginDraw ( ) ;
253+ D2Graphics . ClearBackground ( new ( bgColor . ToArgb ( ) ) ) ;
249254 OnRender ( D2Graphics ) ;
250255
251256 // end drawing session
252- D2Graphics . EndDraw ( ) ;
257+ DeviceContext . EndDraw ( ) ;
253258
254259
255- // Use GDPI+ to draw
256- e . Graphics . Clear ( bgColor ) ;
260+ // Use GDI+ to draw
257261 OnRender ( e . Graphics ) ;
258262
259263
@@ -273,18 +277,18 @@ protected override void OnPaint(PaintEventArgs e)
273277 }
274278 }
275279
276-
277- #endregion
280+ #endregion // Override functions
278281
279282
283+ // Virtual functions
280284 #region Virtual functions
281285
282286 /// <summary>
283287 /// Paints the control using Direct2D <see cref="DXGraphics"/>.
284288 /// </summary>
285289 protected virtual void OnRender ( DXGraphics g )
286290 {
287- RenderDX ? . Invoke ( this , new RenderEventArgs ( g ) ) ;
291+ RenderDX ? . Invoke ( this , new RenderDXEventArgs ( g ) ) ;
288292 }
289293
290294
@@ -295,7 +299,7 @@ protected virtual void OnRender(Graphics g)
295299 {
296300 RenderGDIPlus ? . Invoke ( this , new ( g , ClientRectangle ) ) ;
297301 }
298-
302+
299303
300304 /// <summary>
301305 /// Process animation logic when frame changes
@@ -305,10 +309,10 @@ protected virtual void OnFrame(FrameEventArgs e)
305309 Frame ? . Invoke ( this , e ) ;
306310 }
307311
308- #endregion
309-
312+ #endregion // Virtual functions
310313
311314
315+ // Private functions
312316 #region Private functions
313317
314318 /// <summary>
@@ -328,7 +332,7 @@ private void CreateGraphicsResources()
328332
329333 RenderTarget . SetDpi ( BaseDpi , BaseDpi ) ;
330334 RenderTarget . Resize ( new ( ClientSize . Width , ClientSize . Height ) ) ;
331-
335+
332336
333337 DeviceContext = ( ID2D1DeviceContext ) RenderTarget ;
334338 D2Graphics = new DXGraphics ( DeviceContext , DWriteFactory ) ;
@@ -345,13 +349,6 @@ private void Ticker_Tick(object? sender, VerticalBlankTickerEventArgs e)
345349 }
346350 }
347351
348- #endregion
349-
350-
351- #region Public functions
352-
353-
354-
355- #endregion
352+ #endregion // Private functions
356353
357354}
0 commit comments