Skip to content

Commit 7c60bec

Browse files
committed
Fixed bug 4018 - Implement SDL_GetWindowBordersSize() under Windows/Win32/WinAPI
Ismael Ferreras Morezuelas (Swyter) As a new year gift I have implemented the Windows version of SDL_GetWindowBordersSize(). I needed it for auto-selecting a cozy window size for the game I'm currently working on and noticed that it only worked under X11, so I thought it could be a good excuse to contribute back more stuff. The Mercurial patch is attached as a .diff file. Let me know what you think. Happy 2018 to all the SDL2 devs and users! -- PS: Keep in mind that Windows 10 includes the 8px invisible grip borders as part of the frame. There's a way of detecting if Aero/DWM is being used and ask only for the visible rect, but I believe that GetWindowRect() is doing that for a reason and working as intended, so I haven't changed it. (See [2]) References: [1]: http://www.firststeps.ru/mfc/winapi/r.php?72 [2]: https://stackoverflow.com/a/34143777/674685 [3]: https://stackoverflow.com/a/431548/674685 [4]: https://wiki.libsdl.org/SDL_GetWindowBordersSize
1 parent 8111a63 commit 7c60bec

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/video/windows/SDL_windowsvideo.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ WIN_CreateDevice(int devindex)
143143
device->SetWindowIcon = WIN_SetWindowIcon;
144144
device->SetWindowPosition = WIN_SetWindowPosition;
145145
device->SetWindowSize = WIN_SetWindowSize;
146+
device->GetWindowBordersSize = WIN_GetWindowBordersSize;
146147
device->SetWindowOpacity = WIN_SetWindowOpacity;
147148
device->ShowWindow = WIN_ShowWindow;
148149
device->HideWindow = WIN_HideWindow;

src/video/windows/SDL_windowswindow.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,49 @@ WIN_SetWindowSize(_THIS, SDL_Window * window)
479479
WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
480480
}
481481

482+
int
483+
WIN_GetWindowBordersSize(_THIS, SDL_Window * window, int *top, int *left, int *bottom, int *right)
484+
{
485+
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
486+
RECT rcClient, rcWindow;
487+
POINT ptDiff;
488+
489+
/* rcClient stores the size of the inner window, while rcWindow stores the outer size relative to the top-left
490+
* screen position; so the top/left values of rcClient are always {0,0} and bottom/right are {height,width} */
491+
GetClientRect(hwnd, &rcClient);
492+
GetWindowRect(hwnd, &rcWindow);
493+
494+
/* convert the top/left values to make them relative to
495+
* the window; they will end up being slightly negative */
496+
ptDiff.y = rcWindow.top;
497+
ptDiff.x = rcWindow.left;
498+
499+
ScreenToClient(hwnd, &ptDiff);
500+
501+
rcWindow.top = ptDiff.y;
502+
rcWindow.left = ptDiff.x;
503+
504+
/* convert the bottom/right values to make them relative to the window,
505+
* these will be slightly bigger than the inner width/height */
506+
ptDiff.y = rcWindow.bottom;
507+
ptDiff.x = rcWindow.right;
508+
509+
ScreenToClient(hwnd, &ptDiff);
510+
511+
rcWindow.bottom = ptDiff.y;
512+
rcWindow.right = ptDiff.x;
513+
514+
/* Now that both the inner and outer rects use the same coordinate system we can substract them to get the border size.
515+
* Keep in mind that the top/left coordinates of rcWindow are negative because the border lies slightly before {0,0},
516+
* so switch them around because SDL2 wants them in positive. */
517+
*top = rcClient.top - rcWindow.top;
518+
*left = rcClient.left - rcWindow.left;
519+
*bottom = rcWindow.bottom - rcClient.bottom;
520+
*right = rcWindow.right - rcClient.right;
521+
522+
return 0;
523+
}
524+
482525
void
483526
WIN_ShowWindow(_THIS, SDL_Window * window)
484527
{

src/video/windows/SDL_windowswindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extern void WIN_SetWindowTitle(_THIS, SDL_Window * window);
5858
extern void WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon);
5959
extern void WIN_SetWindowPosition(_THIS, SDL_Window * window);
6060
extern void WIN_SetWindowSize(_THIS, SDL_Window * window);
61+
extern int WIN_GetWindowBordersSize(_THIS, SDL_Window * window, int *top, int *left, int *bottom, int *right);
6162
extern int WIN_SetWindowOpacity(_THIS, SDL_Window * window, float opacity);
6263
extern void WIN_ShowWindow(_THIS, SDL_Window * window);
6364
extern void WIN_HideWindow(_THIS, SDL_Window * window);

0 commit comments

Comments
 (0)