WinStd
Windows Win32 API using Standard C++
GDI.h
1/*
2 ​SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4*/
5
7
8#pragma once
9
10#include "Common.h"
11
12namespace winstd
13{
16
20 template<class T>
21 class gdi_handle : public handle<T, NULL>
22 {
24
25 public:
31 virtual ~gdi_handle()
32 {
33 if (m_h != invalid)
35 }
36
37 protected:
43 void free_internal() noexcept override
44 {
45 DeleteObject(m_h);
46 }
47 };
48
52 class dc : public handle<HDC, NULL>
53 {
55
56 public:
62 virtual ~dc()
63 {
64 if (m_h != invalid)
66 }
67
68 protected:
74 void free_internal() noexcept override
75 {
76 DeleteDC(m_h);
77 }
78 };
79
83 class window_dc : public handle<HDC, NULL>
84 {
85 public:
89 window_dc() noexcept :
90 m_hwnd(NULL)
91 {}
92
96 window_dc(_In_opt_ handle_type h, _In_opt_ HWND hwnd) noexcept :
98 m_hwnd(hwnd)
99 {}
100
104 window_dc(_Inout_ window_dc &&h) noexcept :
105 handle<handle_type, NULL>(std::move(h)),
106 m_hwnd(h.m_hwnd)
107 {}
108
112 window_dc& operator=(_Inout_ window_dc &&h) noexcept
113 {
115 m_hwnd = h.m_hwnd;
116 return *this;
117 }
118
120
121 public:
127 virtual ~window_dc()
128 {
129 if (m_h != invalid)
131 }
132
133 protected:
139 void free_internal() noexcept override
140 {
141 ReleaseDC(m_hwnd, m_h);
142 }
143
144 protected:
145 HWND m_hwnd;
146 };
147
152 {
155
156 public:
162 dc_selector(_In_ HDC hdc, _In_ HGDIOBJ h) noexcept :
163 m_hdc(hdc),
164 m_orig(SelectObject(hdc, h))
165 {
166 }
167
173 virtual ~dc_selector()
174 {
175 if (m_orig)
176 SelectObject(m_hdc, m_orig);
177 }
178
184 HGDIOBJ status() const noexcept
185 {
186 return m_orig;
187 }
188
189 protected:
190 HDC m_hdc;
191 HGDIOBJ m_orig;
192 };
193
195}
Context scope DC object restorer.
Definition: GDI.h:152
dc_selector(HDC hdc, HGDIOBJ h) noexcept
Selects an object into the specified device context (DC). The new object replaces the previous object...
Definition: GDI.h:162
virtual ~dc_selector()
Restores original object.
Definition: GDI.h:173
HGDIOBJ m_orig
Original object handle.
Definition: GDI.h:191
HGDIOBJ status() const noexcept
Return result of SelectObject() call.
Definition: GDI.h:184
HDC m_hdc
A handle to the device context.
Definition: GDI.h:190
Device context wrapper class.
Definition: GDI.h:53
void free_internal() noexcept override
Deletes the specified device context (DC).
Definition: GDI.h:74
virtual ~dc()
Deletes the specified device context (DC).
Definition: GDI.h:62
Windows HGDIOBJ wrapper class.
Definition: GDI.h:22
void free_internal() noexcept override
Closes an open object handle.
Definition: GDI.h:43
virtual ~gdi_handle()
Closes an open object handle.
Definition: GDI.h:31
Base abstract template class to support generic object handle keeping.
Definition: Common.h:607
HDC handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:612
handle_type m_h
Object handle.
Definition: Common.h:858
Device context wrapper class.
Definition: GDI.h:84
HWND m_hwnd
Window handle.
Definition: GDI.h:145
window_dc(handle_type h, HWND hwnd) noexcept
Initializes a device context from existing data.
Definition: GDI.h:96
void free_internal() noexcept override
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:139
virtual ~window_dc()
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:127
window_dc() noexcept
Initializes an empty device context.
Definition: GDI.h:89
window_dc & operator=(window_dc &&h) noexcept
Copy an existing device context.
Definition: GDI.h:112
window_dc(window_dc &&h) noexcept
Move an existing device context.
Definition: GDI.h:104
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:66
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition: Common.h:74
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:163
static const T invalid
Invalid handle value.
Definition: Common.h:617