1 // Copyright 2024 The PDFium Authors 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 CORE_FXCRT_WIN_SCOPED_SELECT_OBJECT_H_ 6 #define CORE_FXCRT_WIN_SCOPED_SELECT_OBJECT_H_ 7 8 #include <windows.h> 9 10 #include "core/fxcrt/check.h" 11 12 namespace pdfium { 13 14 // Helper class for deselecting object from DC. 15 class ScopedSelectObject { 16 public: ScopedSelectObject(HDC hdc,HGDIOBJ object)17 ScopedSelectObject(HDC hdc, HGDIOBJ object) 18 : hdc_(hdc), oldobj_(SelectObject(hdc, object)) { 19 DCHECK(hdc_); 20 DCHECK(object); 21 DCHECK(oldobj_); 22 DCHECK(oldobj_ != HGDI_ERROR); 23 } 24 25 ScopedSelectObject(const ScopedSelectObject&) = delete; 26 ScopedSelectObject& operator=(const ScopedSelectObject&) = delete; 27 ~ScopedSelectObject()28 ~ScopedSelectObject() { 29 [[maybe_unused]] HGDIOBJ object = SelectObject(hdc_, oldobj_); 30 DCHECK((GetObjectType(oldobj_) != OBJ_REGION && object) || 31 (GetObjectType(oldobj_) == OBJ_REGION && object != HGDI_ERROR)); 32 } 33 34 private: 35 const HDC hdc_; 36 const HGDIOBJ oldobj_; 37 }; 38 39 } // namespace pdfium 40 41 #endif // CORE_FXCRT_WIN_SCOPED_SELECT_OBJECT_H_ 42