OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_WIN_SCOPED_HDC_H_ | 5 #ifndef BASE_WIN_SCOPED_HDC_H_ |
6 #define BASE_WIN_SCOPED_HDC_H_ | 6 #define BASE_WIN_SCOPED_HDC_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <windows.h> | 9 #include <windows.h> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/logging.h" | 12 #include "base/compiler_specific.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 namespace win { | 15 namespace win { |
16 | 16 |
17 // Like ScopedHandle but for HDC. Only use this on HDCs returned from | 17 // The ScopedGetDC and ScopedCreateDC classes manage the default GDI objects |
18 // GetDC. | 18 // that are initially selected into a DC. They help you avoid the following |
19 class ScopedGetDC { | 19 // common mistake: |
20 // | |
21 // HDC hdc = GetDC(NULL); | |
22 // SelectObject(hdc, new_bitmap); | |
23 // .. drawing code here .. | |
24 // ReleaseDC(hdc); <--- error: the DC has a custom object still selected! | |
25 // | |
26 // This code should be: | |
27 // | |
28 // HDC hdc = GetDC(NULL); | |
29 // HGDIOBJ old_obj = SelectObject(hdc, new_bitmap); | |
30 // .. drawing code here .. | |
31 // SelectObject(hdc, old_obj); | |
32 // ReleaseDC(hdc); <--- ok to release now. | |
33 // | |
34 // But why work so hard? Use our handy classes: | |
35 // | |
36 // ScopedGetDC dc(NULL); | |
37 // dc.SelectBitmap(hdc, new_bitmap); | |
38 // .. drawing here | |
39 // .. when dc goes out of scope it will select the original object before | |
40 // .. being released. | |
41 // | |
42 class ScopedDC { | |
20 public: | 43 public: |
21 explicit ScopedGetDC(HWND hwnd) | 44 virtual ~ScopedDC(); |
22 : hwnd_(hwnd), | 45 |
23 hdc_(GetDC(hwnd)) { | 46 virtual void DisposeDC(HDC hdc) = 0; |
24 DCHECK(!hwnd_ || IsWindow(hwnd_)); | 47 |
25 DCHECK(hdc_); | 48 HDC get() { |
Peter Kasting
2012/02/10 19:47:39
Nit: Whole def'n can all be on one line
| |
49 return hdc_; | |
26 } | 50 } |
27 | 51 |
28 ~ScopedGetDC() { | 52 void SelectBitmap(HBITMAP bitmap); |
29 if (hdc_) | 53 |
Peter Kasting
2012/02/10 19:47:39
Nit: I'd remove the newlines between these
| |
30 ReleaseDC(hwnd_, hdc_); | 54 void SelectFont(HFONT font); |
55 | |
56 void SelectBrush(HBRUSH brush); | |
57 | |
58 void SelectPen(HPEN pen); | |
59 | |
60 void SelectRegion(HRGN region); | |
61 | |
62 protected: | |
63 ScopedDC(HDC hdc); | |
64 | |
65 void Close(); | |
66 | |
67 void Reset(HDC hdc); | |
68 | |
69 private: | |
70 void ResetObjects(); | |
71 | |
72 void Select(HGDIOBJ object, HGDIOBJ* holder); | |
73 | |
74 HDC hdc_; | |
75 HGDIOBJ bitmap_; | |
76 HGDIOBJ font_; | |
77 HGDIOBJ brush_; | |
78 HGDIOBJ pen_; | |
79 HGDIOBJ region_; | |
80 }; | |
Peter Kasting
2012/02/10 19:47:39
Nit: DISALLOW_COPY_AND_ASSIGN?
| |
81 | |
82 // Creates and manages an HDC obtained by GetDC. | |
83 class ScopedGetDC : public ScopedDC { | |
84 public: | |
85 explicit ScopedGetDC(HWND hwnd) : ScopedDC(GetDC(hwnd)), hwnd_(hwnd) { | |
31 } | 86 } |
32 | 87 |
33 operator HDC() { return hdc_; } | 88 virtual ~ScopedGetDC() { |
Peter Kasting
2012/02/10 19:47:39
Nit: Never inline virtual functions or other funct
| |
89 Close(); | |
90 } | |
34 | 91 |
35 private: | 92 private: |
93 virtual void DisposeDC(HDC hdc) OVERRIDE { | |
94 ReleaseDC(hwnd_, hdc); | |
95 } | |
96 | |
36 HWND hwnd_; | 97 HWND hwnd_; |
37 HDC hdc_; | |
38 | |
39 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); | 98 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); |
40 }; | 99 }; |
41 | 100 |
42 // Like ScopedHandle but for HDC. Only use this on HDCs returned from | 101 // Like ScopedHandle but for HDC. Only use this on HDCs returned from |
43 // CreateCompatibleDC, CreateDC and CreateIC. | 102 // CreateCompatibleDC, CreateDC and CreateIC. |
44 class ScopedCreateDC { | 103 class ScopedCreateDC : public ScopedDC { |
45 public: | 104 public: |
46 ScopedCreateDC() : hdc_(NULL) { } | 105 ScopedCreateDC() : ScopedDC(0) { |
47 explicit ScopedCreateDC(HDC h) : hdc_(h) { } | 106 } |
48 | 107 |
49 ~ScopedCreateDC() { | 108 explicit ScopedCreateDC(HDC hdc) : ScopedDC(hdc) { |
109 } | |
110 | |
111 virtual ~ScopedCreateDC() { | |
50 Close(); | 112 Close(); |
51 } | 113 } |
52 | 114 |
53 HDC Get() { | 115 void Set(HDC hdc) { |
54 return hdc_; | 116 Reset(hdc); |
55 } | 117 } |
56 | 118 |
57 void Set(HDC h) { | 119 private: |
58 Close(); | 120 virtual void DisposeDC(HDC hdc) OVERRIDE { |
59 hdc_ = h; | 121 DeleteDC(hdc); |
60 } | 122 } |
61 | 123 |
62 operator HDC() { return hdc_; } | |
63 | |
64 private: | |
65 void Close() { | |
66 #ifdef NOGDI | |
67 assert(false); | |
68 #else | |
69 if (hdc_) | |
70 DeleteDC(hdc_); | |
71 #endif // NOGDI | |
72 } | |
73 | |
74 HDC hdc_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC); | 124 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC); |
77 }; | 125 }; |
78 | 126 |
79 } // namespace win | 127 } // namespace win |
80 } // namespace base | 128 } // namespace base |
81 | 129 |
82 #endif // BASE_WIN_SCOPED_HDC_H_ | 130 #endif // BASE_WIN_SCOPED_HDC_H_ |
OLD | NEW |