Knowing the size of window decorations can be essential for certain application scenarios, especially when precise positioning and layout are required. Here are some practical examples:
Example:
#include <SDL.h>
#include <iostream>
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* Window{SDL_CreateWindow(
"Centered Client Area",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
300, 200, 0
)};
int Top, Left, Bottom, Right;
if (SDL_GetWindowBordersSize(
Window, &Top, &Left, &Bottom, &Right
) == 0) {
int ScreenWidth = 800;
int ScreenHeight = 600;
int NewX = (ScreenWidth - 300) / 2 - Left;
int NewY = (ScreenHeight - 200) / 2 - Top;
SDL_SetWindowPosition(Window, NewX, NewY);
}
SDL_Delay(3000);
SDL_DestroyWindow(Window);
SDL_Quit();
}
This approach ensures the client area, not the window frame, is centered on the screen.
Answers to questions are automatically generated and may not have been reviewed.
An introduction to managing SDL2 window decorations, borders, and client areas.