c++ - How resize window Console?

Started by
3 comments, last by cambalinho 8 years, 6 months ago

how can i resize window Console?

i did these code:


void SetWindowSize(COORD newsize)
    {
        PCONSOLE_SCREEN_BUFFER_INFOEX consolesize;
        GetConsoleScreenBufferInfoEx(hConsole,consolesize);

        COORD c;
        consolesize->srWindow.Right  = newsize.X+ consolesize->srWindow.Left;
        consolesize->srWindow.Bottom = newsize.Y+ consolesize->srWindow.Top;
        SetConsoleWindowInfo(hConsole, TRUE, &consolesize->srWindow);
        PCONSOLE_FONT_INFO consolefont;
        GetCurrentConsoleFont(hConsole, FALSE,consolefont);
        GetConsoleFontSize(hConsole,consolefont->nFont);
        c.X =(consolesize->srWindow.Right-consolesize->srWindow.Left)/consolefont->dwFontSize.X;
        c.Y =(consolesize->srWindow.Bottom-consolesize->srWindow.Top)/consolefont->dwFontSize.Y;
        DebugText("hello size " + to_string(c.X) + "  " + to_string(c.Y));
        SetConsoleScreenBufferSize( hConsole, c );
    }

but i get a memory leak, or i see the scrollbars.

what i'm doing wrong?

Advertisement
According to the MSDN documentation for SetConsoleWindowInfo:

"The function fails if the specified window rectangle extends beyond the boundaries of the console screen buffer. This means that the Top and Left members of the lpConsoleWindow rectangle (or the calculated top and left coordinates, if bAbsolute is FALSE) cannot be less than zero. Similarly, the Bottom and Right members (or the calculated bottom and right coordinates) cannot be greater than (screen buffer height – 1) and (screen buffer width – 1), respectively. The function also fails if the Right member (or calculated right coordinate) is less than or equal to the Left member (or calculated left coordinate) or if the Bottom member (or calculated bottom coordinate) is less than or equal to the Top member (or calculated top coordinate)."

This, to me, seems to state that you must first increase the screen buffer size and then set the window size. Are you checking the return values? Which function is failing, if any?

the SetConsoleWindowInfo() is give me a memory leak

but seems that depends on position

ok.. now no memory leak:


void SetWindowSize(COORD newsize)
    {
        CONSOLE_SCREEN_BUFFER_INFOEX consolesize;//see these type name
        consolesize.cbSize=sizeof(consolesize);//i forgot these
        GetConsoleScreenBufferInfoEx(hConsole,&consolesize);
        COORD c;
        consolesize.srWindow.Right  = newsize.X-1;
        consolesize.srWindow.Bottom = newsize.Y-1;
        CONSOLE_FONT_INFO consolefont={0} ;//see these type name
        GetCurrentConsoleFont(hConsole, FALSE,&consolefont);
        GetConsoleFontSize(hConsole,consolefont.nFont);
        c.X =(consolesize.srWindow.Right)/consolefont.dwFontSize.X;
        c.Y =(consolesize.srWindow.Bottom)/consolefont.dwFontSize.Y;
        SetConsoleScreenBufferSize( hConsole, c );
        SetConsoleWindowInfo(hConsole, FALSE, &consolesize.srWindow);
    }

i have 1 problem on c.X and c.Y calculations, because the vertical scrollbar continues visible :(

can you advice me?

This topic is closed to new replies.

Advertisement