Window Decorations and Borders

Uses of window decoration sizes

What are some practical uses for knowing the size of window decorations?

Abstract art representing computer programming

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:

  • Accurate Window Placement: If your application needs to position its window precisely on the screen, knowing the decoration sizes helps ensure the client area aligns with the intended location. For example, if you want the content to occupy the top-left corner of the screen, you need to offset the window’s position by the size of the decorations.
  • Custom Borders and Overlays: Applications with custom-drawn borders or overlays, such as video editors or games, need to know the decoration sizes to avoid drawing over them or to adjust the custom graphics.
  • Fullscreen Simulation: In simulated fullscreen modes (borderless windows), understanding decoration sizes ensures the window is correctly sized to fill the screen without cutting off or overlapping system UI elements.
  • Drag-and-Drop Applications: In applications where the user can drag elements between multiple windows, knowing the decoration sizes ensures seamless alignment of draggable content between windows.

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.

This Question is from the Lesson:

Window Decorations and Borders

An introduction to managing SDL2 window decorations, borders, and client areas.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Window Decorations and Borders

An introduction to managing SDL2 window decorations, borders, and client areas.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 71 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved