Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(877)

Side by Side Diff: base/win/scoped_hdc.h

Issue 9387027: Revert 121840 - Make scoped dc objects smarter (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gypi ('k') | base/win/scoped_hdc.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/compiler_specific.h" 12 #include "base/logging.h"
13 13
14 namespace base { 14 namespace base {
15 namespace win { 15 namespace win {
16 16
17 // The ScopedGetDC and ScopedCreateDC classes manage the default GDI objects 17 // Like ScopedHandle but for HDC. Only use this on HDCs returned from
18 // that are initially selected into a DC. They help you avoid the following 18 // GetDC.
19 // common mistake: 19 class ScopedGetDC {
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 {
43 public: 20 public:
44 virtual ~ScopedDC(); 21 explicit ScopedGetDC(HWND hwnd)
22 : hwnd_(hwnd),
23 hdc_(GetDC(hwnd)) {
24 DCHECK(!hwnd_ || IsWindow(hwnd_));
25 DCHECK(hdc_);
26 }
45 27
46 virtual void DisposeDC(HDC hdc) = 0; 28 ~ScopedGetDC() {
29 if (hdc_)
30 ReleaseDC(hwnd_, hdc_);
31 }
47 32
48 HDC get() { return hdc_; } 33 operator HDC() { return hdc_; }
49
50 void SelectBitmap(HBITMAP bitmap);
51 void SelectFont(HFONT font);
52 void SelectBrush(HBRUSH brush);
53 void SelectPen(HPEN pen);
54 void SelectRegion(HRGN region);
55
56 protected:
57 ScopedDC(HDC hdc);
58 void Close();
59 void Reset(HDC hdc);
60 34
61 private: 35 private:
62 void ResetObjects(); 36 HWND hwnd_;
63 void Select(HGDIOBJ object, HGDIOBJ* holder); 37 HDC hdc_;
64 38
65 HDC hdc_;
66 HGDIOBJ bitmap_;
67 HGDIOBJ font_;
68 HGDIOBJ brush_;
69 HGDIOBJ pen_;
70 HGDIOBJ region_;
71 };
72
73 // Creates and manages an HDC obtained by GetDC.
74 class ScopedGetDC : public ScopedDC {
75 public:
76 explicit ScopedGetDC(HWND hwnd);
77 virtual ~ScopedGetDC();
78
79 private:
80 virtual void DisposeDC(HDC hdc) OVERRIDE;
81
82 HWND hwnd_;
83 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); 39 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC);
84 }; 40 };
85 41
86 // Like ScopedHandle but for HDC. Only use this on HDCs returned from 42 // Like ScopedHandle but for HDC. Only use this on HDCs returned from
87 // CreateCompatibleDC, CreateDC and CreateIC. 43 // CreateCompatibleDC, CreateDC and CreateIC.
88 class ScopedCreateDC : public ScopedDC { 44 class ScopedCreateDC {
89 public: 45 public:
90 ScopedCreateDC(); 46 ScopedCreateDC() : hdc_(NULL) { }
91 explicit ScopedCreateDC(HDC hdc); 47 explicit ScopedCreateDC(HDC h) : hdc_(h) { }
92 virtual ~ScopedCreateDC(); 48
93 void Set(HDC hdc); 49 ~ScopedCreateDC() {
50 Close();
51 }
52
53 HDC Get() {
54 return hdc_;
55 }
56
57 void Set(HDC h) {
58 Close();
59 hdc_ = h;
60 }
61
62 operator HDC() { return hdc_; }
94 63
95 private: 64 private:
96 virtual void DisposeDC(HDC hdc) OVERRIDE; 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_;
97 75
98 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC); 76 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC);
99 }; 77 };
100 78
101 } // namespace win 79 } // namespace win
102 } // namespace base 80 } // namespace base
103 81
104 #endif // BASE_WIN_SCOPED_HDC_H_ 82 #endif // BASE_WIN_SCOPED_HDC_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/win/scoped_hdc.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698