scoped_select_object.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_WIN_SCOPED_SELECT_OBJECT_H_
  5. #define BASE_WIN_SCOPED_SELECT_OBJECT_H_
  6. #include <windows.h>
  7. #include "base/check.h"
  8. #include "base/macros.h"
  9. namespace base {
  10. namespace win {
  11. // Helper class for deselecting object from DC.
  12. class ScopedSelectObject {
  13. public:
  14. ScopedSelectObject(HDC hdc, HGDIOBJ object)
  15. : hdc_(hdc), oldobj_(SelectObject(hdc, object)) {
  16. DCHECK(hdc_);
  17. DCHECK(object);
  18. DCHECK(oldobj_ != NULL && oldobj_ != HGDI_ERROR);
  19. }
  20. ~ScopedSelectObject() {
  21. HGDIOBJ object = SelectObject(hdc_, oldobj_);
  22. DCHECK((GetObjectType(oldobj_) != OBJ_REGION && object != NULL) ||
  23. (GetObjectType(oldobj_) == OBJ_REGION && object != HGDI_ERROR));
  24. }
  25. private:
  26. HDC hdc_;
  27. HGDIOBJ oldobj_;
  28. DISALLOW_COPY_AND_ASSIGN(ScopedSelectObject);
  29. };
  30. } // namespace win
  31. } // namespace base
  32. #endif // BASE_WIN_SCOPED_SELECT_OBJECT_H_