Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29279 Discussions

How do I reinitialize CONSOLE window?

WSinc
New Contributor I
962 Views
I know hw to do that with a graphics window, but can that also be done with a CONSOLE window?

Either a way to clear the screen, or to start rewritng it from the upper left hand corner would work.

I could not find any libray routines that do this. Is that even possible?
0 Kudos
5 Replies
Paul_Curtis
Valued Contributor I
962 Views
A window is a window, the concept of a "console" window containing text characters is a fiction maintained by some application program, not a property of the Windows opsys. That said, a window is cleared simply by invalidating its client rectangle, which will send a repaint message to its proc function. To do this, you only need the window's handle; here is a sample F90 wrapper routine:

[bash]SUBROUTINE repaint (hwnd, bkg_erase) IMPLICIT NONE INTEGER(HANDLE), INTENT(IN) :: hwnd LOGICAL, INTENT(IN), OPTIONAL :: bkg_erase INTEGER :: rval TYPE(T_RECT) :: rect LOGICAL :: erase IF (hwnd > 0) THEN erase = .FALSE. IF (PRESENT(bkg_erase)) erase = bkg_erase rval = GetClientRect (hwnd, rect) IF (erase) THEN rval = InvalidateRect (hwnd, rect, TRUE) ELSE rval = InvalidateRect (hwnd, rect, FALSE) END IF END IF END SUBROUTINE repaint [/bash]
and note that TRUE and FALSE in the above are Windows constants (from IFWINTY.F90), not the same as the standard Fortran booleans .TRUE. and .FALSE.
0 Kudos
IanH
Honored Contributor III
962 Views
Quoting Paul Curtis
A window is a window, the concept of a "console" window containing text characters is a fiction maintained by some application program, not a property of the Windows opsys.

The operating system provides consoles for applications to use. Older versions of the operating system allowed those consoles to be quite special, in that they could be run fullscreen. The windows component that provides console services still has "magic powers" that are beyond the capability of a normal executable.

Invalidating the window is just going to redraw the contents of the console buffer.

Here's a program that shows one way of clearing the console, adapted from Microsoft's C examples for the same task. It's ifort/windows specific and obliterates the entire screen buffer, not just what's visible.

[fortran]PROGRAM ClearConsole IMPLICIT NONE !***************************************************************************** ! For testing - we need some text. PRINT "(A)", 'Mary had a little lamb, its fleece was white as snow' PRINT "(A)", 'and every where that Mary went, the lamb was sure to go.' PRINT "(A)", 'It followed her to school one day, & &which was against the rules.' PRINT "(A)", 'It made the children laugh and play, to see a lamb at school.' PRINT "(A)", 'Mary had a little lamb, her father shot it dead.' PRINT "(A)", 'Now Mary takes that lamb to school, & &between two chunks of bread.' ! Now send our poetry into oblivion. CALL cls CONTAINS SUBROUTINE cls USE KERNEL32 !--------------------------------------------------------------------------- ! Local constants ! Coordinates of the top left hand corner of the console. TYPE(T_COORD), PARAMETER :: top_left_coords = T_COORD(0, 0) !--------------------------------------------------------------------------- ! Local variables INTEGER(HANDLE) :: stdout ! Console output handle. INTEGER(BOOL) :: brc ! BOOL API result. ! Information about the screen buffer for the console. TYPE(T_CONSOLE_SCREEN_BUFFER_INFO) :: screen_buffer_info ! Number of characters in the screen buffer. INTEGER(DWORD) :: buffer_chars ! Number of characters written to the console. INTEGER(DWORD) :: chars_written !*************************************************************************** ! Get the output handle to the console, assuming that we are a console ! program and the output handle hasn't been redirected. ! ! -11 is the magic number for standard output. stdout = GetStdHandle(-11_DWORD) IF (stdout == 0_HANDLE) STOP 'No output handle!' ! Get the dimensions of the screen buffer. brc = GetConsoleScreenBufferInfo(stdout, screen_buffer_info) IF (brc == 0_BOOL) STOP 'Couldn''t get the screen buffer''s info!' ! Calculate number of characters in the buffer. The kind ! conversion is there to avoid overflow. buffer_chars = INT(screen_buffer_info%dwSize%X, DWORD) & * INT(screen_buffer_info%dwSize%Y, DWORD) ! Output that many blanks. brc = FillConsoleOutputCharacter( stdout, ' ', buffer_chars, & top_left_coords, LOC(chars_written) ) IF (brc == 0_BOOL) STOP 'Couldn''t fill the console with chars!' ! Someone before us might have been playing games with colours etc - so ! reset the display attributes as well. brc = FillConsoleOutputAttribute( stdout, screen_buffer_info%wAttributes, & buffer_chars, top_left_coords, LOC(chars_written) ) IF (brc == 0_BOOL) STOP 'Couldn''t fill the console with attrs!' ! Move cursor up to the top left. brc = SetConsoleCursorPosition(stdout, top_left_coords) IF (brc == 0_BOOL) STOP 'Couldn''t move position up to the top left!' END SUBROUTINE cls END PROGRAM ClearConsole [/fortran]

0 Kudos
joerg_kuthe
Novice
962 Views
I hope I may point you to a library of console functions for Intel Visual Fortran my company is offering:
qtConsole
http://www.qtsoftware.de/oxShop/en/Libraries-Tools/for-GUI-Design/qtConsole.html
qtConsole functions

Among other functions qtConsole offers a function for clearing the console window. You use it as follows:
USE qtConsole
!...
iRet = qtConClearConsole() ! clears the console and positions the cusor at the top left corner

I would be happy, if this is useful for you.

Kind regards,

Joerg Kuthe
www.qtsoftware.com
0 Kudos
WSinc
New Contributor I
962 Views
Actually, I was able to just reposition the curson at the top left, rather than clearing the whole window first.
Then just rewrite the window lines over the old ones.

That was a lot more efficient.

Thanks for all the suggestions ! ! !
0 Kudos
dboggs
New Contributor I
962 Views

How do you "reposition the cursor at the top left"?

0 Kudos
Reply