| 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), | |
| 23 hdc_(GetDC(hwnd)) { | |
| 24 DCHECK(!hwnd_ || IsWindow(hwnd_)); | |
| 25 DCHECK(hdc_); | |
| 26 } | |
| 27 | 45 |
| 28 ~ScopedGetDC() { | 46 virtual void DisposeDC(HDC hdc) = 0; |
| 29 if (hdc_) | |
| 30 ReleaseDC(hwnd_, hdc_); | |
| 31 } | |
| 32 | 47 |
| 33 operator HDC() { return hdc_; } | 48 HDC get() { 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); |
| 34 | 60 |
| 35 private: | 61 private: |
| 62 void ResetObjects(); |
| 63 void Select(HGDIOBJ object, HGDIOBJ* holder); |
| 64 |
| 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 |
| 36 HWND hwnd_; | 82 HWND hwnd_; |
| 37 HDC hdc_; | |
| 38 | |
| 39 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); | 83 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); |
| 40 }; | 84 }; |
| 41 | 85 |
| 42 // Like ScopedHandle but for HDC. Only use this on HDCs returned from | 86 // Like ScopedHandle but for HDC. Only use this on HDCs returned from |
| 43 // CreateCompatibleDC, CreateDC and CreateIC. | 87 // CreateCompatibleDC, CreateDC and CreateIC. |
| 44 class ScopedCreateDC { | 88 class ScopedCreateDC : public ScopedDC { |
| 45 public: | 89 public: |
| 46 ScopedCreateDC() : hdc_(NULL) { } | 90 ScopedCreateDC(); |
| 47 explicit ScopedCreateDC(HDC h) : hdc_(h) { } | 91 explicit ScopedCreateDC(HDC hdc); |
| 48 | 92 virtual ~ScopedCreateDC(); |
| 49 ~ScopedCreateDC() { | 93 void Set(HDC hdc); |
| 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_; } | |
| 63 | 94 |
| 64 private: | 95 private: |
| 65 void Close() { | 96 virtual void DisposeDC(HDC hdc) OVERRIDE; |
| 66 #ifdef NOGDI | |
| 67 assert(false); | |
| 68 #else | |
| 69 if (hdc_) | |
| 70 DeleteDC(hdc_); | |
| 71 #endif // NOGDI | |
| 72 } | |
| 73 | |
| 74 HDC hdc_; | |
| 75 | 97 |
| 76 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC); | 98 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC); |
| 77 }; | 99 }; |
| 78 | 100 |
| 79 } // namespace win | 101 } // namespace win |
| 80 } // namespace base | 102 } // namespace base |
| 81 | 103 |
| 82 #endif // BASE_WIN_SCOPED_HDC_H_ | 104 #endif // BASE_WIN_SCOPED_HDC_H_ |
| OLD | NEW |