WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
GDI.h
Go to the documentation of this file.
1/*
2 ​SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4*/
5
11
12#pragma once
13
14#include "Common.h"
15
16namespace winstd
17{
20
24 template<class T>
25 class gdi_handle : public handle<T, NULL>
26 {
28
29 public:
35 virtual ~gdi_handle()
36 {
37 if (m_h != invalid)
39 }
40
41 protected:
47 void free_internal() noexcept override
48 {
49 DeleteObject(m_h);
50 }
51 };
52
56 class dc : public handle<HDC, NULL>
57 {
59
60 public:
66 virtual ~dc()
67 {
68 if (m_h != invalid)
70 }
71
72 protected:
78 void free_internal() noexcept override
79 {
80 DeleteDC(m_h);
81 }
82 };
83
87 class window_dc : public handle<HDC, NULL>
88 {
89 public:
93 window_dc() noexcept :
94 m_hwnd(NULL)
95 {}
96
100 window_dc(_In_opt_ handle_type h, _In_opt_ HWND hwnd) noexcept :
102 m_hwnd(hwnd)
103 {}
104
108 window_dc(_Inout_ window_dc &&h) noexcept :
109 handle<handle_type, NULL>(std::move(h)),
110 m_hwnd(h.m_hwnd)
111 {}
112
116 window_dc& operator=(_Inout_ window_dc &&h) noexcept
117 {
119 m_hwnd = h.m_hwnd;
120 return *this;
121 }
122
124
125 public:
131 virtual ~window_dc()
132 {
133 if (m_h != invalid)
135 }
136
137 protected:
143 void free_internal() noexcept override
144 {
145 ReleaseDC(m_hwnd, m_h);
146 }
147
148 protected:
149 HWND m_hwnd;
150 };
151
156 {
159
160 public:
166 dc_selector(_In_ HDC hdc, _In_ HGDIOBJ h) noexcept :
167 m_hdc(hdc),
168 m_orig(SelectObject(hdc, h))
169 {
170 }
171
177 virtual ~dc_selector()
178 {
179 if (m_orig)
180 SelectObject(m_hdc, m_orig);
181 }
182
188 HGDIOBJ status() const noexcept
189 {
190 return m_orig;
191 }
192
193 protected:
194 HDC m_hdc;
195 HGDIOBJ m_orig;
196 };
197
199}
General API.
Context scope DC object restorer.
Definition: GDI.h:156
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:166
virtual ~dc_selector()
Restores original object.
Definition: GDI.h:177
HGDIOBJ m_orig
Original object handle.
Definition: GDI.h:195
HGDIOBJ status() const noexcept
Return result of SelectObject() call.
Definition: GDI.h:188
HDC m_hdc
A handle to the device context.
Definition: GDI.h:194
Device context wrapper class.
Definition: GDI.h:57
void free_internal() noexcept override
Deletes the specified device context (DC).
Definition: GDI.h:78
virtual ~dc()
Deletes the specified device context (DC).
Definition: GDI.h:66
Windows HGDIOBJ wrapper class.
Definition: GDI.h:26
void free_internal() noexcept override
Closes an open object handle.
Definition: GDI.h:47
virtual ~gdi_handle()
Closes an open object handle.
Definition: GDI.h:35
Base abstract template class to support generic object handle keeping.
Definition: Common.h:615
HDC handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:620
handle_type m_h
Object handle.
Definition: Common.h:866
Device context wrapper class.
Definition: GDI.h:88
HWND m_hwnd
Window handle.
Definition: GDI.h:149
window_dc(handle_type h, HWND hwnd) noexcept
Initializes a device context from existing data.
Definition: GDI.h:100
void free_internal() noexcept override
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:143
virtual ~window_dc()
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:131
window_dc() noexcept
Initializes an empty device context.
Definition: GDI.h:93
window_dc & operator=(window_dc &&h) noexcept
Copy an existing device context.
Definition: GDI.h:116
window_dc(window_dc &&h) noexcept
Move an existing device context.
Definition: GDI.h:108
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:74
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition: Common.h:82
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:171
static const T invalid
Invalid handle value.
Definition: Common.h:625