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
10 namespace winstd
11{
17
21 template<class T>
22 class gdi_handle : public handle<T, NULL>
23 {
24 WINSTD_HANDLE_IMPL(gdi_handle, NULL)
25
26 public:
32 virtual ~gdi_handle()
33 {
34 if (m_h != invalid)
35 free_internal();
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 {
55 WINSTD_HANDLE_IMPL(dc, NULL)
56
57 public:
63 virtual ~dc()
64 {
65 if (m_h != invalid)
66 free_internal();
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 :
92 handle<handle_type, NULL>(h),
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 {
103 handle<handle_type, NULL>::operator=(std::move(h));
104 m_hwnd = h.m_hwnd;
105 return *this;
106 }
107
108 WINSTD_NONCOPYABLE(window_dc)
109
110 public:
116 virtual ~window_dc()
117 {
118 if (m_h != invalid)
119 free_internal();
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
140 class dc_selector
141 {
142 WINSTD_NONCOPYABLE(dc_selector)
143 WINSTD_NONMOVABLE(dc_selector)
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}
winstd::dc_selector
Context scope DC object restorer.
Definition: GDI.h:141
winstd::dc_selector::~dc_selector
virtual ~dc_selector()
Restores original object.
Definition: GDI.h:162
winstd::dc_selector::m_orig
HGDIOBJ m_orig
Original object handle.
Definition: GDI.h:180
winstd::dc_selector::status
HGDIOBJ status() const noexcept
Return result of SelectObject() call.
Definition: GDI.h:173
winstd::dc_selector::m_hdc
HDC m_hdc
A handle to the device context.
Definition: GDI.h:179
winstd::dc_selector::dc_selector
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
winstd::dc
Device context wrapper class.
Definition: GDI.h:54
winstd::dc::free_internal
void free_internal() noexcept override
Deletes the specified device context (DC).
Definition: GDI.h:75
winstd::dc::~dc
virtual ~dc()
Deletes the specified device context (DC).
Definition: GDI.h:63
winstd::gdi_handle
Windows HGDIOBJ wrapper class.
Definition: GDI.h:23
winstd::gdi_handle::free_internal
void free_internal() noexcept override
Closes an open object handle.
Definition: GDI.h:44
winstd::gdi_handle::~gdi_handle
virtual ~gdi_handle()
Closes an open object handle.
Definition: GDI.h:32
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
winstd::handle< HDC, NULL >::handle_type
HDC handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
winstd::handle< T, NULL >::m_h
handle_type m_h
Object handle.
Definition: Common.h:833
winstd::window_dc
Device context wrapper class.
Definition: GDI.h:85
winstd::window_dc::m_hwnd
HWND m_hwnd
Window handle.
Definition: GDI.h:134
winstd::window_dc::free_internal
void free_internal() noexcept override
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:128
winstd::window_dc::~window_dc
virtual ~window_dc()
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:116
WINSTD_NONCOPYABLE
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:53
WINSTD_NONMOVABLE
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition: Common.h:61
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:138
winstd::handle< T, NULL >::invalid
static const T invalid
Invalid handle value.
Definition: Common.h:590