91 lines
2.7 KiB
C++
91 lines
2.7 KiB
C++
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2025 Vanessa T. <nessa@neko-tools.de>
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
#include "Window.h"
|
|
#include "Utility.h"
|
|
|
|
Window::Window(HWND hWnd) : m_hWnd(hWnd) {
|
|
|
|
}
|
|
|
|
static BOOL CALLBACK enumWindowsCallback(HWND hWnd, LPARAM lParam) {
|
|
auto windows = reinterpret_cast<std::list<Window>*>(lParam);
|
|
|
|
windows->emplace_back(hWnd);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
std::list<Window> Window::enumWindows() {
|
|
std::list<Window> windows{};
|
|
|
|
EnumWindows(enumWindowsCallback, reinterpret_cast<LPARAM>(&windows));
|
|
|
|
return windows;
|
|
}
|
|
|
|
std::wstring Window::title() const {
|
|
if (!m_hWnd) throw std::runtime_error("Invalid window handle");
|
|
|
|
auto length = GetWindowTextLengthW(m_hWnd);
|
|
if (length == 0) {
|
|
checkAndThrowLastWindowsError("Unable to get window title length");
|
|
return {};
|
|
}
|
|
|
|
std::vector<wchar_t> buffer(length + 1);
|
|
if (GetWindowTextW(m_hWnd, buffer.data(), length + 1) <= 0) {
|
|
checkAndThrowLastWindowsError("Unable to get window title");
|
|
}
|
|
|
|
return {buffer.data()};
|
|
}
|
|
|
|
DWORD Window::processId() const {
|
|
if (!m_hWnd) throw std::runtime_error("Invalid window handle");
|
|
|
|
DWORD processId;
|
|
if (GetWindowThreadProcessId(m_hWnd, &processId)) {
|
|
checkAndThrowLastWindowsError("Unable to get window process id");
|
|
}
|
|
|
|
return processId;
|
|
}
|
|
|
|
bool Window::isMainWindow() const {
|
|
if (!m_hWnd) throw std::runtime_error("Invalid window handle");
|
|
|
|
return GetWindow(m_hWnd, GW_OWNER) == nullptr;
|
|
}
|
|
|
|
void Window::bringToForeground() const {
|
|
SetForegroundWindow(m_hWnd);
|
|
}
|
|
|
|
HWND Window::handle() const {
|
|
return m_hWnd;
|
|
}
|