-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
132 lines (114 loc) · 5.17 KB
/
Form1.cs
File metadata and controls
132 lines (114 loc) · 5.17 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Direct2D;
using DWrite;
using static DWrite.DWriteTools;
using WIC;
using GlobalStructures;
using static GlobalStructures.GlobalTools;
using System.Runtime.InteropServices;
namespace CSharp_ScrollingTextControl
{
public partial class Form1 : Form
{
private ID2D1Factory m_pD2DFactory = null;
private ID2D1Factory1 m_pD2DFactory1 = null;
private IDWriteFactory m_pDWriteFactory = null;
private IWICImagingFactory m_pWICImagingFactory = null;
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
this.FormClosing += Form1_FormClosing;
}
private void Form1_Load(object sender, EventArgs e)
{
HRESULT hr = HRESULT.S_OK;
this.DoubleBuffered = true;
this.ClientSize = new System.Drawing.Size(660, 664);
this.Text = "C# - Direct2D - Scrolling Text";
hr = CreateD2D1Factory();
if (SUCCEEDED(hr))
{
hr = CreateDWriteFactory();
if (SUCCEEDED(hr))
{
m_pWICImagingFactory = (IWICImagingFactory)Activator.CreateInstance(Type.GetTypeFromCLSID(WICTools.CLSID_WICImagingFactory));
// Initialize scrolling text controls
scrollingTextControl1.Initialize(m_pD2DFactory1, m_pDWriteFactory, m_pWICImagingFactory,
"This is a text with shadow", "Times New Roman", 35, 600, 500, 0, false, false, true);
scrollingTextControl1.TextColor = ColorF.Enum.Blue;
scrollingTextControl1.SetGradientBackground(ColorF.Enum.Yellow, ColorF.Enum.Red);
scrollingTextControl1.Speed = 1.5f;
scrollingTextControl2.Initialize(m_pD2DFactory1, m_pDWriteFactory, m_pWICImagingFactory,
"Text with Gabriola font and fading, speed = 0.5", "Gabriola", 48, 600, 500, 0, false, false, false, true);
scrollingTextControl2.SetGradientBackground(ColorF.Enum.Magenta, ColorF.Enum.DarkBlue);
scrollingTextControl2.TextColor = ColorF.Enum.Yellow;
scrollingTextControl2.Speed = 0.5f;
scrollingTextControl3.Initialize(m_pD2DFactory1, m_pDWriteFactory, m_pWICImagingFactory,
"This is a bold text, speed = 3", "Cooper Std", 35, 600, 500, 0, true);
scrollingTextControl3.SetGradientBackground(ColorF.Enum.Lime, ColorF.Enum.Orange);
scrollingTextControl3.TextColor = ColorF.Enum.Green;
scrollingTextControl3.Speed = 3.0f;
Image imgBackground = (Image)Properties.Resources.Flowers_Background;
using (System.IO.MemoryStream msBackground = new System.IO.MemoryStream())
{
imgBackground.Save(msBackground, imgBackground.RawFormat);
scrollingTextControl4.Initialize(m_pD2DFactory1, m_pDWriteFactory, m_pWICImagingFactory,
char.ConvertFromUtf32(0x1F496) + "Shadow Segoe UI Emoji" + char.ConvertFromUtf32(0x1F499) + "Vertical scrolling",
"Segoe UI Emoji", 40, 600, 500, 1, false, false, true);
scrollingTextControl4.SetBitmapBackground(msBackground);
}
}
}
this.CenterToScreen();
}
public HRESULT CreateD2D1Factory()
{
HRESULT hr = HRESULT.S_OK;
D2D1_FACTORY_OPTIONS options = new D2D1_FACTORY_OPTIONS();
#if DEBUG
options.debugLevel = D2D1_DEBUG_LEVEL.D2D1_DEBUG_LEVEL_INFORMATION;
#endif
hr = D2DTools.D2D1CreateFactory(
D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED,
ref D2DTools.CLSID_D2D1Factory,
ref options,
out m_pD2DFactory);
m_pD2DFactory1 = (ID2D1Factory1)m_pD2DFactory;
return hr;
}
public HRESULT CreateDWriteFactory()
{
HRESULT hr = HRESULT.S_OK;
IntPtr pDWriteFactoryPtr = IntPtr.Zero;
hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED, ref CLSID_DWriteFactory, out pDWriteFactoryPtr);
if (SUCCEEDED(hr))
{
m_pDWriteFactory = Marshal.GetObjectForIUnknown(pDWriteFactoryPtr) as IDWriteFactory;
if (pDWriteFactoryPtr != IntPtr.Zero)
Marshal.Release(pDWriteFactoryPtr);
}
return hr;
}
private void Clean()
{
SafeRelease(ref m_pDWriteFactory);
SafeRelease(ref m_pD2DFactory1);
SafeRelease(ref m_pD2DFactory);
SafeRelease(ref m_pWICImagingFactory);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Clean();
}
}
}