Jesse Martinez
Jesse Martinez

Reputation: 403

C++ How to get the window size (width and height)?

I'm trying to create a graph and I need to know the size of the window the user is running the code in. I'm trying to scale the data so the data shows only on the size of the window, without wrapping or scrolling. I'm on windows but I want to use something similar to Linux equivalent

int lines = atoi(getenv("LINES") ;
int cols  = atoi(getenv("COLUMNS") ;

So I can scale numbers and show a graph like this

320 a ============================================================
160 b ==============================
 80 c ===============
 40 d =======
 20 e ===
 10 f =
  5 g 
  2 h 
  1 i 
  2 j 
 17 k ===
 41 l =======
 67 m ============
 97 n ==================
127 o ========================
157 p =============================
191 q ====================================
227 r ===========================================
257 s ================================================
283 t =====================================================
331 u ==============================================================
367 v =====================================================================
373 w ======================================================================
379 x ========================================================================
383 y ========================================================================
389 z ==========================================================================

Is there something that will work on Windows and Linux? I'm using Visual Studio 2012.

Upvotes: 5

Views: 45949

Answers (3)

David Heffernan
David Heffernan

Reputation: 612794

You already have a solution for Linux.

On Windows the API call you need is GetConsoleScreenBufferInfo.

This returns a CONSOLE_SCREEN_BUFFER_INFO struct, from which you read out the dwSize member:

A COORD structure that contains the size of the console screen buffer, in character columns and rows.

Upvotes: 0

Zuu
Zuu

Reputation: 1123

Use GetConsoleScreenBufferInfo or one of its siblings. You are interrested in the dzSize field of the "returned" struct. Read documentation here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx

Upvotes: 1

Mr.Geeker
Mr.Geeker

Reputation: 445

Use GetWindowRect

RECT rect;
if(GetWindowRect(hwnd, &rect))
{
  int width = rect.right - rect.left;
  int height = rect.bottom - rect.top;
}

Upvotes: 16

Related Questions