-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDWriteWindow.cpp
More file actions
90 lines (75 loc) · 2.54 KB
/
DWriteWindow.cpp
File metadata and controls
90 lines (75 loc) · 2.54 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
#include "DWriteWindow.h"
#include <dwrite.h>
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")
wil::com_ptr<ID2D1Factory> d2dFactory;
wil::com_ptr<IDWriteFactory> dwriteFactory;
void DWriteWindow::recreateFont()
{
THROW_IF_FAILED(dwriteFactory->CreateTextFormat(
m_fontFamily.data(),
nullptr,
DWRITE_FONT_WEIGHT::DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE::DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH::DWRITE_FONT_STRETCH_NORMAL,
m_fontSize,
L"",
textFormat.put()
));
THROW_IF_FAILED(textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT::DWRITE_TEXT_ALIGNMENT_LEADING));
}
DWriteWindow::DWriteWindow(winrt::hstring const& text) : m_text{text}, BaseWindow{ L"DWrite window" }
{
THROW_IF_FAILED(D2D1CreateFactory(D2D1_FACTORY_TYPE::D2D1_FACTORY_TYPE_SINGLE_THREADED, d2dFactory.put()));
THROW_IF_FAILED(DWriteCreateFactory(DWRITE_FACTORY_TYPE::DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory), dwriteFactory.put_unknown()));
recreateFont();
auto const clientRect = ClientRect();
THROW_IF_FAILED(d2dFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(m_hwnd.get(), D2D1::SizeU(clientRect.right, clientRect.bottom)),
m_renderTarget.put()
));
THROW_IF_FAILED(m_renderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), m_textBrush.put()));
}
void DWriteWindow::OnSize(HWND hwnd, WPARAM wparam, UINT width, UINT height)
{
if (auto renderTarget = getSelfFromHwnd(hwnd)->m_renderTarget)
{
THROW_IF_FAILED(renderTarget->Resize(D2D1::SizeU(width, height)));
}
}
void DWriteWindow::SetText(winrt::hstring const& text)
{
m_text = text;
OnPaint(m_hwnd.get());
}
void DWriteWindow::SetFontSize(int fontSize)
{
m_fontSize = fontSize;
recreateFont();
OnPaint(m_hwnd.get());
}
void DWriteWindow::SetFontFamily(winrt::hstring const& fontFamily)
{
m_fontFamily = fontFamily;
recreateFont();
OnPaint(m_hwnd.get());
}
void DWriteWindow::OnPaint(HWND hwnd)
{
auto self = getSelfFromHwnd(hwnd);
//auto const clientRect = self->ClientRect();
auto renderTargetSize = self->m_renderTarget->GetSize();
self->m_renderTarget->BeginDraw();
self->m_renderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
self->m_renderTarget->DrawTextW(
self->m_text.data(),
self->m_text.size(),
self->textFormat.get(),
//D2D1::RectF(0, 0, clientRect.right, clientRect.bottom),
D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
self->m_textBrush.get()
);
THROW_IF_FAILED(self->m_renderTarget->EndDraw());
THROW_IF_WIN32_BOOL_FALSE(ValidateRect(hwnd, nullptr));
}