Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/updater/win32_window_util.cpp
7197 views
1
// SPDX-FileCopyrightText: 2019-2026 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#include "win32_window_util.h"
5
6
void Win32WindowUtil::CenterWindowOnMonitorAtCursorPosition(HWND hwnd)
7
{
8
// Get the current cursor position
9
POINT cursor_pos = {};
10
GetCursorPos(&cursor_pos);
11
12
// Find the monitor containing the cursor
13
HMONITOR monitor = MonitorFromPoint(cursor_pos, MONITOR_DEFAULTTONEAREST);
14
15
// Get monitor info (work area excludes taskbar)
16
MONITORINFO mi = {};
17
mi.cbSize = sizeof(MONITORINFO);
18
if (!GetMonitorInfoW(monitor, &mi))
19
return;
20
21
// Get the window dimensions
22
RECT window_rect = {};
23
GetWindowRect(hwnd, &window_rect);
24
const int window_width = window_rect.right - window_rect.left;
25
const int window_height = window_rect.bottom - window_rect.top;
26
27
// Calculate centered position within the monitor's work area
28
const RECT& work_area = mi.rcWork;
29
const int window_x = work_area.left + (work_area.right - work_area.left - window_width) / 2;
30
const int window_y = work_area.top + (work_area.bottom - work_area.top - window_height) / 2;
31
32
// Move the window
33
SetWindowPos(hwnd, nullptr, window_x, window_y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
34
}
35
36