CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2022
    Posts
    11

    Aspect and format of the console

    I would like to change the aspect and the format fo the shell used to launch the following code:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
    	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO InfCon;
    	GetConsoleScreenBufferInfo(hCon, &InfCon);
    	if (!GetConsoleScreenBufferInfo(hCon, &InfCon))
    	{
    		printf("GetConsoleScreenBufferInfo failed %d\n", GetLastError());
    		return 1;
    	}
    	printf("x: %d, y: %d\n", InfCon.dwSize.X, InfCon.dwSize.Y);
    	InfCon.dwSize.X = 50;
    	InfCon.dwSize.Y = 50;
    	
    	if (!SetConsoleScreenBufferSize(hCon, InfCon.dwSize))
    	{
    		printf("SetConsoleScreenBufferSize %d\n", GetLastError());
    		return 1;
    	}
    	
    	return 0;
    }
    but the console don't change and also get an error:

    Code:
    C:\Users\Quasar999\Documents\Source\WIN32\Win32_0016\ConsoleApplication1\x64\Release>ConsoleApplication1
    x: 120, y: 9001
    SetConsoleScreenBufferSize 87
    
    C:\Users\Quasar999\Documents\Source\WIN32\Win32_0016\ConsoleApplication1\x64\Release>

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,246

    Re: Aspect and format of the console

    Quote Originally Posted by Quasar999 View Post
    I would like to change the aspect and the format fo the shell used to launch the following code:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
    	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    	CONSOLE_SCREEN_BUFFER_INFO InfCon;
    	GetConsoleScreenBufferInfo(hCon, &InfCon);
    	if (!GetConsoleScreenBufferInfo(hCon, &InfCon))
    	{
    		printf("GetConsoleScreenBufferInfo failed %d\n", GetLastError());
    		return 1;
    	}
    	printf("x: %d, y: %d\n", InfCon.dwSize.X, InfCon.dwSize.Y);
    	InfCon.dwSize.X = 50;
    	InfCon.dwSize.Y = 50;
    	
    	if (!SetConsoleScreenBufferSize(hCon, InfCon.dwSize))
    	{
    		printf("SetConsoleScreenBufferSize %d\n", GetLastError());
    		return 1;
    	}
    	
    	return 0;
    }
    but the console don't change and also get an error:

    Code:
    C:\Users\Quasar999\Documents\Source\WIN32\Win32_0016\ConsoleApplication1\x64\Release>ConsoleApplication1
    x: 120, y: 9001
    SetConsoleScreenBufferSize 87
    
    C:\Users\Quasar999\Documents\Source\WIN32\Win32_0016\ConsoleApplication1\x64\Release>
    According to the documentation this error means:
    ERROR_INVALID_PARAMETER

    87 (0x57)

    The parameter is incorrect.
    Did you check whether the HANDLE hCon is correct/valid?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2022
    Posts
    11

    Re: Aspect and format of the console

    Quote Originally Posted by VictorN View Post
    According to the documentation this error means:
    Did you check whether the HANDLE hCon is correct/valid?
    Changed the code, but output is the same:



    #include <windows.h>
    #include <stdio.h>

    int main()
    {
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hCon == INVALID_HANDLE_VALUE) {
    printf("GetStdHandle Error");
    return 1;
    }
    CONSOLE_SCREEN_BUFFER_INFO InfCon;
    GetConsoleScreenBufferInfo(hCon, &InfCon);
    if (!GetConsoleScreenBufferInfo(hCon, &InfCon))
    {
    printf("GetConsoleScreenBufferInfo failed %d\n", GetLastError());
    return 1;
    }
    printf("x: %d, y: %d\n", InfCon.dwSize.X, InfCon.dwSize.Y);
    InfCon.dwSize.X = 50;
    InfCon.dwSize.Y = 50;

    if (!SetConsoleScreenBufferSize(hCon, InfCon.dwSize))
    {
    printf("SetConsoleScreenBufferSize %d\n", GetLastError());
    return 1;
    }

    return 0;
    }

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,630

    Re: Aspect and format of the console

    You can't change the buffer size to be less than the size of the console window. Note that there are two sizes - the size of the buffer and the size of the window. The window is a 'picture' of the buffer where the window 'moves' over the buffer. This is how scrolling is done. The window size can't be larger than the buffer. The buffer can't be smaller than the window.

    It looks like you want to change the size of the console window. Consider:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main() {
    	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	if (hCon == INVALID_HANDLE_VALUE) {
    		printf("GetStdHandle Error");
    		return 1;
    	}
    
    	CONSOLE_SCREEN_BUFFER_INFO InfCon;
    
    	if (!GetConsoleScreenBufferInfo(hCon, &InfCon)) {
    		printf("GetConsoleScreenBufferInfo failed %d\n", GetLastError());
    		return 1;
    	}
    
    	printf("x: %d, y: %d\n", InfCon.srWindow.Right - InfCon.srWindow.Left + 1, InfCon.srWindow.Bottom - InfCon.srWindow.Top + 1);
    	InfCon.srWindow.Right = InfCon.srWindow.Left + 50;
    	InfCon.srWindow.Bottom = InfCon.srWindow.Top + 30;
    
    	if (!SetConsoleWindowInfo(hCon, true, &InfCon.srWindow)) {
    		printf("SetConsoleWindowInfo %d\n", GetLastError());
    		return 1;
    	}
    }
    This changes the window size for me. Note that if error 87 is returned then the window has been given an invalid size.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.2.4)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured