WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
GDI.h
1/*
2 ​SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4*/
5
6#pragma once
7
8#include "Common.h"
9
10namespace winstd
11{
17
21 template<class T>
22 class gdi_handle : public handle<T, NULL>
23 {
25
26 public:
32 virtual ~gdi_handle()
33 {
34 if (m_h != invalid)
36 }
37
38 protected:
44 void free_internal() noexcept override
45 {
46 DeleteObject(m_h);
47 }
48 };
49
53 class dc : public handle<HDC, NULL>
54 {
56
57 public:
63 virtual ~dc()
64 {
65 if (m_h != invalid)
67 }
68
69 protected:
75 void free_internal() noexcept override
76 {
77 DeleteDC(m_h);
78 }
79 };
80
84 class window_dc : public handle<HDC, NULL>
85 {
86 public:
87 window_dc() noexcept :
88 m_hwnd(NULL)
89 {}
90
91 window_dc(_In_opt_ handle_type h, _In_opt_ HWND hwnd) noexcept :
93 m_hwnd(hwnd)
94 {}
95
96 window_dc(_Inout_ window_dc &&h) noexcept :
97 handle<handle_type, NULL>(std::move(h)),
98 m_hwnd(h.m_hwnd)
99 {}
100
101 window_dc& operator=(_Inout_ window_dc &&h) noexcept
102 {
104 m_hwnd = h.m_hwnd;
105 return *this;
106 }
107
109
110 public:
116 virtual ~window_dc()
117 {
118 if (m_h != invalid)
120 }
121
122 protected:
128 void free_internal() noexcept override
129 {
130 ReleaseDC(m_hwnd, m_h);
131 }
132
133 protected:
134 HWND m_hwnd;
135 };
136
141 {
144
145 public:
151 dc_selector(_In_ HDC hdc, _In_ HGDIOBJ h) noexcept :
152 m_hdc(hdc),
153 m_orig(SelectObject(hdc, h))
154 {
155 }
156
162 virtual ~dc_selector()
163 {
164 if (m_orig)
165 SelectObject(m_hdc, m_orig);
166 }
167
173 HGDIOBJ status() const noexcept
174 {
175 return m_orig;
176 }
177
178 protected:
179 HDC m_hdc;
180 HGDIOBJ m_orig;
181 };
182
184}
Context scope DC object restorer.
Definition: GDI.h:141
virtual ~dc_selector()
Restores original object.
Definition: GDI.h:162
HGDIOBJ m_orig
Original object handle.
Definition: GDI.h:180
HGDIOBJ status() const noexcept
Return result of SelectObject() call.
Definition: GDI.h:173
HDC m_hdc
A handle to the device context.
Definition: GDI.h:179
dc_selector(_In_ HDC hdc, _In_ HGDIOBJ h) noexcept
Selects an object into the specified device context (DC). The new object replaces the previous object...
Definition: GDI.h:151
Device context wrapper class.
Definition: GDI.h:54
void free_internal() noexcept override
Deletes the specified device context (DC).
Definition: GDI.h:75
virtual ~dc()
Deletes the specified device context (DC).
Definition: GDI.h:63
Windows HGDIOBJ wrapper class.
Definition: GDI.h:23
void free_internal() noexcept override
Closes an open object handle.
Definition: GDI.h:44
virtual ~gdi_handle()
Closes an open object handle.
Definition: GDI.h:32
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
HDC handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
handle_type m_h
Object handle.
Definition: Common.h:833
Device context wrapper class.
Definition: GDI.h:85
HWND m_hwnd
Window handle.
Definition: GDI.h:134
void free_internal() noexcept override
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:128
virtual ~window_dc()
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:116
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:53
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition: Common.h:61
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:138
static const T invalid
Invalid handle value.
Definition: Common.h:590