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:
90 window_dc() noexcept :
91 m_hwnd(NULL)
92 {}
93
97 window_dc(_In_opt_ handle_type h, _In_opt_ HWND hwnd) noexcept :
99 m_hwnd(hwnd)
100 {}
101
105 window_dc(_Inout_ window_dc &&h) noexcept :
106 handle<handle_type, NULL>(std::move(h)),
107 m_hwnd(h.m_hwnd)
108 {}
109
113 window_dc& operator=(_Inout_ window_dc &&h) noexcept
114 {
116 m_hwnd = h.m_hwnd;
117 return *this;
118 }
119
121
122 public:
128 virtual ~window_dc()
129 {
130 if (m_h != invalid)
132 }
133
134 protected:
140 void free_internal() noexcept override
141 {
142 ReleaseDC(m_hwnd, m_h);
143 }
144
145 protected:
146 HWND m_hwnd;
147 };
148
153 {
156
157 public:
163 dc_selector(_In_ HDC hdc, _In_ HGDIOBJ h) noexcept :
164 m_hdc(hdc),
165 m_orig(SelectObject(hdc, h))
166 {
167 }
168
174 virtual ~dc_selector()
175 {
176 if (m_orig)
177 SelectObject(m_hdc, m_orig);
178 }
179
185 HGDIOBJ status() const noexcept
186 {
187 return m_orig;
188 }
189
190 protected:
191 HDC m_hdc;
192 HGDIOBJ m_orig;
193 };
194
196}
Context scope DC object restorer.
Definition: GDI.h:153
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:163
virtual ~dc_selector()
Restores original object.
Definition: GDI.h:174
HGDIOBJ m_orig
Original object handle.
Definition: GDI.h:192
HGDIOBJ status() const noexcept
Return result of SelectObject() call.
Definition: GDI.h:185
HDC m_hdc
A handle to the device context.
Definition: GDI.h:191
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:603
HDC handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:608
handle_type m_h
Object handle.
Definition: Common.h:854
Device context wrapper class.
Definition: GDI.h:85
HWND m_hwnd
Window handle.
Definition: GDI.h:146
window_dc(handle_type h, HWND hwnd) noexcept
Initializes a device context from existing data.
Definition: GDI.h:97
void free_internal() noexcept override
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:140
virtual ~window_dc()
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:128
window_dc() noexcept
Initializes an empty device context.
Definition: GDI.h:90
window_dc & operator=(window_dc &&h) noexcept
Copy an existing device context.
Definition: GDI.h:113
window_dc(window_dc &&h) noexcept
Move an existing device context.
Definition: GDI.h:105
#define WINSTD_NONCOPYABLE(C)
Declares a class as non-copyable.
Definition: Common.h:52
#define WINSTD_NONMOVABLE(C)
Declares a class as non-movable.
Definition: Common.h:60
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:161
static const T invalid
Invalid handle value.
Definition: Common.h:613