Are you trying to change the size of a console window in your code?
Printable View
Are you trying to change the size of a console window in your code?
OK. Is it a console that your program has created or a console in which your program is running?
OK. You should use the specific console api functions for dealing with a console rather than the general windows ones. I'll knock you up a test program to allow you to resize/move a console program and post it for you shortly.
You can't just change the size of a console window like you can other windows. You need to use the special console api functions to change the console buffer size and the console window size.
This will allow you to set the console size in terms of number of chars width and height up to max allowed for your monitor. It will also centre the re-sized window. Note there is a problem getting console window info just after the console size has been changed. That is why the Sleep is needed. If the console doesn't centre properly try increasing this value from 5.
Code:#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
//These values are in terms of characters NOT screen pixels
#define CON_HEIGHT 40
#define CON_WIDTH 120
int main()
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
if (hcon == INVALID_HANDLE_VALUE) {
puts("Not a console!");
return (1);
}
COORD max;
COORD co;
co.X = CON_WIDTH;
co.Y = CON_HEIGHT;
max = GetLargestConsoleWindowSize(hcon);
if (max.X == 0 || max.Y == 0) {
printf("Bad Large: %i\n", GetLastError());
return (2);
}
if (co.X > max.X) co.X = max.X;
if (co.Y > max.Y) co.Y = max.Y;
if (!SetConsoleScreenBufferSize(hcon, co)) {
printf("Bad buffer: %i\n", GetLastError());
return (3);
}
SMALL_RECT rect;
rect.Top = 0;
rect.Bottom = co.Y - 1;
rect.Left = 0;
rect.Right = co.X - 1;
if (!SetConsoleWindowInfo(hcon, TRUE, &rect)) {
printf("Bad set: %i\n", GetLastError());
return (4);
}
Sleep(5); //THIS IS NOT GOOD BUT NEED TO GIVE THE SYSTEM TIME TO CHANGE CONSOLE SIZE
//BEFORE GET WINDOW RECT FOR CONSOLE. IF DO WITHOUT DELAY CAN GET WRONG SIZE
HWND hwndScreen = GetDesktopWindow(),
conwd = GetConsoleWindow();
RECT rectScreen,
rectcon;
int ConsolePosX,
ConsolePosY;
GetWindowRect(hwndScreen, &rectScreen);
GetWindowRect(conwd, &rectcon);
ConsolePosX = (rectScreen.right - rectScreen.left - (rectcon.right - rectcon.left)) >> 1;
ConsolePosY = (rectScreen.bottom - rectScreen.top - (rectcon.bottom - rectcon.top)) >> 1;
SetWindowPos(conwd, HWND_NOTOPMOST, ConsolePosX, ConsolePosY, 0, 0, SWP_NOOWNERZORDER | SWP_NOSIZE);
system("pause");
return (0);
}
Yes. Just set co.X and co.Y to whatever number of chars you want. I just used #define for the test program.
Code://Set the size of the console width and height in chars here
co.X = CON_WIDTH;
co.Y = CON_HEIGHT;
another question: what you think about system() funtion?
i have found 1 way for the width, but the windows give me an error and then close my aplication:(
Code:int SetWindow(HWND hwnwindow,int PosX, int PosY, int Width, int Height)
{
SetWindowPos(hwnwindow,HWND_NOTOPMOST,PosX,PosY,Width,Height,SWP_NOOWNERZORDER);
char *command="";
sprintf(command,"mode CON: COLS=%d",Width);
system(command);
return 0;
}
what you can tell me?Code:system("mode CON: COLS=1000")
mode CON: works with chars not pixels. So if you want a screen 120 chars wide by 40 deep then use
Code:system("mode CON: cols=120 lines=40");
thanks.. but how can i convert chars to pixels?
can you give me a link for the system() commands?
is possivel use for positions too?
i have 1 question: how can i calculate for put the window in center o f screen?
screen.width/2 - window.width/2
screen.height/2 - window.height/2
it's correct?
Here you are. The SetConsoleSize() function will set the console size to the specified width and height in pixels and will centre the console. It does work with a width of 1000!
Code:#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
//These values are in terms of pixels
#define CON_HEIGHT 500
#define CON_WIDTH 1000
int SetConsoleSize(int width, int height);
int main()
{
SetConsoleSize(CON_WIDTH, CON_HEIGHT);
system("pause");
return (0);
}
//Set console size in pixels and centres console
int SetConsoleSize(int width, int height)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
if (hcon == INVALID_HANDLE_VALUE) {
return ((int)hcon);
}
CONSOLE_FONT_INFO finfo;
COORD fsize;
if (!GetCurrentConsoleFont(hcon, FALSE, &finfo)) {
return (GetLastError());
}
fsize = GetConsoleFontSize(hcon, finfo.nFont);
if (fsize.X == 0 || fsize.Y == 0) {
return (GetLastError());
}
COORD max;
COORD co;
co.X = width / fsize.X;
co.Y = height / fsize.Y;
max = GetLargestConsoleWindowSize(hcon);
if (max.X == 0 || max.Y == 0) {
return (GetLastError());
}
if (co.X > max.X) co.X = max.X;
if (co.Y > max.Y) co.Y = max.Y;
if (!SetConsoleScreenBufferSize(hcon, co)) {
return (GetLastError());
}
SMALL_RECT rect;
rect.Top = 0;
rect.Bottom = co.Y - 1;
rect.Left = 0;
rect.Right = co.X - 1;
if (!SetConsoleWindowInfo(hcon, TRUE, &rect)) {
return (GetLastError());
}
Sleep(5); //THIS IS NOT GOOD BUT NEED TO GIVE THE SYSTEM TIME TO CHANGE CONSOLE SIZE
//BEFORE GET WINDOW RECT FOR CONSOLE. IF DO WITHOUT DELAY CAN GET WRONG SIZE
HWND hwndScreen = GetDesktopWindow(),
conwd = GetConsoleWindow();
RECT rectScreen,
rectcon;
int ConsolePosX,
ConsolePosY;
GetWindowRect(hwndScreen, &rectScreen);
GetWindowRect(conwd, &rectcon);
ConsolePosX = (rectScreen.right - rectScreen.left - (rectcon.right - rectcon.left)) >> 1;
ConsolePosY = (rectScreen.bottom - rectScreen.top - (rectcon.bottom - rectcon.top)) >> 1;
SetWindowPos(conwd, HWND_NOTOPMOST, ConsolePosX, ConsolePosY, 0, 0, SWP_NOOWNERZORDER | SWP_NOSIZE);
return (0);
}
sorry your code isn't compatible with Visual C++ 6:(
"Deleting intermediate files and output files for project 'CatchDiamonds - Win32 Debug'.
--------------------Configuration: CatchDiamonds - Win32 Debug--------------------
Compiling resources...
Compiling...
CatchDiamonds.cpp
c:\users\joaquim\documents\visual c 98\catchdiamonds\catchdiamonds.cpp(405) : error C2065: 'CONSOLE_FONT_INFO' : undeclared identifier
c:\users\joaquim\documents\visual c 98\catchdiamonds\catchdiamonds.cpp(405) : error C2146: syntax error : missing ';' before identifier 'finfo'
c:\users\joaquim\documents\visual c 98\catchdiamonds\catchdiamonds.cpp(405) : error C2065: 'finfo' : undeclared identifier
c:\users\joaquim\documents\visual c 98\catchdiamonds\catchdiamonds.cpp(408) : error C2065: 'GetCurrentConsoleFont' : undeclared identifier
c:\users\joaquim\documents\visual c 98\catchdiamonds\catchdiamonds.cpp(412) : error C2065: 'GetConsoleFontSize' : undeclared identifier
c:\users\joaquim\documents\visual c 98\catchdiamonds\catchdiamonds.cpp(412) : error C2228: left of '.nFont' must have class/struct/union type
c:\users\joaquim\documents\visual c 98\catchdiamonds\catchdiamonds.cpp(450) : error C2065: 'GetConsoleWindow' : undeclared identifier
c:\users\joaquim\documents\visual c 98\catchdiamonds\catchdiamonds.cpp(450) : error C2440: 'initializing' : cannot convert from 'int' to 'struct HWND__ *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
CatchDiamonds.exe - 8 error(s), 0 warning(s)"
thanks for all
It occured to me later that others might find this a useful routine. So I've slightly enhanced the function so that it can now take either pixels or char numbers for width and height. It also will now allow the console window to be down-sized as well as up-sized.
Here's the revised code
Code:#define _WIN32_WINNT 0x0500
#include <windows.h>
int SetConsoleSize(int width, int height, bool pixel = true);
int main()
{
//Tests for function
SetConsoleSize(1000, 500);
system("pause");
SetConsoleSize(200, 200);
system("pause");
SetConsoleSize(600, 400);
system("pause");
return (0);
}
//Set console size in pixels and centres console
int SetConsoleSize(int width, int height, bool pixel)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
if (hcon == INVALID_HANDLE_VALUE) {
return ((int)hcon);
}
COORD co;
//Convert pixel to chars if specified
if (pixel == true) {
CONSOLE_FONT_INFO finfo;
COORD fsize;
if (!GetCurrentConsoleFont(hcon, FALSE, &finfo)) return (GetLastError());
fsize = GetConsoleFontSize(hcon, finfo.nFont);
if (fsize.X == 0 || fsize.Y == 0) return (GetLastError());
co.X = width / fsize.X;
co.Y = height / fsize.Y;
} else {
co.X = width;
co.Y = height;
}
//Can not have a console window larger than the max allowed
COORD max = GetLargestConsoleWindowSize(hcon);
if (max.X == 0 || max.Y == 0) return (GetLastError());
if (co.X > max.X) co.X = max.X;
if (co.Y > max.Y) co.Y = max.Y;
CONSOLE_SCREEN_BUFFER_INFO bufInfo;
//Console window must be no larger than the screen buffer and screen buffer no smaller than console window
if (!GetConsoleScreenBufferInfo(hcon, &bufInfo)) return (GetLastError());
bool change = false;
if (co.X > bufInfo.dwSize.X) {
bufInfo.dwSize.X = co.X;
change = true;
}
if (co.Y > bufInfo.dwSize.Y) {
bufInfo.dwSize.Y = co.Y;
change = true;
}
if ((change == true) && !SetConsoleScreenBufferSize(hcon, bufInfo.dwSize)) return (GetLastError());
SMALL_RECT rect;
//Change console window size
rect.Top = 0;
rect.Bottom = co.Y - 1;
rect.Left = 0;
rect.Right = co.X - 1;
if (!SetConsoleWindowInfo(hcon, TRUE, &rect)) return (GetLastError());
Sleep(5); //THIS IS NOT GOOD BUT NEED TO GIVE THE SYSTEM TIME TO CHANGE CONSOLE SIZE
//BEFORE GET WINDOW RECT FOR CONSOLE. IF DO WITHOUT DELAY CAN GET WRONG SIZE
HWND hwndScreen = GetDesktopWindow(),
conwd = GetConsoleWindow();
RECT rectScreen,
rectcon;
int ConsolePosX,
ConsolePosY;
if (!GetWindowRect(hwndScreen, &rectScreen)) return (GetLastError());
if (!GetWindowRect(conwd, &rectcon)) return (GetLastError());
//Position resized console window in middle of desktop
ConsolePosX = (rectScreen.right - rectScreen.left - (rectcon.right - rectcon.left)) >> 1;
ConsolePosY = (rectScreen.bottom - rectScreen.top - (rectcon.bottom - rectcon.top)) >> 1;
if (!SetWindowPos(conwd, HWND_NOTOPMOST, ConsolePosX, ConsolePosY, 0, 0, SWP_NOOWNERZORDER | SWP_NOSIZE)) return (GetLastError());
return (0);
}