|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Xuesong Peng <pengxuesong.cn@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +WCHAR_SIZE() { |
| 20 | + return 2 |
| 21 | +} |
| 22 | + |
| 23 | +global CCHDEVICENAME := 32 |
| 24 | +global MONITOR_DEFAULTTONULL := 0 |
| 25 | +global MONITOR_DEFAULTTOPRIMARY := 1 |
| 26 | +global MONITOR_DEFAULTTONEAREST := 2 |
| 27 | + |
| 28 | +class Point extends Class { |
| 29 | + __New(x := 0, y := 0) { |
| 30 | + this.buff := Buffer(Point.struct_size(), 0) |
| 31 | + this.x := x |
| 32 | + this.y := y |
| 33 | + } |
| 34 | + |
| 35 | + static x_offset := (*) => 0 |
| 36 | + static y_offset := (*) => Point.x_offset() + INT_SIZE() |
| 37 | + static struct_size := (*) => Point.y_offset() + INT_SIZE() |
| 38 | + |
| 39 | + struct_ptr := (*) => this.buff.Ptr |
| 40 | + |
| 41 | + x { |
| 42 | + get => NumGet(this.struct_ptr(), Point.x_offset(), "Int") |
| 43 | + set => NumPut("Int", Value, this.struct_ptr(), Point.x_offset()) |
| 44 | + } |
| 45 | + y { |
| 46 | + get => NumGet(this.struct_ptr(), Point.y_offset(), "Int") |
| 47 | + set => NumPut("Int", Value, this.struct_ptr(), Point.y_offset()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +class Rect extends Class { |
| 52 | + __New(left := 0, top := 0, right := 0, bottom := 0) { |
| 53 | + this.buff := Buffer(Rect.struct_size(), 0) |
| 54 | + this.left := left |
| 55 | + this.top := top |
| 56 | + this.right := right |
| 57 | + this.bottom := bottom |
| 58 | + } |
| 59 | + |
| 60 | + static left_offset := (*) => 0 |
| 61 | + static top_offset := (*) => Rect.left_offset() + INT_SIZE() |
| 62 | + static right_offset := (*) => Rect.top_offset() + INT_SIZE() |
| 63 | + static bottom_offset := (*) => Rect.right_offset() + INT_SIZE() |
| 64 | + static struct_size := (*) => Rect.bottom_offset() + INT_SIZE() |
| 65 | + |
| 66 | + struct_ptr := (*) => this.buff.Ptr |
| 67 | + |
| 68 | + left { |
| 69 | + get => NumGet(this.struct_ptr(), Rect.left_offset(), "Int") |
| 70 | + set => NumPut("Int", Value, this.struct_ptr(), Rect.left_offset()) |
| 71 | + } |
| 72 | + top { |
| 73 | + get => NumGet(this.struct_ptr(), Rect.top_offset(), "Int") |
| 74 | + set => NumPut("Int", Value, this.struct_ptr(), Rect.top_offset()) |
| 75 | + } |
| 76 | + right { |
| 77 | + get => NumGet(this.struct_ptr(), Rect.right_offset(), "Int") |
| 78 | + set => NumPut("Int", Value, this.struct_ptr(), Rect.right_offset()) |
| 79 | + } |
| 80 | + bottom { |
| 81 | + get => NumGet(this.struct_ptr(), Rect.bottom_offset(), "Int") |
| 82 | + set => NumPut("Int", Value, this.struct_ptr(), Rect.bottom_offset()) |
| 83 | + } |
| 84 | + |
| 85 | + width() { |
| 86 | + return this.right - this.left |
| 87 | + } |
| 88 | + height() { |
| 89 | + return this.bottom - this.top |
| 90 | + } |
| 91 | +} ; Rect |
| 92 | + |
| 93 | +class MonitorInfo extends Class { |
| 94 | + __New() { |
| 95 | + this.buff := Buffer(MonitorInfo.struct_size(), 0) |
| 96 | + NumPut("Int", MonitorInfo.struct_size(), this.struct_ptr()) |
| 97 | + } |
| 98 | + |
| 99 | + static size_offset := (*) => 0 |
| 100 | + static monitor_offset := (*) => MonitorInfo.size_offset() + INT_SIZE() |
| 101 | + static work_offset := (*) => MonitorInfo.monitor_offset() + Rect.struct_size() |
| 102 | + static flags_offset := (*) => MonitorInfo.work_offset() + Rect.struct_size() |
| 103 | + static struct_size := (*) => MonitorInfo.flags_offset() + INT_SIZE() |
| 104 | + |
| 105 | + struct_ptr := (*) => this.buff.Ptr |
| 106 | + |
| 107 | + size { |
| 108 | + get => NumGet(this.struct_ptr(), MonitorInfo.size_offset(), "Int") |
| 109 | + } |
| 110 | + monitor { |
| 111 | + get => Rect( |
| 112 | + NumGet(this.struct_ptr(), MonitorInfo.monitor_offset(), "Int"), |
| 113 | + NumGet(this.struct_ptr(), MonitorInfo.monitor_offset() + INT_SIZE(), "Int"), |
| 114 | + NumGet(this.struct_ptr(), MonitorInfo.monitor_offset() + INT_SIZE() * 2, "Int"), |
| 115 | + NumGet(this.struct_ptr(), MonitorInfo.monitor_offset() + INT_SIZE() * 3, "Int") |
| 116 | + ) |
| 117 | + } |
| 118 | + work { |
| 119 | + get => Rect( |
| 120 | + NumGet(this.struct_ptr(), MonitorInfo.work_offset(), "Int"), |
| 121 | + NumGet(this.struct_ptr(), MonitorInfo.work_offset() + INT_SIZE(), "Int"), |
| 122 | + NumGet(this.struct_ptr(), MonitorInfo.work_offset() + INT_SIZE() * 2, "Int"), |
| 123 | + NumGet(this.struct_ptr(), MonitorInfo.work_offset() + INT_SIZE() * 3, "Int") |
| 124 | + ) |
| 125 | + } |
| 126 | + flags { |
| 127 | + get => NumGet(this.struct_ptr(), MonitorInfo.flags_offset(), "Int") |
| 128 | + } |
| 129 | +} ; MonitorInfo |
| 130 | + |
| 131 | +class MonitorInfoEx extends MonitorInfo { |
| 132 | + __New() { |
| 133 | + this.buff := Buffer(MonitorInfoEx.struct_size(), 0) |
| 134 | + NumPut("Int", MonitorInfoEx.struct_size(), this.struct_ptr()) |
| 135 | + } |
| 136 | + |
| 137 | + static device_offset := (*) => MonitorInfoEx.flags_offset() + INT_SIZE() |
| 138 | + static struct_size := (*) => MonitorInfoEx.device_offset() + WCHAR_SIZE() * CCHDEVICENAME |
| 139 | + |
| 140 | + device { |
| 141 | + get => StrGet(this.struct_ptr() + MonitorInfoEx.device_offset(), CCHDEVICENAME) |
| 142 | + } |
| 143 | +} ; MonitorInfoEx |
| 144 | + |
| 145 | +class MonitorManage extends Class { |
| 146 | + static monitors := Map() |
| 147 | + |
| 148 | + static MonitorEnumProc(hMon, hDC, rect, data) { |
| 149 | + MonitorManage.monitors[hMon] := MonitorManage.GetMonitorInfo(hMon) |
| 150 | + return true |
| 151 | + } |
| 152 | + |
| 153 | + static EnumDisplayMonitors() { |
| 154 | + MonitorManage.monitors := Map() |
| 155 | + return DllCall("EnumDisplayMonitors", "Ptr", 0, "Ptr", 0, "Ptr", CallbackCreate(ObjBindMethod(MonitorManage, "MonitorEnumProc"), , 4), "Ptr", 0) |
| 156 | + } |
| 157 | + |
| 158 | + static MonitorFromWindow(hWnd, flags := MONITOR_DEFAULTTONULL) { |
| 159 | + return DllCall("MonitorFromWindow", "Ptr", hWnd, "UInt", flags) |
| 160 | + } |
| 161 | + |
| 162 | + static MonitorFromPoint(point, flags := MONITOR_DEFAULTTONULL) { |
| 163 | + return DllCall("MonitorFromPoint", "Int64", (point.x & 0xFFFFFFFF) | (point.y << 32), "UInt", flags) |
| 164 | + } |
| 165 | + |
| 166 | + static MonitorFromRect(rect, flags := MONITOR_DEFAULTTONULL) { |
| 167 | + return DllCall("MonitorFromRect", "Ptr", rect.struct_ptr(), "UInt", flags) |
| 168 | + } |
| 169 | + |
| 170 | + static GetMonitorInfo(hMon) { |
| 171 | + info := MonitorInfoEx() |
| 172 | + res := DllCall("GetMonitorInfo", "Ptr", hMon, "Ptr", info.struct_ptr()) |
| 173 | + return res ? info : 0 |
| 174 | + } |
| 175 | +} ; MonitorManage |
0 commit comments