WinStd Windows Win32 API using Standard C++

Loading...
Searching...
No Matches
GDI.h
1/*
2 ​SPDX-License-Identifier: MIT
3 Copyright © 1991-2024 Amebis
4*/
5
7
8#pragma once
9
10#include "Common.h"
11
12 namespace winstd
13{
16
20 template<class T>
21 class gdi_handle : public handle<T, NULL>
22 {
23 WINSTD_HANDLE_IMPL(gdi_handle, T, NULL)
24
25 public:
31 virtual ~gdi_handle()
32 {
33 if (m_h != invalid)
34 free_internal();
35 }
36
37 protected:
43 void free_internal() noexcept override
44 {
45 DeleteObject(m_h);
46 }
47 };
48
52 class icon : public handle<HICON, NULL>
53 {
54 WINSTD_HANDLE_IMPL(icon, HICON, NULL)
55
56 public:
62 virtual ~icon()
63 {
64 if (m_h != invalid)
65 free_internal();
66 }
67
68 protected:
74 void free_internal() noexcept override
75 {
76 DestroyIcon(m_h);
77 }
78 };
79
83 class dc : public handle<HDC, NULL>
84 {
85 WINSTD_HANDLE_IMPL(dc, HDC, NULL)
86
87 public:
93 virtual ~dc()
94 {
95 if (m_h != invalid)
96 free_internal();
97 }
98
99 protected:
105 void free_internal() noexcept override
106 {
107 DeleteDC(m_h);
108 }
109 };
110
114 class window_dc : public handle<HDC, NULL>
115 {
116 public:
120 window_dc() noexcept :
121 m_hwnd(NULL)
122 {}
123
127 window_dc(_In_opt_ handle_type h, _In_opt_ HWND hwnd) noexcept :
128 handle<handle_type, NULL>(h),
129 m_hwnd(hwnd)
130 {}
131
135 window_dc(_Inout_ window_dc &&h) noexcept :
136 handle<handle_type, NULL>(std::move(h)),
137 m_hwnd(h.m_hwnd)
138 {}
139
143 window_dc& operator=(_Inout_ window_dc &&h) noexcept
144 {
145 handle<handle_type, NULL>::operator=(std::move(h));
146 m_hwnd = h.m_hwnd;
147 return *this;
148 }
149
150 WINSTD_NONCOPYABLE(window_dc)
151
152 public:
158 virtual ~window_dc()
159 {
160 if (m_h != invalid)
161 free_internal();
162 }
163
164 protected:
170 void free_internal() noexcept override
171 {
172 ReleaseDC(m_hwnd, m_h);
173 }
174
175 protected:
176 HWND m_hwnd;
177 };
178
182 class dc_selector
183 {
184 WINSTD_NONCOPYABLE(dc_selector)
185 WINSTD_NONMOVABLE(dc_selector)
186
187 public:
193 dc_selector(_In_ HDC hdc, _In_ HGDIOBJ h) noexcept :
194 m_hdc(hdc),
195 m_orig(SelectObject(hdc, h))
196 {}
197
203 virtual ~dc_selector()
204 {
205 if (m_orig)
206 SelectObject(m_hdc, m_orig);
207 }
208
214 HGDIOBJ status() const noexcept
215 {
216 return m_orig;
217 }
218
219 protected:
220 HDC m_hdc;
221 HGDIOBJ m_orig;
222 };
223
225}
winstd::dc_selector
Context scope DC object restorer.
Definition GDI.h:183
winstd::dc_selector::dc_selector
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:193
winstd::dc_selector::~dc_selector
virtual ~dc_selector()
Restores original object.
Definition GDI.h:203
winstd::dc_selector::m_orig
HGDIOBJ m_orig
Original object handle.
Definition GDI.h:221
winstd::dc_selector::status
HGDIOBJ status() const noexcept
Return result of SelectObject() call.
Definition GDI.h:214
winstd::dc_selector::m_hdc
HDC m_hdc
A handle to the device context.
Definition GDI.h:220
winstd::dc
Device context wrapper class.
Definition GDI.h:84
winstd::dc::free_internal
void free_internal() noexcept override
Deletes the specified device context (DC).
Definition GDI.h:105
winstd::dc::~dc
virtual ~dc()
Deletes the specified device context (DC).
Definition GDI.h:93
winstd::gdi_handle
Windows HGDIOBJ wrapper class.
Definition GDI.h:22
winstd::gdi_handle::free_internal
void free_internal() noexcept override
Closes an open object handle.
Definition GDI.h:43
winstd::gdi_handle::~gdi_handle
virtual ~gdi_handle()
Closes an open object handle.
Definition GDI.h:31
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition Common.h:1024
winstd::handle< T, NULL >::m_h
handle_type m_h
Object handle.
Definition Common.h:1276
winstd::icon
Windows HICON wrapper class.
Definition GDI.h:53
winstd::icon::free_internal
void free_internal() noexcept override
Closes an open object handle.
Definition GDI.h:74
winstd::icon::~icon
virtual ~icon()
Closes an open object handle.
Definition GDI.h:62
winstd::ref_unique_ptr
Helper class for returning pointers to std::unique_ptr.
Definition Common.h:863
winstd::window_dc
Device context wrapper class.
Definition GDI.h:115
winstd::window_dc::m_hwnd
HWND m_hwnd
Window handle.
Definition GDI.h:176
winstd::window_dc::window_dc
window_dc(handle_type h, HWND hwnd) noexcept
Initializes a device context from existing data.
Definition GDI.h:127
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:170
winstd::window_dc::~window_dc
virtual ~window_dc()
Releases a device context (DC), freeing it for use by other applications.
Definition GDI.h:158
winstd::window_dc::window_dc
window_dc() noexcept
Initializes an empty device context.
Definition GDI.h:120
winstd::window_dc::operator=
window_dc & operator=(window_dc &&h) noexcept
Copy an existing device context.
Definition GDI.h:143
winstd::window_dc::window_dc
window_dc(window_dc &&h) noexcept
Move an existing device context.
Definition GDI.h:135
WINSTD_NONCOPYABLE
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition Common.h:67
WINSTD_NONMOVABLE
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition Common.h:75
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:164
winstd::handle< T, NULL >::invalid
static const T invalid
Invalid handle value.
Definition Common.h:1034