Aspect and format of the console





















  1. 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 %dn", GetLastError());
    		return 1;
    	}
    	printf("x: %d, y: %dn", InfCon.dwSize.X, InfCon.dwSize.Y);
    	InfCon.dwSize.X = 50;
    	InfCon.dwSize.Y = 50;
    	
    	if (!SetConsoleScreenBufferSize(hCon, InfCon.dwSize))
    	{
    		printf("SetConsoleScreenBufferSize %dn", GetLastError());
    		return 1;
    	}
    	
    	return 0;
    }

    but the console don’t change and also get an error:

    Code:

    C:UsersQuasar999DocumentsSourceWIN32Win32_0016ConsoleApplication1x64Release>ConsoleApplication1
    x: 120, y: 9001
    SetConsoleScreenBufferSize 87
    
    C:UsersQuasar999DocumentsSourceWIN32Win32_0016ConsoleApplication1x64Release>






  2. Re: Aspect and format of the console

    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 %dn", GetLastError());
    		return 1;
    	}
    	printf("x: %d, y: %dn", InfCon.dwSize.X, InfCon.dwSize.Y);
    	InfCon.dwSize.X = 50;
    	InfCon.dwSize.Y = 50;
    	
    	if (!SetConsoleScreenBufferSize(hCon, InfCon.dwSize))
    	{
    		printf("SetConsoleScreenBufferSize %dn", GetLastError());
    		return 1;
    	}
    	
    	return 0;
    }

    but the console don’t change and also get an error:

    Code:

    C:UsersQuasar999DocumentsSourceWIN32Win32_0016ConsoleApplication1x64Release>ConsoleApplication1
    x: 120, y: 9001
    SetConsoleScreenBufferSize 87
    
    C:UsersQuasar999DocumentsSourceWIN32Win32_0016ConsoleApplication1x64Release>

    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. 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
    #include

    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 %dn”, GetLastError());
    return 1;
    }
    printf(“x: %d, y: %dn”, InfCon.dwSize.X, InfCon.dwSize.Y);
    InfCon.dwSize.X = 50;
    InfCon.dwSize.Y = 50;

    if (!SetConsoleScreenBufferSize(hCon, InfCon.dwSize))
    {
    printf(“SetConsoleScreenBufferSize %dn”, GetLastError());
    return 1;
    }

    return 0;
    }






  4. 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 %dn", GetLastError());
    		return 1;
    	}
    
    	printf("x: %d, y: %dn", 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 %dn", 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 acknowledgment. Also note that I only provide advice and guidance via the forums – and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.1.5)


















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













.

Leave a Comment