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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/win/scoped_hdc.h"
#include "base/logging.h"
namespace base {
namespace win {
ScopedDC::ScopedDC(HDC hdc)
: hdc_(hdc),
bitmap_(0),
font_(0),
brush_(0),
pen_(0),
region_(0) {
}
ScopedDC::~ScopedDC() {}
void ScopedDC::SelectBitmap(HBITMAP bitmap) {
Select(bitmap, &bitmap_);
}
void ScopedDC::SelectFont(HFONT font) {
Select(font, &font_);
}
void ScopedDC::SelectBrush(HBRUSH brush) {
Select(brush, &brush_);
}
void ScopedDC::SelectPen(HPEN pen) {
Select(pen, &pen_);
}
void ScopedDC::SelectRegion(HRGN region) {
Select(region, ®ion_);
}
void ScopedDC::Close() {
if (!hdc_)
return;
ResetObjects();
DisposeDC(hdc_);
}
void ScopedDC::Reset(HDC hdc) {
Close();
hdc_ = hdc;
}
void ScopedDC::ResetObjects() {
if (bitmap_) {
SelectObject(hdc_, bitmap_);
bitmap_ = 0;
}
if (font_) {
SelectObject(hdc_, font_);
font_ = 0;
}
if (brush_) {
SelectObject(hdc_, brush_);
brush_ = 0;
}
if (pen_) {
SelectObject(hdc_, pen_);
pen_ = 0;
}
if (region_) {
SelectObject(hdc_, region_);
region_ = 0;
}
}
void ScopedDC::Select(HGDIOBJ object, HGDIOBJ* holder) {
HGDIOBJ old = SelectObject(hdc_, object);
DCHECK(old);
// We only want to store the first |old| object.
if (!*holder)
*holder = old;
}
ScopedGetDC::ScopedGetDC(HWND hwnd) : ScopedDC(GetDC(hwnd)), hwnd_(hwnd) {
}
ScopedGetDC::~ScopedGetDC() {
Close();
}
void ScopedGetDC::DisposeDC(HDC hdc) {
ReleaseDC(hwnd_, hdc);
}
ScopedCreateDC::ScopedCreateDC() : ScopedDC(0) {
}
ScopedCreateDC::ScopedCreateDC(HDC hdc) : ScopedDC(hdc) {
}
ScopedCreateDC::~ScopedCreateDC() {
Close();
}
void ScopedCreateDC::Set(HDC hdc) {
Reset(hdc);
}
void ScopedCreateDC::DisposeDC(HDC hdc) {
DeleteDC(hdc);
}
} // namespace win
} // namespace base
|