CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2019
    Posts
    53

    Resizing WinAPI Windows Console without text rearranging?

    Is there a way to prevent the text displayed in the Windows Console from rearranging when you resize the window?

    When you resize the window the text reformats itself to accommodate the new size.

    Are there any functions in the Windows API to prevent the text rearranging when you resize the window or when you minimize and then maximize it?

    Even with minimize and then maximize the text rearranges.

    Thanks!

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

    Re: Resizing WinAPI Windows Console without text rearranging?

    Maybe using CR LF in text?
    Victor Nijegorodov

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

    Re: Resizing WinAPI Windows Console without text rearranging?

    It will auto-adjust text in the console window between newlines (or begin newline/newline end) to suit the size of the window. This is a 'feature' of the console. If you have say a width of 80 with a line length of 100 then this will display as 80 chars on one line and 20 on the next. If you adjust the width to say 110, then this line will be displayed as one line of 100. AFAIK, the only way around this is to have lines that are no greater in length than the current width of the screen.

    How are you writing to the screen?
    Last edited by 2kaud; March 19th, 2021 at 09:09 AM.
    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)

  4. #4
    Join Date
    May 2019
    Posts
    53

    Re: Resizing WinAPI Windows Console without text rearranging?

    I write to the console as follows:

    I have an wchar_t* array of 3600 characters.

    The console has 120 characters per line and 30 lines (so that's 120 * 30 = 3600).

    I fill the character array with a mixture of letters, symbols and spaces as needed.

    I then write to the console using WriteConsoleOutputCharacter() function.

    I do it this way because it writes to the console very, very quickly and avoids the screen flickering associated with other ways of writing to the console.

    The problem is, if the user minimizes the console window, or resizes it, the text that I have carefully arranged is scattered all over the console, ruining the effect.

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

    Re: Resizing WinAPI Windows Console without text rearranging?

    Ah. You might be able to subclass the console window and trap the WM_SIZE message and ignore it et al. Not tried though.
    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)

  6. #6
    Join Date
    May 2019
    Posts
    53

    Re: Resizing WinAPI Windows Console without text rearranging?

    Quote Originally Posted by 2kaud View Post
    Ah. You might be able to subclass the console window and trap the WM_SIZE message and ignore it et al. Not tried though.
    I'm a beginner.

    What do you mean by "subclass the console"?

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

    Re: Resizing WinAPI Windows Console without text rearranging?

    Just looked into it. Appears you can't sub-class the windows console. Sorry.

    Have a look at:
    https://stackoverflow.com/questions/...le-window-in-c
    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)

  8. #8
    Join Date
    May 2019
    Posts
    53

    Re: Resizing WinAPI Windows Console without text rearranging?

    Quote Originally Posted by 2kaud View Post
    Just looked into it. Appears you can't sub-class the windows console. Sorry.

    Have a look at:
    https://stackoverflow.com/questions/...le-window-in-c
    Yes, I followed this guide. It's quite effective.

    I also didnt know you could use
    Code:
    ~WS_STYLENAME
    in the SetWindowLongA function to REMOVE WS_STYLE messages...

    Unfortunately, if the user minimizes the window, it still garbles the output.

    I can't really prevent the user minimizing the window, since that's extremely inconvenient for them.

    I'll keep looking around. Let me know if you find anything else..(!)

  9. #9
    Join Date
    May 2019
    Posts
    53

    Re: Resizing WinAPI Windows Console without text rearranging?

    SOLUTION


    The solution appears to be correct use of the function SetConsoleWindowInfo().

    By setting the dimensions of the console to the exact size of the buffer you don't trigger text realignment when you change the shape of the window.

    This seems to be the only way to work around the text-realignment feature of the Windows Console.

    Thanks for all your help..(!)

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