scoped_gdi_object.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #ifndef BASE_WIN_SCOPED_GDI_OBJECT_H_
  5. #define BASE_WIN_SCOPED_GDI_OBJECT_H_
  6. #include <windows.h>
  7. #include "base/scoped_generic.h"
  8. namespace base {
  9. namespace win {
  10. namespace internal {
  11. template <class T>
  12. struct ScopedGDIObjectTraits {
  13. static T InvalidValue() { return nullptr; }
  14. static void Free(T object) { DeleteObject(object); }
  15. };
  16. // An explicit specialization for HICON because we have to call DestroyIcon()
  17. // instead of DeleteObject() for HICON.
  18. template <>
  19. void inline ScopedGDIObjectTraits<HICON>::Free(HICON icon) {
  20. DestroyIcon(icon);
  21. }
  22. } // namespace internal
  23. // Like ScopedHandle but for GDI objects.
  24. template <class T>
  25. using ScopedGDIObject = ScopedGeneric<T, internal::ScopedGDIObjectTraits<T>>;
  26. // Typedefs for some common use cases.
  27. typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
  28. typedef ScopedGDIObject<HRGN> ScopedRegion;
  29. typedef ScopedGDIObject<HFONT> ScopedHFONT;
  30. typedef ScopedGDIObject<HICON> ScopedHICON;
  31. } // namespace win
  32. } // namespace base
  33. #endif // BASE_WIN_SCOPED_GDI_OBJECT_H_