/* * MIT License * * Copyright (c) 2025 Vanessa T. * * 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 #include #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*>(lParam); windows->emplace_back(hWnd); return TRUE; } std::list Window::enumWindows() { std::list windows{}; EnumWindows(enumWindowsCallback, reinterpret_cast(&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 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; }