-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitors.hpp
104 lines (90 loc) · 4.02 KB
/
monitors.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
The MIT License (MIT)
Copyright (c) 2025 sigma-axis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <cstdint>
#include <algorithm>
#include <tuple>
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
////////////////////////////////
// Windows API 利用の補助関数.
////////////////////////////////
namespace sigma_lib::W32
{
template<bool work, uint32_t default_to = MONITOR_DEFAULTTONEAREST>
struct monitor {
// work area: the portion of the screen excluding the task bar area (and maybe others).
RECT bound;
bool is_primary;
explicit monitor(int x, int y) : monitor(POINT{ x, y }) {}
explicit monitor(POINT const& point) : monitor(::MonitorFromPoint(point, default_to)) {}
explicit monitor(int left, int top, int right, int bottom) : monitor(RECT{ left, top, right, bottom }) {}
explicit monitor(RECT const& rect) : monitor(::MonitorFromRect(&rect, default_to)) {}
explicit monitor(HWND hwnd) : monitor(::MonitorFromWindow(hwnd, default_to)) {}
monitor(HMONITOR monitor = nullptr)
{
MONITORINFO mi{ .cbSize = sizeof(mi) };
::GetMonitorInfoW(monitor == nullptr ?
::MonitorFromPoint({ 0, 0 }, MONITOR_DEFAULTTOPRIMARY) : // yields primary.
monitor, &mi);
if constexpr (work) bound = mi.rcWork;
else bound = mi.rcMonitor;
is_primary = (mi.dwFlags & MONITORINFOF_PRIMARY) != 0;
}
constexpr int width() const { return bound.right - bound.left; }
constexpr int height() const { return bound.bottom - bound.top; }
constexpr auto clamp(int left, int top, int right, int bottom) const {
return clamp_core(left, top, right, bottom, bound);
}
constexpr RECT clamp(RECT const& rect) const {
auto [l, t, r, b] = clamp(rect.left, rect.top, rect.right, rect.bottom);
return { l, t, r, b };
}
constexpr auto clamp(int x, int y) const {
return clamp_core(x, y, bound);
}
constexpr POINT clamp(POINT const& point) const {
auto [x, y] = clamp(point.x, point.y);
return { x, y };
}
constexpr monitor& expand(int left, int top, int right, int bottom)
{
bound.left -= left;
bound.top -= top;
bound.right += right;
bound.bottom += bottom;
return *this;
}
constexpr monitor& expand(int horiz, int vert) { return expand(horiz, vert, horiz, vert); }
constexpr monitor& expand(int len) { return expand(len, len); }
private:
constexpr static auto clamp_core(int x, int y, RECT const& rc)
{
return std::pair{
std::clamp<int>(x, rc.left, rc.right),
std::clamp<int>(y, rc.top, rc.bottom)
};
}
constexpr static auto clamp_core(int left, int top, int right, int bottom, RECT const& rc)
{
auto [l, r] = clamp_core(left, right, rc.left, rc.right);
auto [t, b] = clamp_core(top, bottom, rc.top, rc.bottom);
return std::tuple{ l, t, r, b };
}
constexpr static std::pair<int, int> clamp_core(int lbd, int ubd, int min, int max)
{
bool oversized = ubd - lbd > max - min;
if ((lbd < min) ^ oversized)
return { min, min + ubd - lbd };
else if ((ubd > max) ^ oversized)
return { max + lbd - ubd, max };
else return { lbd, ubd };
}
};
}