-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollingTextControl.cs
More file actions
1065 lines (919 loc) · 43.7 KB
/
ScrollingTextControl.cs
File metadata and controls
1065 lines (919 loc) · 43.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Direct2D;
using static Direct2D.D2DTools;
using DWrite;
using DXGI;
using static DXGI.DXGITools;
using WIC;
using GlobalStructures;
using static GlobalStructures.GlobalTools;
namespace CSharp_ScrollingTextControl
{
public partial class ScrollingTextControl : UserControl
{
private ID2D1Factory1 m_pD2DFactory1 = null;
private IDWriteFactory m_pDWriteFactory = null;
private IWICImagingFactory m_pWICImagingFactory = null;
private string m_sText = string.Empty;
private ColorF.Enum m_TextColor = ColorF.Enum.Black;
private ColorF.Enum m_BackColor = ColorF.Enum.White;
private ID2D1SolidColorBrush m_pMainBrush = null;
private ColorF.Enum m_pGradientColor1 = ColorF.Enum.White;
private ColorF.Enum m_pGradientColor2 = ColorF.Enum.White;
private ID2D1LinearGradientBrush m_pLinearGradientBrush = null;
private ID2D1BitmapBrush1 m_pBackBitmapBrush = null;
private IntPtr m_pD3D11DevicePtr = IntPtr.Zero;
private IntPtr m_pD3D11DeviceContextPtr = IntPtr.Zero;
private IDXGIDevice1 m_pDXGIDevice = null;
private ID2D1Bitmap1 m_pD2DTargetBitmap = null;
private IDXGISwapChain1 m_pDXGISwapChain1 = null;
private ID2D1Device m_pD2DDevice = null;
private ID2D1DeviceContext m_pD2DContext = null;
private IDWriteTextFormat m_pTextFormat = null;
private IDWriteTextLayout m_pTextLayout = null;
private ID2D1Effect m_pShadowEffect = null;
private int m_nOrientation = 0;
private float m_nX = 0;
private float m_nY = 0;
private string m_sFont = "Arial";
private int m_nFontsize = 0;
private bool m_bBold = false;
private bool m_bItalic = false;
private bool m_bShadow = false;
private bool m_bFade = false;
private float m_nSpeed = 1;
private int m_nWidth = 500;
private int m_nHeight = 500;
IntPtr m_hFrameLatencyWaitable = IntPtr.Zero;
bool m_bRunning = false;
public const int WM_DESTROY = 0x0002;
public const int WM_SIZE = 0x0005;
public const int WM_PAINT = 0x000F;
[StructLayout(LayoutKind.Sequential)]
public struct PAINTSTRUCT
{
public IntPtr hdc;
[MarshalAs(UnmanagedType.Bool)] public bool fErase;
public int rcPaint_left;
public int rcPaint_top;
public int rcPaint_right;
public int rcPaint_bottom;
[MarshalAs(UnmanagedType.Bool)] public bool fRestore;
[MarshalAs(UnmanagedType.Bool)] public bool fIncUpdate;
public int reserved1;
public int reserved2;
public int reserved3;
public int reserved4;
public int reserved5;
public int reserved6;
public int reserved7;
public int reserved8;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InvalidateRect(IntPtr hWnd, ref RECT lpRect, bool bErase);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint WaitForSingleObjectEx(IntPtr hHandle, uint dwMilliseconds, bool bAlertable);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint WaitForMultipleObjectsEx(uint nCount, IntPtr[] lpHandles, [MarshalAs(UnmanagedType.Bool)] bool bWaitAll,
uint dwMilliseconds, [MarshalAs(UnmanagedType.Bool)] bool bAlertable);
private const uint INFINITE = 0xFFFFFFFF;
public ScrollingTextControl()
{
InitializeComponent();
this.SetStyle(ControlStyles.DoubleBuffer, true);
}
public void Initialize(ID2D1Factory1 pD2DFactory1, IDWriteFactory pDWriteFactory,IWICImagingFactory pWICImagingFactory,
string sText, string sFont, int nFontSize, int nWidth, int nHeight, int nOrientation = 0, bool bBold = false, bool bItalic = false,
bool bShadow = false, bool bFade = false)
{
m_pD2DFactory1 = pD2DFactory1;
m_pDWriteFactory = pDWriteFactory;
m_pWICImagingFactory = pWICImagingFactory;
m_sText = sText;
m_sFont = sFont;
m_nFontsize = nFontSize;
m_nWidth = nWidth;
m_nHeight = nHeight;
m_nOrientation = nOrientation;
m_bBold = bBold;
m_bItalic = bItalic;
m_bShadow = bShadow;
m_bFade = bFade;
HRESULT hr = CreateD3D11Device();
hr = CreateDeviceResources();
hr = CreateSwapChain(this.Handle);
if (SUCCEEDED(hr))
{
hr = ConfigureSwapChain();
m_bRunning = true;
StartRenderLoop();
//var renderThread = new Thread(() =>
//{
// const int WAIT_TIMEOUT = 16; // ~60 FPS
// while (m_bRunning)
// {
// if (m_hFrameLatencyWaitable != IntPtr.Zero)
// {
// uint waitResult = WaitForSingleObjectEx(m_hFrameLatencyWaitable, WAIT_TIMEOUT, true);
// if (waitResult == 0) // object signaled
// {
// if (!this.IsDisposed)
// {
// this.BeginInvoke(new Action(() =>
// {
// Invalidate();
// }));
// }
// }
// }
// else
// {
// Thread.Sleep(16); // 60 FPS
// }
// }
//});
//renderThread.IsBackground = true;
//renderThread.Start();
}
m_nX = this.ClientSize.Width;
m_nY = this.ClientSize.Height;
}
//private System.Threading.Timer _renderTimer;
private System.Windows.Forms.Timer _FormsTimer;
public void StartRenderLoop()
{
//_renderTimer = new System.Threading.Timer(_ =>
//{
// //if (!this.IsDisposed)
// // this.BeginInvoke((Action)(() => Invalidate()));
// if (!this.IsDisposed)
// this.BeginInvoke((Action)(() => InvalidateRect(this.Handle, IntPtr.Zero, false)));
//}, null, 0, 1);
if (_FormsTimer == null)
{
_FormsTimer = new System.Windows.Forms.Timer();
_FormsTimer.Interval = 1;
_FormsTimer.Tick += (s, e) =>
{
InvalidateRect(this.Handle, IntPtr.Zero, false);
};
_FormsTimer.Start();
}
}
public void StopRenderLoop()
{
// _renderTimer?.Dispose();
// _renderTimer = null;
_FormsTimer?.Stop();
_FormsTimer?.Dispose();
_FormsTimer = null;
}
private Thread _renderThread;
private AutoResetEvent _stopEvent = new AutoResetEvent(false);
private void StartRenderLoop2()
{
if (_renderThread != null && _renderThread.IsAlive)
return;
m_bRunning = true;
_renderThread = new Thread(() =>
{
IntPtr hFrameEvent = m_hFrameLatencyWaitable;
// Wait handles: stopEvent (0), frameEvent (1)
IntPtr[] waitHandles = new IntPtr[2] { _stopEvent.SafeWaitHandle.DangerousGetHandle(), hFrameEvent };
while (m_bRunning)
{
uint waitResult = WaitForMultipleObjectsEx(
(uint)waitHandles.Length,
waitHandles,
false,
16, // ~60 FPS cap
true // alertable
);
if (waitResult == 0) // Stop requested
break;
if (waitResult == 1) // DXGI frame ready
{
if (!this.IsDisposed && this.IsHandleCreated)
{
this.BeginInvoke((Action)(() => Invalidate()));
}
}
}
});
_renderThread.IsBackground = true;
_renderThread.Start();
}
private void StopRenderLoop2()
{
if (!(_renderThread?.IsAlive ?? false))
return;
m_bRunning = false;
_stopEvent.Set(); // unblock the wait
_renderThread.Join();
}
public virtual ColorF.Enum TextColor
{
get => m_TextColor;
set
{
m_TextColor = value;
SafeRelease(ref m_pMainBrush);
if (m_pD2DContext != null)
m_pD2DContext.CreateSolidColorBrush(new ColorF(m_TextColor), BrushProperties(), out m_pMainBrush);
}
}
public virtual ColorF.Enum BackgroundColor
{
get => m_BackColor;
set => m_BackColor = value;
}
public virtual float Speed
{
get => m_nSpeed;
set => m_nSpeed = value;
}
public HRESULT CreateD3D11Device()
{
HRESULT hr = HRESULT.S_OK;
uint creationFlags = (uint)D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if DEBUG
creationFlags |= (uint)D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_DEBUG;
#endif
int[] featureLevels =
{
(int)D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_1,
(int)D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
(int)D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_10_1,
(int)D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_10_0,
(int)D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_3,
(int)D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_2,
(int)D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_9_1
};
D3D_FEATURE_LEVEL featureLevel;
hr = D3D11CreateDevice(
null,
D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE,
IntPtr.Zero,
creationFlags,
featureLevels,
(uint)featureLevels.Length,
D2DTools.D3D11_SDK_VERSION,
out m_pD3D11DevicePtr,
out featureLevel,
out m_pD3D11DeviceContextPtr
);
if (hr == HRESULT.S_OK)
{
m_pDXGIDevice = (IDXGIDevice1)Marshal.GetObjectForIUnknown(m_pD3D11DevicePtr);
hr = m_pD2DFactory1.CreateDevice(m_pDXGIDevice, out m_pD2DDevice);
if (hr == HRESULT.S_OK)
{
hr = m_pD2DDevice.CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS.D2D1_DEVICE_CONTEXT_OPTIONS_NONE, out m_pD2DContext);
m_pD2DContext.SetAntialiasMode(D2D1_ANTIALIAS_MODE.D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
m_pD2DContext.SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE.D2D1_TEXT_ANTIALIAS_MODE_DEFAULT);
Marshal.ReleaseComObject(m_pD2DDevice);
}
Marshal.Release(m_pD3D11DevicePtr);
Marshal.Release(m_pD3D11DeviceContextPtr);
}
return hr;
}
private HRESULT CreateSwapChain(IntPtr hWnd)
{
HRESULT hr = HRESULT.S_OK;
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = new DXGI_SWAP_CHAIN_DESC1
{
Width = (uint)ClientSize.Width,
Height = (uint)ClientSize.Height,
Format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM,
Stereo = false,
SampleDesc = new DXGI_SAMPLE_DESC { Count = 1, Quality = 0 },
BufferUsage = D2DTools.DXGI_USAGE_RENDER_TARGET_OUTPUT,
BufferCount = 2,
Scaling = hWnd != IntPtr.Zero ? DXGI_SCALING.DXGI_SCALING_NONE : DXGI_SCALING.DXGI_SCALING_STRETCH,
SwapEffect = DXGI_SWAP_EFFECT.DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL,
Flags = DXGI_SWAP_CHAIN_FLAG.DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT
};
IDXGIAdapter pDXGIAdapter = null;
hr = m_pDXGIDevice.GetAdapter(out pDXGIAdapter);
if (hr == HRESULT.S_OK)
{
IntPtr pDXGIFactory2Ptr;
hr = pDXGIAdapter.GetParent(typeof(IDXGIFactory2).GUID, out pDXGIFactory2Ptr);
if (hr == HRESULT.S_OK)
{
IDXGIFactory2 pDXGIFactory2 = (IDXGIFactory2)Marshal.GetObjectForIUnknown(pDXGIFactory2Ptr);
if (hWnd != IntPtr.Zero)
hr = pDXGIFactory2.CreateSwapChainForHwnd(m_pD3D11DevicePtr, hWnd, swapChainDesc, IntPtr.Zero, null, out m_pDXGISwapChain1);
else
hr = pDXGIFactory2.CreateSwapChainForComposition(m_pD3D11DevicePtr, swapChainDesc, null, out m_pDXGISwapChain1);
if (SUCCEEDED(hr))
{
IDXGISwapChain2 pDXGISwapChain2 = m_pDXGISwapChain1 as IDXGISwapChain2;
if (pDXGISwapChain2 != null)
{
hr = pDXGISwapChain2.SetMaximumFrameLatency(1);
m_hFrameLatencyWaitable = pDXGISwapChain2.GetFrameLatencyWaitableObject();
//SafeRelease(ref pDXGISwapChain2);
}
}
SafeRelease(ref pDXGIFactory2);
Marshal.Release(pDXGIFactory2Ptr);
}
SafeRelease(ref pDXGIAdapter);
}
return hr;
}
private HRESULT ConfigureSwapChain()
{
HRESULT hr = HRESULT.S_OK;
D2D1_BITMAP_PROPERTIES1 bitmapProperties = new D2D1_BITMAP_PROPERTIES1
{
bitmapOptions = D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS.D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
pixelFormat = D2DTools.PixelFormat(DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_IGNORE)
};
uint dpi = GetDpiForWindow(Handle);
bitmapProperties.dpiX = dpi;
bitmapProperties.dpiY = dpi;
IntPtr pDXGISurfacePtr = IntPtr.Zero;
hr = m_pDXGISwapChain1.GetBuffer(0, typeof(IDXGISurface).GUID, out pDXGISurfacePtr);
if (hr == HRESULT.S_OK)
{
IDXGISurface pDXGISurface = (IDXGISurface)Marshal.GetObjectForIUnknown(pDXGISurfacePtr);
hr = m_pD2DContext.CreateBitmapFromDxgiSurface(pDXGISurface, bitmapProperties, out m_pD2DTargetBitmap);
if (hr == HRESULT.S_OK)
{
m_pD2DContext.SetTarget(m_pD2DTargetBitmap);
}
SafeRelease(ref pDXGISurface);
Marshal.Release(pDXGISurfacePtr);
}
return hr;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PAINT)
{
HRESULT hr = OnPaintProc(m.HWnd);
m.Result = (IntPtr)hr;
}
else if (m.Msg == WM_SIZE)
{
HRESULT hr = OnResizeProc(ref m);
m.Result = IntPtr.Zero;
}
else if (m.Msg == WM_DESTROY)
{
//m_bRunning = false;
StopRenderLoop();
Clean();
}
else
{
base.WndProc(ref m);
}
}
public HRESULT OnPaintProc(IntPtr hWnd)
{
HRESULT hr = HRESULT.S_OK;
PAINTSTRUCT ps = new PAINTSTRUCT();
if (BeginPaint(hWnd, ref ps) != IntPtr.Zero)
{
if (m_pD2DContext != null)
{
m_pD2DContext.BeginDraw();
m_pD2DContext.GetSize(out D2D1_SIZE_F size);
m_pD2DContext.Clear(new ColorF(m_BackColor));
var pTransformIdentity = Matrix3x2F.Identity();
m_pD2DContext.SetTransform(pTransformIdentity);
// Background fill
if (m_pLinearGradientBrush != null)
{
var rect = new D2D1_RECT_F(0, 0, size.width, size.height);
m_pD2DContext.FillRectangle(rect, m_pLinearGradientBrush);
}
else if (m_pBackBitmapBrush != null)
{
var rect = new D2D1_RECT_F(0, 0, size.width, size.height);
m_pD2DContext.FillRectangle(rect, m_pBackBitmapBrush);
}
DWRITE_TEXT_METRICS tm = new DWRITE_TEXT_METRICS();
hr = m_pTextLayout.GetMetrics(out tm);
DWRITE_OVERHANG_METRICS overhang = new DWRITE_OVERHANG_METRICS();
hr = m_pTextLayout.GetOverhangMetrics(out overhang);
uint nDpi = GetDpiForWindow(this.Handle);
D2D1_SIZE_F padding = new D2D1_SIZE_F(96.0f / nDpi, 96.0f / nDpi);
Direct2D.D2D1_POINT_2F offset = new Direct2D.D2D1_POINT_2F(
(float)Math.Ceiling(overhang.left + padding.width),
(float)Math.Ceiling(overhang.top + padding.height)
);
D2D1_SIZE_F maskSize = new D2D1_SIZE_F(
overhang.right + padding.width + offset.x + m_pTextLayout.GetMaxWidth(),
overhang.bottom + padding.height + offset.y + m_pTextLayout.GetMaxHeight()
);
Direct2D.D2D1_SIZE_U maskPixelSize = new Direct2D.D2D1_SIZE_U(
(uint)Math.Abs(Math.Ceiling(maskSize.width * nDpi / 96.0f)),
(uint)Math.Abs(Math.Ceiling(maskSize.height * nDpi / 96.0f))
);
// Translation transform
D2D1_MATRIX_3X2_F transform;
Direct2D.D2D1_POINT_2F pt;
if (m_nOrientation == 0)
{
float nY = (size.height - tm.height + offset.y) / 2.0f;
if (maskPixelSize.height > tm.height) nY += (offset.y) / 2.0F;
transform = Matrix3x2F.Translation(m_nX, 1);
pt = new Direct2D.D2D1_POINT_2F(offset.x, nY);
}
else
{
float nX = (size.width - tm.width) / 2 + offset.x;
transform = Matrix3x2F.Translation(1, m_nY);
pt = new Direct2D.D2D1_POINT_2F(nX, offset.y);
}
m_pD2DContext.SetTransform(transform);
if (m_bShadow)
{
if (m_pShadowEffect == null)
{
ID2D1BitmapRenderTarget pCompatibleRenderTarget = null;
Direct2D.D2D1_SIZE_U sizeU = new Direct2D.D2D1_SIZE_U() { width = (uint)size.width, height = (uint)size.height };
D2D1_PIXEL_FORMAT pf = new D2D1_PIXEL_FORMAT()
{
format = DXGI_FORMAT.DXGI_FORMAT_UNKNOWN,
alphaMode = D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED
};
hr = m_pD2DContext.CreateCompatibleRenderTarget(
size,
sizeU,
pf,
D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS.D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE,
out pCompatibleRenderTarget);
if (hr == HRESULT.S_OK)
{
pCompatibleRenderTarget.BeginDraw();
pCompatibleRenderTarget.Clear(null);
// Draw the text layout onto the compatible render target
pCompatibleRenderTarget.DrawTextLayout(
pt,
m_pTextLayout,
m_pMainBrush,
D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NO_SNAP |
D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT);
hr = pCompatibleRenderTarget.EndDraw(out ulong tag1Compatible, out ulong tag2Compatible);
ID2D1Bitmap pCompatibleBitmap = null;
hr = pCompatibleRenderTarget.GetBitmap(out pCompatibleBitmap);
hr = m_pD2DContext.CreateEffect(CLSID_D2D1Shadow, out m_pShadowEffect);
if (hr == HRESULT.S_OK && m_pShadowEffect != null)
m_pShadowEffect.SetInput(0, pCompatibleBitmap);
SafeRelease(ref pCompatibleBitmap);
SafeRelease(ref pCompatibleRenderTarget);
}
}
Direct2D.D2D1_POINT_2F ptShadow = new Direct2D.D2D1_POINT_2F() { x = 3, y = 3 };
D2D1_RECT_F sourceRectangle = new D2D1_RECT_F() { left = 0, top = 0, right = size.width, bottom = size.height };
m_pD2DContext.DrawImage((ID2D1Image)m_pShadowEffect, ptShadow, sourceRectangle,
D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR,
D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
}
m_pD2DContext.DrawTextLayout(pt, m_pTextLayout, m_pMainBrush,
D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NO_SNAP |
D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT);
if (m_bFade)
{
m_pD2DContext.SetTransform(pTransformIdentity);
ID2D1LinearGradientBrush pLinearGradientBrush = null;
// build first gradient stops (gradientColor1 -> transparent)
var gs = new D2D1_GRADIENT_STOP[2];
gs[0].color = new ColorF(m_pGradientColor1);
gs[0].position = 0.0f;
var c2 = new ColorF(m_pGradientColor1);
gs[1].color = new ColorF(c2.r, c2.g, c2.b, 0.0f);
gs[1].position = 1.0f;
ID2D1GradientStopCollection pGSC = null;
hr = m_pD2DContext.CreateGradientStopCollection(gs, 2, D2D1_GAMMA.D2D1_GAMMA_2_2, D2D1_EXTEND_MODE.D2D1_EXTEND_MODE_CLAMP, out pGSC);
if (hr == HRESULT.S_OK && pGSC != null)
{
var props = new D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES();
if (m_nOrientation == 0)
{
props.startPoint = new Direct2D.D2D1_POINT_2F(0, 0);
props.endPoint = new Direct2D.D2D1_POINT_2F(size.width / 5.0f, 0);
}
else
{
props.startPoint = new Direct2D.D2D1_POINT_2F(0, 0);
props.endPoint = new Direct2D.D2D1_POINT_2F(0, size.height / 5.0f);
}
hr = m_pD2DContext.CreateLinearGradientBrush(ref props, IntPtr.Zero, pGSC, out pLinearGradientBrush);
if (hr == HRESULT.S_OK && pLinearGradientBrush != null)
{
var rect = new D2D1_RECT_F(0, 0, size.width, size.height);
m_pD2DContext.FillRectangle(rect, pLinearGradientBrush);
SafeRelease(ref pLinearGradientBrush);
}
SafeRelease(ref pGSC);
}
// Now build the other fade gradient (transparent -> gradientColor2)
var c1 = new ColorF(m_pGradientColor2);
gs[0].color = new ColorF(c1.r, c1.g, c1.b, 0.0f);
gs[0].position = 0.0f;
gs[1].color = new ColorF(m_pGradientColor2);
gs[1].position = 1.0f;
pGSC = null;
hr = m_pD2DContext.CreateGradientStopCollection(gs, 2,
D2D1_GAMMA.D2D1_GAMMA_2_2,
D2D1_EXTEND_MODE.D2D1_EXTEND_MODE_CLAMP,
out pGSC);
if (hr == HRESULT.S_OK && pGSC != null)
{
var props2 = new D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES();
if (m_nOrientation == 0)
{
props2.startPoint = new Direct2D.D2D1_POINT_2F(size.width - size.width / 5.0f, 0);
props2.endPoint = new Direct2D.D2D1_POINT_2F(size.width, 0);
}
else
{
props2.startPoint = new Direct2D.D2D1_POINT_2F(0, size.height - size.height / 5.0f);
props2.endPoint = new Direct2D.D2D1_POINT_2F(0, size.height);
}
hr = m_pD2DContext.CreateLinearGradientBrush(ref props2, IntPtr.Zero, pGSC, out pLinearGradientBrush);
if (hr == HRESULT.S_OK && pLinearGradientBrush != null)
{
// fill the edge rect corresponding to this second gradient
if (m_nOrientation == 0)
{
var rect = new Direct2D.D2D1_RECT_F(size.width - size.width / 5.0f, 0, size.width, size.height);
m_pD2DContext.FillRectangle(rect, pLinearGradientBrush);
}
else
{
var rect = new D2D1_RECT_F(0, size.height - size.height / 5.0f, size.width, size.height);
m_pD2DContext.FillRectangle(rect, pLinearGradientBrush);
}
SafeRelease(ref pLinearGradientBrush);
}
SafeRelease(ref pGSC);
}
}
// Scroll animation
if (m_nOrientation == 0)
{
m_nX -= m_nSpeed;
if (m_nX + maskPixelSize.width <= 0)
m_nX = size.width;
}
else
{
m_nY -= m_nSpeed;
if (m_nY + maskPixelSize.height <= 0)
m_nY = size.height;
}
ulong tag1, tag2;
hr = m_pD2DContext.EndDraw(out tag1, out tag2);
if ((uint)hr == D2DERR_RECREATE_TARGET)
{
m_pD2DContext.SetTarget(null);
SafeRelease(ref m_pD2DContext);
hr = CreateD3D11Device();
hr = CreateDeviceResources();
hr = CreateSwapChain(this.Handle);
hr = ConfigureSwapChain();
}
hr = m_pDXGISwapChain1.Present(1, 0);
}
EndPaint(hWnd, ref ps);
}
return hr;
}
public HRESULT OnResizeProc(ref Message m)
{
HRESULT hr = HRESULT.S_OK;
if (m_pDXGISwapChain1 != null)
{
m_pD2DContext?.SetTarget(null);
SafeRelease(ref m_pD2DTargetBitmap);
hr = m_pDXGISwapChain1.ResizeBuffers(
2, 0, 0,
DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, 0);
ConfigureSwapChain();
}
return hr;
}
public HRESULT CreateFormatAndLayout()
{
HRESULT hr = HRESULT.S_OK;
if (m_pTextFormat == null)
{
var weight = m_bBold
? DWRITE_FONT_WEIGHT.DWRITE_FONT_WEIGHT_BOLD
: DWRITE_FONT_WEIGHT.DWRITE_FONT_WEIGHT_NORMAL;
var style = m_bItalic
? DWRITE_FONT_STYLE.DWRITE_FONT_STYLE_ITALIC
: DWRITE_FONT_STYLE.DWRITE_FONT_STYLE_NORMAL;
hr = m_pDWriteFactory.CreateTextFormat(
m_sFont,
null,
weight,
style,
DWRITE_FONT_STRETCH.DWRITE_FONT_STRETCH_NORMAL,
m_nFontsize,
"",
out m_pTextFormat);
if (hr == HRESULT.S_OK)
{
// hr = m_pTextFormat.SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT.DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
hr = m_pTextFormat.SetTextAlignment(DWRITE_TEXT_ALIGNMENT.DWRITE_TEXT_ALIGNMENT_CENTER);
hr = m_pDWriteFactory.CreateTextLayout(
m_sText,
m_sText.Length,
m_pTextFormat,
m_nWidth,
m_nHeight,
out m_pTextLayout);
if (hr == HRESULT.S_OK)
{
IDWriteTypography pTypography = null;
hr = m_pDWriteFactory.CreateTypography(out pTypography);
if (hr == HRESULT.S_OK)
{
var ff = new DWRITE_FONT_FEATURE
{
nameTag = DWRITE_FONT_FEATURE_TAG.DWRITE_FONT_FEATURE_TAG_STYLISTIC_SET_7,
parameter = 1
};
hr = pTypography.AddFontFeature(ff);
if (hr == HRESULT.S_OK)
{
var tr = new DWRITE_TEXT_RANGE
{
startPosition = 0,
length = (uint)m_sText.Length
};
m_pTextLayout.SetTypography(pTypography, tr);
}
SafeRelease(ref pTypography);
}
}
}
}
return hr;
}
public HRESULT SetGradientBackground(ColorF.Enum pGradientColor1, ColorF.Enum pGradientColor2)
{
HRESULT hr = HRESULT.S_OK;
var gs = new D2D1_GRADIENT_STOP[2];
m_pGradientColor1 = pGradientColor1;
m_pGradientColor2 = pGradientColor2;
gs[0].color = new ColorF(m_pGradientColor1);
gs[0].position = 0.0f;
gs[1].color = new ColorF(m_pGradientColor2);
gs[1].position = 1.0f;
ID2D1GradientStopCollection pGSC = null;
hr = m_pD2DContext.CreateGradientStopCollection(
gs,
(uint)gs.Length,
D2D1_GAMMA.D2D1_GAMMA_2_2,
D2D1_EXTEND_MODE.D2D1_EXTEND_MODE_CLAMP,
out pGSC);
if (hr == HRESULT.S_OK)
{
var props = new D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES
{
startPoint = new Direct2D.D2D1_POINT_2F(0, 0),
endPoint = m_nOrientation == 0
? new Direct2D.D2D1_POINT_2F(Size.Width, Size.Height)
: new Direct2D.D2D1_POINT_2F(0, Size.Height)
};
hr = m_pD2DContext.CreateLinearGradientBrush(props, IntPtr.Zero, pGSC, out m_pLinearGradientBrush);
SafeRelease(ref pGSC);
}
return hr;
}
public HRESULT SetBitmapBackground(System.IO.MemoryStream ms)
{
HRESULT hr = HRESULT.S_OK;
byte[] byteArray = ms.ToArray();
IWICStream wicStream = null;
hr = m_pWICImagingFactory.CreateStream(out wicStream);
if (hr == HRESULT.S_OK)
{
hr = wicStream.InitializeFromMemory(byteArray, byteArray.Length);
if (hr == HRESULT.S_OK)
{
IWICBitmapDecoder pDecoder = null;
hr = m_pWICImagingFactory.CreateDecoderFromStream(
wicStream,
Guid.Empty,
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
out pDecoder);
if (hr == HRESULT.S_OK)
{
IWICBitmapFrameDecode pFrame = null;
hr = pDecoder.GetFrame(0, out pFrame);
if (hr == HRESULT.S_OK)
{
IWICFormatConverter pConvertedSourceBitmap = null;
hr = m_pWICImagingFactory.CreateFormatConverter(out pConvertedSourceBitmap);
if (hr == HRESULT.S_OK)
{
hr = pConvertedSourceBitmap.Initialize(
pFrame,
WICTools.GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherType.WICBitmapDitherTypeNone,
null,
0,
WICBitmapPaletteType.WICBitmapPaletteTypeCustom);
if (hr == HRESULT.S_OK)
{
var bp = new D2D1_BITMAP_PROPERTIES();
ID2D1Bitmap pBitmap = null;
hr = m_pD2DContext.CreateBitmapFromWicBitmap(pConvertedSourceBitmap, bp, out pBitmap);
if (hr == HRESULT.S_OK)
{
var bbp = new D2D1_BITMAP_BRUSH_PROPERTIES1
{
extendModeX = D2D1_EXTEND_MODE.D2D1_EXTEND_MODE_WRAP,
extendModeY = D2D1_EXTEND_MODE.D2D1_EXTEND_MODE_WRAP,
interpolationMode = D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR
};
hr = m_pD2DContext.CreateBitmapBrush(pBitmap, ref bbp, BrushProperties(), out m_pBackBitmapBrush);
SafeRelease(ref pBitmap);
}
}
SafeRelease(ref pConvertedSourceBitmap);
}
SafeRelease(ref pFrame);
}
SafeRelease(ref pDecoder);
}
}
SafeRelease(ref wicStream);
}
return hr;
}
public HRESULT AddImage(System.IO.MemoryStream ms, int nWidth, int nHeight)
{
HRESULT hr = HRESULT.S_OK;
var pInlineImage = new CInlineImage(m_pWICImagingFactory, m_pD2DContext, ms, nWidth, nHeight);
var tr = new DWRITE_TEXT_RANGE { startPosition = 0, length = 1 };
hr = m_pTextLayout.SetInlineObject(pInlineImage, tr);
return hr;
}
public HRESULT CreateDeviceResources()
{
HRESULT hr = HRESULT.S_OK;
if (m_pMainBrush == null)
m_pD2DContext.CreateSolidColorBrush(new ColorF(m_TextColor), BrushProperties(), out m_pMainBrush);
hr = CreateFormatAndLayout();
return hr;
}
private void CleanDeviceResources()
{
SafeRelease(ref m_pMainBrush);
SafeRelease(ref m_pLinearGradientBrush);
SafeRelease(ref m_pBackBitmapBrush);
}
private void Clean()
{
SafeRelease(ref m_pTextFormat);
SafeRelease(ref m_pTextLayout);
CleanDeviceResources();
SafeRelease(ref m_pD2DContext);
SafeRelease(ref m_pD2DTargetBitmap);
SafeRelease(ref m_pDXGISwapChain1);
SafeRelease(ref m_pDXGIDevice);
SafeRelease(ref m_pWICImagingFactory);
SafeRelease(ref m_pDWriteFactory);
SafeRelease(ref m_pD2DFactory1);
}
}
// To be improved...
public class CInlineImage : IDWriteInlineObject
{
private ID2D1DeviceContext m_pD2DContext = null;
private ID2D1Bitmap1 m_pD2DBitmap = null;
private int m_nWidth = 0;
private int m_nHeight = 0;
public CInlineImage(IWICImagingFactory pWICImagingFactory, ID2D1DeviceContext pD2DContext, System.IO.MemoryStream ms, int nWidth, int nHeight)
{
HRESULT hr = HRESULT.S_OK;
m_pD2DContext = pD2DContext;
m_nWidth = nWidth;
m_nHeight = nHeight;
byte[] byteArray = ms.ToArray();
IWICStream wicStream = null;
hr = pWICImagingFactory.CreateStream(out wicStream);
if (hr == HRESULT.S_OK)
{
hr = wicStream.InitializeFromMemory(byteArray, byteArray.Length);
if (hr == HRESULT.S_OK)
{
IWICBitmapDecoder pDecoder = null;
hr = pWICImagingFactory.CreateDecoderFromStream(wicStream, Guid.Empty, WICDecodeOptions.WICDecodeMetadataCacheOnDemand, out pDecoder);
if (hr == HRESULT.S_OK)
{
IWICBitmapFrameDecode pFrame = null;
hr = pDecoder.GetFrame(0, out pFrame);
if (hr == HRESULT.S_OK)
{
IWICFormatConverter pConvertedSourceBitmap = null;
hr = pWICImagingFactory.CreateFormatConverter(out pConvertedSourceBitmap);
if (hr == HRESULT.S_OK)
{
hr = pConvertedSourceBitmap.Initialize(
pFrame,
WICTools.GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherType.WICBitmapDitherTypeNone,
null,
0,
WICBitmapPaletteType.WICBitmapPaletteTypeCustom);
if (hr == HRESULT.S_OK)