-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDrawPanel.h
157 lines (133 loc) · 3.11 KB
/
CDrawPanel.h
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
#pragma once
#include "pch.h"
#include "CDPDefine.h"
struct DpStr
{
BOOL bRefStr;
union
{
eck::CRefStrW rs;
CDPSTR str;
BYTE byDummy[std::max(sizeof(rs), sizeof(str))];
};
DpStr() :bRefStr{ FALSE }, byDummy{}
{
}
~DpStr()
{
if (bRefStr)
rs.~CRefStrT();
else
if (str.cchAlloc && str.psz)
eck::TRefStrDefAlloc<WCHAR>{}.deallocate(str.psz, str.cchAlloc);
}
ECK_DISABLE_COPY_MOVE(DpStr);
EckInline void From(eck::CRefStrW& rs)
{
if (bRefStr)
rs = std::move(rs);
else
str.psz = rs.Detach(str.cchAlloc, str.cch);
}
EckInline void From(const CDPSTR& s)
{
if (bRefStr)
rs.Attach(s.psz, s.cchAlloc, s.cch);
else
str = s;
}
EckInline void Clear()
{
if (bRefStr)
rs.Clear();
else
{
if (str.cchAlloc && str.psz)
eck::TRefStrDefAlloc<WCHAR>{}.deallocate(str.psz, str.cchAlloc);
str = {};
}
}
};
class CDrawPanel
{
private:
GpGraphics* m_pGraphics{};
eck::CEzCDC m_DC{};
std::vector<GpBrush*> m_Brushes{};
std::vector<GpPen*> m_Pens{};
std::vector<GpFont*> m_Fonts{};
size_t m_idxCurrBrush{ (size_t)-1 };
size_t m_idxCurrPen{ (size_t)-1 };
size_t m_idxCurrFont{ (size_t)-1 };
int m_cx{}, m_cy{};
BITMAP m_Bmp{};
void ResetBitmapDpi(GpBitmap* pBitmap)
{
REAL xDpi, yDpi;
GdipGetDpiX(m_pGraphics, &xDpi);
GdipGetDpiY(m_pGraphics, &yDpi);
GdipBitmapSetResolution(pBitmap, xDpi, yDpi);
}
EckInline constexpr DWORD& Pixel(int x, int y)
{
return *(DWORD*)((BYTE*)m_Bmp.bmBits + m_Bmp.bmWidthBytes * y + x);
}
public:
ECK_DISABLE_COPY_MOVE_DEF_CONS(CDrawPanel)
public:
~CDrawPanel()
{
UnInit();
}
BOOL Init(int cx, int cy);
BOOL UnInit();
BOOL ReSize(int cx, int cy);
int ExecuteCommand(PCWSTR pszCmd, LPARAM lParam);
EckInline size_t GetObjCount(int i)
{
switch (i)
{
case CDPOT_PEN: return m_Pens.size();
case CDPOT_BRUSH: return m_Brushes.size();
case CDPOT_FONT: return m_Fonts.size();
}
return (size_t)-1;
}
CDPBIN GetImageBin(int i)
{
if (i < CDPIT_BEGIN_ || i >= CDPIT_END_)
return {};
eck::ImageType Type[]{ eck::ImageType::Bmp,eck::ImageType::Png,eck::ImageType::Jpeg };
GpBitmap* pBitmap;
GdipCreateBitmapFromScan0(m_cx, m_cy, m_Bmp.bmWidthBytes, PixelFormat32bppARGB,
(BYTE*)m_Bmp.bmBits, &pBitmap);
auto rb = eck::SaveGpImage(pBitmap, Type[i]);
CDPBIN bin;
bin.pData = rb.Detach(bin.cbAlloc, bin.cb);
return bin;
}
HBITMAP CloneImageToHBITMAP()
{
eck::CDib dib{};
dib.Create(m_cx, -m_cy);
BITMAP bmp;
GetObjectW(dib.GetHBitmap(), sizeof(bmp), &bmp);
memcpy(bmp.bmBits, m_Bmp.bmBits, m_Bmp.bmWidthBytes * m_Bmp.bmHeight);
return dib.Detach();
}
GpBitmap* CloneImageToGpBitmap()
{
GpBitmap* pBitmap{};
GdipCreateBitmapFromScan0(m_cx, m_cy, m_Bmp.bmWidthBytes, PixelFormat32bppARGB,
(BYTE*)m_Bmp.bmBits, &pBitmap);
return pBitmap;
}
int ExecuteCommand(PCWSTR pszCmd, const CDPIMAGE* pImgIn,
CDPSTR& sDisplay, CDPIMAGE* pImgOut, CDPImageMedium eMedium);
};
#define CDPDECLCMD(CmdName) \
constexpr static WCHAR szCmd_##CmdName[]{ \
L"." \
ECKTOSTRW(CmdName) \
L"(" }; \
constexpr static int cchCmd_##CmdName = (int)ARRAYSIZE(szCmd_##CmdName) - 1;