diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 0c4dc8418..b39e4b6a0 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -70,6 +70,12 @@ __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; #define GetProcAddress (void *)GetProcAddress #endif +typedef struct { + int count; + int screen; + HMONITOR monitor; +} EnumScreenData; + typedef struct { int count; int screen; @@ -82,6 +88,16 @@ typedef struct { Point2 pos; } EnumPosData; +static BOOL CALLBACK _MonitorEnumProcScreen(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { + EnumScreenData *data = (EnumScreenData *)dwData; + if (data->monitor == hMonitor) { + data->screen = data->count; + } + + data->count++; + return TRUE; +} + static BOOL CALLBACK _MonitorEnumProcSize(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { EnumSizeData *data = (EnumSizeData *)dwData; if (data->count == data->screen) { @@ -1354,7 +1370,13 @@ Error OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int */ - EnumSizeData data = { 0, 0, Size2() }; + // Get the primary monitor without providing hwnd + // Solution from https://devblogs.microsoft.com/oldnewthing/20070809-00/?p=25643 + const POINT ptZero = { 0, 0 }; + EnumScreenData primary_data = { 0, 0, MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY) }; + EnumDisplayMonitors(NULL, NULL, _MonitorEnumProcScreen, (LPARAM)&primary_data); + + EnumSizeData data = { 0, primary_data.screen, Size2() }; EnumDisplayMonitors(NULL, NULL, _MonitorEnumProcSize, (LPARAM)&data); WindowRect.right = data.size.width; @@ -1875,22 +1897,6 @@ int OS_Windows::get_screen_count() const { return data; } -typedef struct { - int count; - int screen; - HMONITOR monitor; -} EnumScreenData; - -static BOOL CALLBACK _MonitorEnumProcScreen(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { - EnumScreenData *data = (EnumScreenData *)dwData; - if (data->monitor == hMonitor) { - data->screen = data->count; - } - - data->count++; - return TRUE; -} - int OS_Windows::get_current_screen() const { EnumScreenData data = { 0, 0, MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST) }; EnumDisplayMonitors(NULL, NULL, _MonitorEnumProcScreen, (LPARAM)&data);