5

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.

Jesse Martinez
  • 403
  • 2
  • 5
  • 8
  • [Look here for windows version](http://stackoverflow.com/questions/6812224/getting-terminal-size-in-c-for-windows), for crossplatform you would need a library such as ncurses. – Bartek Banachewicz Nov 15 '13 at 16:39

3 Answers3

12

Use GetWindowRect

RECT rect;
if(GetWindowRect(hwnd, &rect))
{
  int width = rect.right - rect.left;
  int height = rect.bottom - rect.top;
}
Mr.Geeker
  • 395
  • 3
  • 13
1

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

Zuu
  • 1,123
  • 5
  • 11
0

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490