I am using ncurses for a terminal application which should also be usable on windows but every time the user resizes the terminal everything is mashed. So I need a way to detect resizing and redraw.
Since Windows does not support signals that does not work. (At least SIGWINCH is not defined). Also KEY_RESIZE is not send when resizing. getmaxx and getmaxy always return the initial terminal size and do not update when the user resizes the window (the same for GetConsoleBufferInfo). Is there another way to get the current actual size of the terminal or check if it has been resized by the user or another way to prevent everything drawn in the terminal to be moshed when resized?
Edit: Like I said I already tried GetConsoleBufferInfo. It works with a normal terminal but stops working when I call initscr from ncurses.
void getConsoleSize(int *w, int *h){
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
*w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
*h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}
This code is what I use to detect console resize events. There are other ways, such as polling the event queue for an actual ms windows resize event. But in my experience, that method was unreliable and ultimately you need to check the new console size anyway...
... and so that necessity gave birth to this code. Your code must query function
check_console_window_resize_event()repeatedly somewhere in your main execution loop. If the function returnsTRUE, your console has a new size, else, no. It works by checking theCONSOLE_SCREEN_BUFFER_INFOstructuresrWindowmember which is the actual console rectangle size (not to be confused with console buffer size which can be much bigger, but never smaller), and comparing current dimension values againststaticstored dimension values.This code is compiled and tested by me today and the featured
Outputis actually copied from the console window the program ran in just minutes ago as I dragged the console window edges with the mouse:Output: