CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [RESOLVED] about console size

    the console have 2 types of size, right?
    i belive that is the window size and the container size. the window size i can do it, but how can i change the container(i think that you call it buffer or something) size?

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

    Re: about console size

    There are 2 sizes associated with a console. The size of the window and the size of the buffer. The buffer can be greater than the size of the window, but the window cannot be greater than the buffer. Basically you write to the buffer and the window displays that portion of the buffer over which it is placed.

    To set buffer size, use SetConsoleScreenBufferSize(...)

    To get buffer size, use something like

    Code:
    DWORD GetBufferSize(HANDLE sout, COORD& size)
    {
    CONSOLE_SCREEN_BUFFER_INFO	info;
    
    	if (!GetConsoleScreenBufferInfo(sout, &info)) {
    		return (GetLastError());
    	}
    
    	size.X = info.dwSize.X;
    	size.Y = info.dwSize.Y;
    	return (0);
    }
    To get windows size, you need something like this

    Code:
    DWORD GetConsoleSize(HANDLE sout, COORD& size)
    {
    CONSOLE_FONT_INFO	cfinfo;
    
    COORD		fsize;
    
    WINDOWINFO	winfo;
    
    	if (!GetCurrentConsoleFont(sout, FALSE, &cfinfo)) {
    		return (GetLastError());
    	}
    
    	fsize = GetConsoleFontSize(sout, cfinfo.nFont);
    	if (!fsize.X && !fsize.Y) {
    		return (GetLastError());
    	}
    
    	winfo.cbSize = sizeof(WINDOWINFO);
    	if (!GetWindowInfo(GetConsoleWindow(), &winfo)) {
    		return (GetLastError());
    	}
    
    	size.Y = (SHORT)((winfo.rcClient.bottom - winfo.rcClient.top) / fsize.Y);
    	size.X = (SHORT)((winfo.rcClient.right - winfo.rcClient.left) / fsize.X);
    
    	return (0);
    }

    To set window size use something like this

    Code:
    DWORD SetConSize(HANDLE sout, COORD size)
    {
    CONSOLE_SCREEN_BUFFER_INFO	info;
    
    	if (!GetConsoleScreenBufferInfo(sout, &info)) {
    		return (GetLastError());
    	}
    	
    	info.srWindow.Top = 0;
    	info.srWindow.Bottom = size.Y - 1;
    	info.srWindow.Left = 0;
    	info.srWindow.Right = size.X - 1;
    
    	return (!SetConsoleWindowInfo(sout, TRUE, &info.srWindow) ? GetLastError() : 0);
    }
    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.6.5)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by 2kaud View Post
    There are 2 sizes associated with a console. The size of the window and the size of the buffer. The buffer can be greater than the size of the window, but the window cannot be greater than the buffer. Basically you write to the buffer and the window displays that portion of the buffer over which it is placed.

    To set buffer size, use SetConsoleScreenBufferSize(...)

    To get buffer size, use something like

    Code:
    DWORD GetBufferSize(HANDLE sout, COORD& size)
    {
    CONSOLE_SCREEN_BUFFER_INFO	info;
    
    	if (!GetConsoleScreenBufferInfo(sout, &info)) {
    		return (GetLastError());
    	}
    
    	size.X = info.dwSize.X;
    	size.Y = info.dwSize.Y;
    	return (0);
    }
    To get windows size, you need something like this

    Code:
    DWORD GetConsoleSize(HANDLE sout, COORD& size)
    {
    CONSOLE_FONT_INFO	cfinfo;
    
    COORD		fsize;
    
    WINDOWINFO	winfo;
    
    	if (!GetCurrentConsoleFont(sout, FALSE, &cfinfo)) {
    		return (GetLastError());
    	}
    
    	fsize = GetConsoleFontSize(sout, cfinfo.nFont);
    	if (!fsize.X && !fsize.Y) {
    		return (GetLastError());
    	}
    
    	winfo.cbSize = sizeof(WINDOWINFO);
    	if (!GetWindowInfo(GetConsoleWindow(), &winfo)) {
    		return (GetLastError());
    	}
    
    	size.Y = (SHORT)((winfo.rcClient.bottom - winfo.rcClient.top) / fsize.Y);
    	size.X = (SHORT)((winfo.rcClient.right - winfo.rcClient.left) / fsize.X);
    
    	return (0);
    }

    To set window size use something like this

    Code:
    DWORD SetConSize(HANDLE sout, COORD size)
    {
    CONSOLE_SCREEN_BUFFER_INFO	info;
    
    	if (!GetConsoleScreenBufferInfo(sout, &info)) {
    		return (GetLastError());
    	}
    	
    	info.srWindow.Top = 0;
    	info.srWindow.Bottom = size.Y - 1;
    	info.srWindow.Left = 0;
    	info.srWindow.Right = size.X - 1;
    
    	return (!SetConsoleWindowInfo(sout, TRUE, &info.srWindow) ? GetLastError() : 0);
    }
    i have 1 question:
    "The buffer can be greater than the size of the window, but the window cannot be greater than the buffer."
    is that why the vertical scrollbar is showed?

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

    Re: about console size

    Quote Originally Posted by Cambalinho View Post
    i have 1 question:
    "The buffer can be greater than the size of the window, but the window cannot be greater than the buffer."
    is that why the vertical scrollbar is showed?
    Yes, the window just shows a part of the buffer if the size of the buffer is greater than the size of the window. Depending upon how the sizes of the buffer and window have been set, you can get a horizontal scrollbar as well. If you don't want scroll bars then set the window size and the buffer size to be the same.

    Note when setting buffer sizes that the rules apply! You cannot set a window greater than the buffer size and then set the buffer! The buffer needs to be set first and then the window size. Also if you are reducing the buffer size to be less than the current window size, then reduce the window size first and then the buffer 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.6.5)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    why the scrollbar isn't hided?
    Code:
    CONSOLE_SCREEN_BUFFER_INFO	info;
    		RECT WindowRect;
    		GetWindowRect(ConsoleHandle,&WindowRect);
    		info.srWindow.Top =WindowRect.top;
    		info.srWindow.Left= WindowRect.left;
    		info.srWindow.Bottom = WindowRect.bottom;
    		info.srWindow.Right = WindowRect.right ;
    		SetConsoleWindowInfo(ConsoleDC, TRUE, &info.srWindow);
    seems that these code is ignored

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: about console size

    What does this GetWindowRect return? TRUE or FALSE?
    The same question - about GetWindowRect.
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by VictorN View Post
    What does this GetWindowRect return? TRUE or FALSE?
    The same question - about GetWindowRect.
    the GetWindowRect() gives 1(true), the SetConsoleWindowInfo() gives me 0(false)

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

    Re: about console size

    Quote Originally Posted by Cambalinho View Post
    why the scrollbar isn't hided?
    Code:
    CONSOLE_SCREEN_BUFFER_INFO	info;
    		RECT WindowRect;
    		GetWindowRect(ConsoleHandle,&WindowRect);
    		info.srWindow.Top =WindowRect.top;
    		info.srWindow.Left= WindowRect.left;
    		info.srWindow.Bottom = WindowRect.bottom;
    		info.srWindow.Right = WindowRect.right ;
    		SetConsoleWindowInfo(ConsoleDC, TRUE, &info.srWindow);
    seems that these code is ignored
    What are you trying to accomplish? You're getting the size of the console window then you try to set the size and position of the console window to what it is already??
    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.6.5)

  9. #9
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by 2kaud View Post
    What are you trying to accomplish? You're getting the size of the console window then you try to set the size and position of the console window to what it is already??
    buffer size = window size

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

    Re: about console size

    Quote Originally Posted by Cambalinho View Post
    buffer size = window size
    Then using the functions from above,

    Code:
    COORD    consize;
    
    GetConsoleSize(consoleDC, consize);
    SetConsoleScreenBufferSize(consoleDC, consize);
    Note you should test for errors.
    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.6.5)

  11. #11
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by 2kaud View Post
    Then using the functions from above,

    Code:
    COORD    consize;
    
    GetConsoleSize(consoleDC, consize);
    SetConsoleScreenBufferSize(consoleDC, consize);
    Note you should test for errors.
    sorry i don't have GetConsoleSize() what dll or library is need?

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: about console size

    Quote Originally Posted by Cambalinho View Post
    the GetWindowRect() gives 1(true), the SetConsoleWindowInfo() gives me 0(false)
    Then how about the call of GetLastError to obtain the extended error info?
    Victor Nijegorodov

  13. #13
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    sorry about that.. you shared. but if the windows and buffer sizes are the same, why the scrollbar stills there?

  14. #14
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about console size

    Quote Originally Posted by VictorN View Post
    Then how about the call of GetLastError to obtain the extended error info?
    number 6.

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

    Re: about console size

    Quote Originally Posted by Cambalinho View Post
    sorry i don't have GetConsoleSize() what dll or library is need?
    I gave you the code for this in post #2.
    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.6.5)

Page 1 of 3 123 LastLast

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