Skip to content

Commit

Permalink
Fix position error on windows 10
Browse files Browse the repository at this point in the history
In windows 10, there is a invisible borders.
When window's position is set to (0, 0), there will be some gap
between screen left border and game window left border.

This commit fix the issue with solution provided in:
https://stackoverflow.com/a/34143777/1277762
  • Loading branch information
dabeibao committed Sep 2, 2023
1 parent 65555c1 commit 941c030
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions d2gl/src/win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "helpers.h"
#include "modules/hd_cursor.h"
#include "option/menu.h"
#include "dwmapi.h"

#include <detours/detours.h>

Expand Down Expand Up @@ -361,12 +362,33 @@ void setWindowRect()
RECT wr_test = { 0 };
AdjustWindowRect(&wr_test, App.window.style, FALSE);

AdjustWindowRect(&wr, App.window.style, FALSE);

// apply the shift
wr.left -= wr_test.left;
wr.right -= wr_test.left;
wr.top -= wr_test.top;
wr.bottom -= wr_test.top;

AdjustWindowRect(&wr, App.window.style, FALSE);
// show window first to get GetWindowRect works
SetWindowPos_Og(App.hwnd, HWND_NOTOPMOST, wr.left, wr.top, (wr.right - wr.left), (wr.bottom - wr.top), SWP_SHOWWINDOW);

// Get the invisible borders
RECT fr = { 0 };
DwmGetWindowAttribute(App.hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &fr, sizeof(fr));

RECT border;
border.left = fr.left - wr.left;
border.top = fr.top - wr.top;
border.right = wr.right - fr.right;
border.bottom = wr.bottom - fr.bottom;

// apply the border
wr.left -= border.left;
wr.top -= border.top;
wr.right += border.right;
wr.bottom += border.bottom;

SetWindowPos_Og(App.hwnd, HWND_NOTOPMOST, wr.left, wr.top, (wr.right - wr.left), (wr.bottom - wr.top), SWP_SHOWWINDOW);
trace_log("Switched to windowed mode: %d x %d", App.window.size.x, App.window.size.y);
} else {
Expand Down Expand Up @@ -490,4 +512,4 @@ RTL_OSVERSIONINFOW getOSVersion()
return info;
}

}
}

0 comments on commit 941c030

Please sign in to comment.