title: Set console window size on Windows
body: <p>I know that there is a lot questions about how to set console size. But all found solutions are the same to my and my code doesn't works for me.</p>
  
  <p>Ok, so for setting console window size, I need two functions. They are <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686044(v=vs.85).aspx" rel="nofollow">SetConsoleScreenBufferSize()</a> and <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686125(v=vs.85).aspx" rel="nofollow">SetConsoleWindowInfo()</a>. First version of my function:</p>
  
  <pre><code>bool SetWindowSize(size_t width, size_t height)
  {
      HANDLE output_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
      if(output_handle == INVALID_HANDLE_VALUE)
          return false;
  
      COORD coord = {};
      coord.X = static_cast&lt;SHORT&gt;(width);
      coord.Y = static_cast&lt;SHORT&gt;(height);
      if(::SetConsoleScreenBufferSize(output_handle, coord) == FALSE)
          return false;
  
      SMALL_RECT rect = {};
      rect.Bottom = coord.X - 1;
      rect.Right = coord.Y - 1;
      return (::SetConsoleWindowInfo(output_handle, TRUE, &amp;rect) != FALSE);
  }
  </code></pre>
  
  <p><code>SetConsoleScreenBufferSize()</code> will work not for all values. From documentation:</p>
  
  <blockquote>
    <p>The specified width and height cannot be less than the width and
    height of the console screen buffer's window</p>
  </blockquote>
  
  <p>Lets try to get current window's size and call our function. To get window size, I need <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx" rel="nofollow">GetConsoleScreenBufferInfo()</a> function. main() test code:</p>
  
  <pre><code>HANDLE output_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
  if(output_handle == INVALID_HANDLE_VALUE)
      return 0;
  CONSOLE_SCREEN_BUFFER_INFO info = {};
  if(::GetConsoleScreenBufferInfo(output_handle, &amp;info) == FALSE)
      return 0;
  size_t width = info.srWindow.Right - info.srWindow.Left;
  size_t height = info.srWindow.Bottom - info.srWindow.Top;
  bool suc = SetWindowSize(width + 1, height + 1);
  </code></pre>
  
  <p>In this case <code>SetConsoleScreenBufferSize()</code> works fine. Next function is <code>SetConsoleWindowInfo()</code>. This function will work in case:</p>
  
  <blockquote>
    <p>The function fails if the specified window rectangle extends beyond
    the boundaries of the console screen buffer. This means that the <code>Top</code>
    and <code>Left</code> members of the <code>lpConsoleWindow</code> rectangle (or the calculated
    top and left coordinates, if <code>bAbsolute</code> is FALSE) cannot be less than
    zero. Similarly, the <code>Bottom</code> and <code>Right</code> members (or the calculated
    bottom and right coordinates) cannot be greater than (screen buffer
    <code>height – 1</code>) and (screen buffer <code>width – 1</code>), respectively. The function
    also fails if the <code>Right</code> member (or calculated right coordinate) is
    less than or equal to the <code>Left</code> member (or calculated left coordinate)
    or if the <code>Bottom</code> member (or calculated bottom coordinate) is less than
    or equal to the <code>Top</code> member (or calculated top coordinate).</p>
  </blockquote>
  
  <p>In our case, the values of rectangle are the same (because <code>Left</code> and <code>Top</code> are zeroes) as values of <code>info.srWindow</code> rectangle after call of <code>GetConsoleScreenBufferInfo()</code>. But! <code>SetConsoleWindowInfo()</code> fails with next <code>::GetLastError()</code></p>
  
  <pre><code>@err,hr ERROR_INVALID_PARAMETER : The parameter is incorrect.   unsigned int
  </code></pre>
  
  <p>If I swap calls of this two functions:</p>
  
  <pre><code>bool SetWindowSize(size_t width, size_t height)
  {
      HANDLE output_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
      if(output_handle == INVALID_HANDLE_VALUE)
          return false;
  
      SMALL_RECT rect = {};
      rect.Bottom = static_cast&lt;SHORT&gt;(width);
      rect.Right = static_cast&lt;SHORT&gt;(height);
      if(::SetConsoleWindowInfo(output_handle, TRUE, &amp;rect) == FALSE)
          return false;
  
      COORD coord = {};
      coord.X = rect.Bottom + 1;
      coord.Y = rect.Right + 1;
  
      return (::SetConsoleScreenBufferSize(output_handle, coord) != FALSE);
  }
  </code></pre>
  
  <p>then I will have the same error.</p>
  
  <p>So, how can I use <code>SetConsoleScreenBufferSize()</code> and <code>SetConsoleWindowInfo()</code> correctly ?</p>