Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
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);
}
sorry the errors continue:(
seems that i don't have the GetConsoleFontSize() and GetConsoleWindow() and CONSOLE_FONT_INFO type:(
Re: window on center of screen and fullscreen(on\off)
All I can say is that it compiles cleanly for me (just 1 expected warning). What compiler are you using? This compiled fine with VS2003. According to MSDN, the minimum Windows OS version supported is XP for GetConsoleFontSize but GetConsoleWindow is supported for Windows 2000.
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
All I can say is that it compiles cleanly for me (just 1 expected warning). What compiler are you using? This compiled fine with VS2003. According to MSDN, the minimum Windows OS version supported is XP for GetConsoleFontSize but GetConsoleWindow is supported for Windows 2000.
i use Windows 7 and Visual C++ 6;)
my function works now. but i have 1 question: how convert from pixels to character?
Code:
int SetWindow(HWND hwnwindow,int PosX, int PosY, int Width, int Height, BOOL Center)
{
//P = 7 * C + 5
if (Center==FALSE)
{
SetWindowPos(hwnwindow,HWND_NOTOPMOST,PosX,PosY,0,0,SWP_NOOWNERZORDER);
}
else
{
//resize the window
HWND hwndScreen;
RECT rectScreen;
int ConsolePosX;
int ConsolePosY;
hwndScreen=GetDesktopWindow ();
GetWindowRect(hwndScreen,&rectScreen);
ConsolePosX = (rectScreen.right/2 - Width/2);
ConsolePosY = (rectScreen.bottom/2 - Height/2);
SetWindowPos(hwnwindow,HWND_NOTOPMOST,ConsolePosX,ConsolePosY,0,0,SWP_NOOWNERZORDER);
}
Width=(Width-5)/7;
Height=(Height-5)/7;
char command[255];
sprintf(command,"mode CON: cols=%d lines=%d",Width,Height);
system(command);
return 0;
}
i use:
P = 7 * C + 5
(P -pixel; C - character)
but i don't know if is correct:(
can you advice me?
Re: window on center of screen and fullscreen(on\off)
Quote:
i use Windows 7 and Visual C++ 6
Not all of the console API functions used are supported in vc++6!
To convert from pixels to chars, you need to divide the width in pixels of the console by the width in pixels of the char and the same for the height. This is what the font functions are doing in my function. However, you don't have them in vc++6! To get the values you need: from the console window, click on the icon next to the window title, click properties then font. This will tell you the height/width of a console char in pixels. Note that if you change the font used in the console then you will also need to then change the numbers in your program. On my system, it is 8 wide and 12 high.
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
Cambalinho
seems that i don't have the GetConsoleFontSize() and GetConsoleWindow() and CONSOLE_FONT_INFO type:(
i use Windows 7 and Visual C++ 6;)
Quote:
Originally Posted by
2kaud
Not all of the console API functions used are supported in vc++6!
If your compiler does not support an API function, you can always use LoadLibrary() and GetProcAddress() to get the address of the function dynamically. The following test program shows how this is done. (I have used 2kaud's variable names where I could.)
Code:
#include <windows.h>
#include <stdio.h>
COORD MyGetFontSize(HANDLE hcon)
{
typedef struct _My_CONSOLE_FONT_INFO {
DWORD nFont;
COORD dwFontSize;
} My_CONSOLE_FONT_INFO;
typedef BOOL (__stdcall* get_current_console_font)(HANDLE, BOOL, My_CONSOLE_FONT_INFO*);
typedef COORD (__stdcall* get_console_font_size)(HANDLE, DWORD);
COORD fsize;
HMODULE library;
get_current_console_font address1;
BOOL success;
My_CONSOLE_FONT_INFO finfo;
get_console_font_size address2;
fsize.X = 0;
fsize.Y = 0;
library = LoadLibrary("KERNEL32.DLL");
if (library != NULL)
{
address1 = (get_current_console_font)(GetProcAddress(library, "GetCurrentConsoleFont"));
if (address1 != NULL)
{
success = (*address1)(hcon, FALSE, &finfo);
if (success)
{
address2 = (get_console_font_size)(GetProcAddress(library, "GetConsoleFontSize"));
if (address2 != NULL)
{
fsize = (*address2)(hcon, finfo.nFont);
}
}
}
}
return fsize;
}
HWND MyGetConsoleWindow()
{
typedef HWND (__stdcall* get_console_window)();
HWND conwd;
HMODULE library;
get_console_window address;
conwd = NULL;
library = LoadLibrary("KERNEL32.DLL");
if (library != NULL)
{
address = (get_console_window)(GetProcAddress(library, "GetConsoleWindow"));
if (address != NULL)
{
conwd = (*address)();
}
}
return conwd;
}
int main()
{
HANDLE hcon;
COORD fsize;
HWND conwd;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
fsize = MyGetFontSize(hcon);
printf("Font Width = %d\nFont Height = %d\n", fsize.X, fsize.Y);
conwd = MyGetConsoleWindow();
printf("Window Handle = %08X\n", conwd);
return 0;
}
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
Not all of the console API functions used are supported in vc++6!
They are supported. However, you need to install a contemporary Windows Platform SDK and direct your VC++6 Directories paths to correspondent PSDK folders.