-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcontext.cpp
More file actions
152 lines (124 loc) · 3.67 KB
/
dcontext.cpp
File metadata and controls
152 lines (124 loc) · 3.67 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
#include "dcontext.hpp"
DContext::DContext(HWND hwnd, GeneralError* external_err_ptr)
: GeneralErrorHandler{ GDI, external_err_ptr }, hwnd{ hwnd },
ps{}, hdc{ BeginPaint(hwnd, &ps) }, brush{ hdc, external_err_ptr},
pen{ hdc, external_err_ptr }, font{ hdc, external_err_ptr }
{
if (hdc == NULL) Throw(2010);
}
DContext::~DContext()
{
brush.CleanUp(false, 2020);
pen.CleanUp(false, 2030);
font.CleanUp(false, 2040);
if (hdc != NULL)
{
EndPaint(hwnd, &ps);
}
}
DContext::operator HDC() const { return hdc; }
DContext::operator HBRUSH() const { return brush; }
DContext::operator HPEN() const { return pen; }
DContext::operator HFONT() const { return font; }
void DContext::Select(HBRUSH hbr, bool fCanDestroy)
{
brush.Select(HGDIOBJ(hbr), fCanDestroy);
}
void DContext::Select(HPEN hpen, bool fCanDestroy)
{
pen.Select(HGDIOBJ(hpen), fCanDestroy);
}
void DContext::Select(HFONT hfont, bool fCanDestroy)
{
font.Select(HGDIOBJ(hfont), fCanDestroy);
}
void DContext::MoveTo(int iX, int iY)
{
if (::MoveToEx(*this, iX, iY, NULL) == 0) Throw(2050);
}
void DContext::LineTo(int iX, int iY)
{
if (::LineTo(*this, iX, iY) == 0) Throw(2060);
}
void DContext::Polygon(const POINT* lpPoints, int iCount)
{
if (::Polygon(*this, lpPoints, iCount) == 0) Throw(2070);
}
void DContext::Ellipse(int iLeft, int iTop, int iRight, int iBottom)
{
if (::Ellipse(*this, iLeft, iTop, iRight, iBottom) == 0) Throw(2080);
}
void DContext::FillRect(const RECT* lprc)
{
if (::FillRect(*this, lprc, *this) == 0) Throw(2090);
}
/*
void DContext::Arc(
int nLeftRect, int nTopRect, int nRightRect, int nBottomRect,
int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc)
{
if (::Arc(
*this, nLeftRect, nTopRect, nRightRect, nBottomRect,
nXStartArc, nYStartArc, nXEndArc, nYEndArc) == 0)
Throw(2100);
}
void DContext::AngleArc(
int iX, int iY, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
{
if (::AngleArc(
*this, iX, iY, dwRadius,
eStartAngle, eSweepAngle) == 0) Throw(2110);
} */
void DContext::TextOut(int nXStart, int nYStart, LPCWSTR lpString, int cbString)
{
if (::TextOutW(*this, nXStart, nYStart, lpString, cbString) == 0) Throw(2120);
}
void DContext::SetTextAlign(UINT fMode)
{
if (::SetTextAlign(*this, fMode) == GDI_ERROR) Throw(2130);
}
/*
void DContext::SetBkMode(int iBkMode)
{
if (::SetBkMode(*this, iBkMode) == 0) Throw(2140);
} */
void DContext::SetTextColor(COLORREF crColor)
{
if (::SetTextColor(*this, crColor) == CLR_INVALID) Throw(2150);
}
DContext::GDIobj::GDIobj(HDC hdc, GeneralError* external_err_ptr)
: GeneralErrorHandler{ GDI, external_err_ptr },
hObj{ NULL }, hOldObj{ NULL }, hdc{ hdc }, fCanDestroy{}
{
}
DContext::GDIobj::operator HBRUSH() const { return HBRUSH(hObj); }
DContext::GDIobj::operator HPEN() const { return HPEN(hObj); }
DContext::GDIobj::operator HFONT() const { return HFONT(hObj); }
void DContext::GDIobj::CleanUp(bool fCanThrow, unsigned where) // can be called from a destructor
{
if (hObj != NULL)
{
auto
fSelObjFailed{ SelectObject(hdc, hOldObj) == NULL },
fDelObjFailed{ false };
if (fCanDestroy) if (DeleteObject(hObj) == 0) fDelObjFailed = true;
hOldObj = hObj = NULL;
if (fSelObjFailed) fCanThrow ? Throw(where) : Post(where);
if (fDelObjFailed) fCanThrow ? Throw(where + 1) : Post(where + 1);
}
}
void DContext::GDIobj::Select(HGDIOBJ hObj, bool fCanDestroy)
{
if (hObj == NULL) Throw(2160);
else
{
CleanUp(true, 2170);
hOldObj = SelectObject(hdc, hObj);
if (hOldObj == NULL) Throw(2180);
else
{
this->hObj = hObj;
this->fCanDestroy = fCanDestroy;
}
}
}