GDI.h
/*
​SPDX-License-Identifier: MIT
Copyright © 1991-2022 Amebis
*/
#pragma once
#include "Common.h"
namespace winstd
{
template<class T>
class gdi_handle : public handle<T, NULL>
{
WINSTD_HANDLE_IMPL(gdi_handle, NULL)
public:
virtual ~gdi_handle()
{
if (m_h != invalid)
free_internal();
}
protected:
void free_internal() noexcept override
{
DeleteObject(m_h);
}
};
class dc : public handle<HDC, NULL>
{
WINSTD_HANDLE_IMPL(dc, NULL)
public:
virtual ~dc()
{
if (m_h != invalid)
free_internal();
}
protected:
void free_internal() noexcept override
{
DeleteDC(m_h);
}
};
class window_dc : public handle<HDC, NULL>
{
public:
window_dc() noexcept :
m_hwnd(NULL)
{}
window_dc(_In_opt_ handle_type h, _In_opt_ HWND hwnd) noexcept :
handle<handle_type, NULL>(h),
m_hwnd(hwnd)
{}
window_dc(_Inout_ window_dc &&h) noexcept :
handle<handle_type, NULL>(std::move(h)),
m_hwnd(h.m_hwnd)
{}
window_dc& operator=(_Inout_ window_dc &&h) noexcept
{
handle<handle_type, NULL>::operator=(std::move(h));
m_hwnd = h.m_hwnd;
return *this;
}
WINSTD_NONCOPYABLE(window_dc)
public:
virtual ~window_dc()
{
if (m_h != invalid)
free_internal();
}
protected:
void free_internal() noexcept override
{
ReleaseDC(m_hwnd, m_h);
}
protected:
HWND m_hwnd;
};
class dc_selector
{
WINSTD_NONCOPYABLE(dc_selector)
WINSTD_NONMOVABLE(dc_selector)
public:
dc_selector(_In_ HDC hdc, _In_ HGDIOBJ h) noexcept :
m_hdc(hdc),
m_orig(SelectObject(hdc, h))
{
}
virtual ~dc_selector()
{
if (m_orig)
SelectObject(m_hdc, m_orig);
}
HGDIOBJ status() const noexcept
{
return m_orig;
}
protected:
HDC m_hdc;
HGDIOBJ m_orig;
};
}
winstd::dc_selector
Context scope DC object restorer.
Definition: GDI.h:153
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:163
winstd::dc_selector::~dc_selector
virtual ~dc_selector()
Restores original object.
Definition: GDI.h:174
winstd::dc_selector::m_orig
HGDIOBJ m_orig
Original object handle.
Definition: GDI.h:192
winstd::dc_selector::status
HGDIOBJ status() const noexcept
Return result of SelectObject() call.
Definition: GDI.h:185
winstd::dc_selector::m_hdc
HDC m_hdc
A handle to the device context.
Definition: GDI.h:191
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:604
winstd::handle< HDC, NULL >::handle_type
HDC handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:609
winstd::handle< T, NULL >::m_h
handle_type m_h
Object handle.
Definition: Common.h:855
winstd::window_dc
Device context wrapper class.
Definition: GDI.h:85
winstd::window_dc::m_hwnd
HWND m_hwnd
Window handle.
Definition: GDI.h:146
winstd::window_dc::window_dc
window_dc(handle_type h, HWND hwnd) noexcept
Initializes a device context from existing data.
Definition: GDI.h:97
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:140
winstd::window_dc::~window_dc
virtual ~window_dc()
Releases a device context (DC), freeing it for use by other applications.
Definition: GDI.h:128
winstd::window_dc::window_dc
window_dc() noexcept
Initializes an empty device context.
Definition: GDI.h:90
winstd::window_dc::operator=
window_dc & operator=(window_dc &&h) noexcept
Copy an existing device context.
Definition: GDI.h:113
winstd::window_dc::window_dc
window_dc(window_dc &&h) noexcept
Move an existing device context.
Definition: GDI.h:105
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:162
winstd::handle< T, NULL >::invalid
static const T invalid
Invalid handle value.
Definition: Common.h:614