1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_WIN_SCOPED_HDC_H_ 6 #define BASE_WIN_SCOPED_HDC_H_ 7 #pragma once 8 9 #include <windows.h> 10 11 #include "base/basictypes.h" 12 13 namespace base { 14 namespace win { 15 16 // Like ScopedHandle but for HDC. Only use this on HDCs returned from 17 // CreateCompatibleDC. For an HDC returned by GetDC, use ReleaseDC instead. 18 class ScopedHDC { 19 public: 20 ScopedHDC() : hdc_(NULL) { } 21 explicit ScopedHDC(HDC h) : hdc_(h) { } 22 23 ~ScopedHDC() { 24 Close(); 25 } 26 27 HDC Get() { 28 return hdc_; 29 } 30 31 void Set(HDC h) { 32 Close(); 33 hdc_ = h; 34 } 35 36 operator HDC() { return hdc_; } 37 38 private: 39 void Close() { 40 #ifdef NOGDI 41 assert(false); 42 #else 43 if (hdc_) 44 DeleteDC(hdc_); 45 #endif // NOGDI 46 } 47 48 HDC hdc_; 49 DISALLOW_COPY_AND_ASSIGN(ScopedHDC); 50 }; 51 52 } // namespace win 53 } // namespace base 54 55 #endif // BASE_WIN_SCOPED_HDC_H_ 56