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 eda50a9
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 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 @@ -358,15 +359,22 @@ void setWindowRect()

SetWindowLong(App.hwnd, GWL_STYLE, App.window.style);

RECT wr_test = { 0 };
AdjustWindowRect(&wr_test, App.window.style, FALSE);

wr.left -= wr_test.left;
wr.right -= wr_test.left;
wr.top -= wr_test.top;
wr.bottom -= wr_test.top;
// 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);

AdjustWindowRect(&wr, App.window.style, FALSE);
// Get the invisible borders
RECT wr_test = { 0 };
DwmGetWindowAttribute(App.hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &wr_test, sizeof(wr_test));

RECT border;
border.left = wr_test.left - wr.left;
border.top = wr_test.top - wr.top;
border.right = wr.right - wr_test.right;
border.bottom = wr.bottom - wr_test.bottom;
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

0 comments on commit eda50a9

Please sign in to comment.