-
window on center of screen and fullscreen(on\off)
i have these code for put the window on center of screen:
Code:
HWND hwndScreen;
RECT rectScreen;
hwndScreen=GetDesktopWindow ();
GetWindowRect(hwndScreen,rectScreen);
SetWindowPos(Console.WindowHandle,NULL,(rectScreen.right\2)-(consolewidth\2),(rectScreen.bottom\2)-(consoleheight\2),500,500,SWP_NOOWNERZORDER);
Console.WindowHandle is the correct handle and consolewidth and consoleheight give me the actual size of the console window.
but why i get these errors:
"Deleting intermediate files and output files for project 'test - Win32 Debug'.
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
c:\test\test\test.cpp(44) : error C2664: 'GetWindowRect' : cannot convert parameter 2 from 'struct tagRECT' to 'struct tagRECT *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\test\test\test.cpp(46) : error C2017: illegal escape sequence
c:\test\test\test.cpp(46) : error C2143: syntax error : missing ')' before 'constant'
c:\test\test\test.cpp(46) : error C2017: illegal escape sequence
c:\test\test\test.cpp(46) : error C2017: illegal escape sequence
c:\test\test\test.cpp(46) : error C2017: illegal escape sequence
c:\test\test\test.cpp(46) : error C2059: syntax error : ')'
Error executing cl.exe.
test.exe - 7 error(s), 0 warning(s)"
???
-
Re: window on center of screen and fullscreen(on\off)
you need a '&' before the rectScreen because you want a pointer to the rect not the rect itself.
\2 is incorrect. division is done with /
if you have a basic background, then C/C++'s / will do what basic's \ does if the types are integers.
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
OReubens
you need a '&' before the rectScreen because you want a pointer to the rect not the rect itself.
\2 is incorrect. division is done with /
if you have a basic background, then C/C++'s / will do what basic's \ does if the types are integers.
Code:
HWND hwndScreen;
RECT rectScreen;
hwndScreen=GetDesktopWindow ();
GetWindowRect(hwndScreen,&rectScreen);
int ConsolePosX= &rectScreen.right/2 - consolewidth/2;
int ConsolePosY= &rectScreen.bottom/2 - consoleheight/2;
SetWindowPos(Console.WindowHandle,HWND_NOTOPMOST,ConsolePosX,ConsolePosY,500,500,SWP_NOOWNERZORDER);
but i have errors:(
"Deleting intermediate files and output files for project 'test - Win32 Debug'.
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
c:\test\test\test.cpp(45) : error C2296: '/' : illegal, left operand has type 'long *'
c:\test\test\test.cpp(45) : error C2296: '/' : illegal, left operand has type 'int (__cdecl *)(void)'
c:\test\test\test.cpp(46) : error C2296: '/' : illegal, left operand has type 'long *'
c:\test\test\test.cpp(46) : error C2296: '/' : illegal, left operand has type 'int (__cdecl *)(void)'
Error executing cl.exe.
test.exe - 4 error(s), 0 warning(s)"
-
Re: window on center of screen and fullscreen(on\off)
RECT is a structure. When you pass a structure variable to a function in C++, you need to pass a pointer/reference to that variable. GetWindowRect() takes two arguments, a window handle and a pointer to the RECT structure variable in which you want to store the window's dimensions. You get a pointer to the structure variable by using the & operator before the variable name.
Code:
HWND hwndScreen;
RECT rect;
hwndScreen = GetDesktopWindow ();
GetWindowRect(hwndScreen, &rectScreen);
Now to get the value of a member of the structure which you declared as a local variable, you do not use the & operator because you are not trying to get a pointer to the variable's member.
Code:
int ConsolePosX = rectScreen.right/2 - consolewidth/2;
int ConsolePosY = rectScreen.bottom/2 - consoleheight/2;
To improve your code's efficiency, when you divide an integer by 2 (or any power of 2), you could use the right shift operator >> instead of the less efficient division operator.
Code:
int ConsolePosX = (rectScreen.right - consolewidth) >> 1;
int ConsolePosY = (rectScreen.bottom - consoleheight) >> 1;
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
Coder Dave
RECT is a structure. When you pass a structure variable to a function in C++, you need to pass a pointer/reference to that variable.
GetWindowRect() takes two arguments, a window handle and a pointer to the
RECT structure variable in which you want to store the window's dimensions. You get a pointer to the structure variable by using the
& operator before the variable name.
Code:
HWND hwndScreen;
RECT rect;
hwndScreen = GetDesktopWindow ();
GetWindowRect(hwndScreen, &rectScreen);
Now to get the value of a member of the structure which you declared as a local variable, you do not use the
& operator because you are not trying to get a pointer to the variable's member.
Code:
int ConsolePosX = rectScreen.right/2 - consolewidth/2;
int ConsolePosY = rectScreen.bottom/2 - consoleheight/2;
To improve your code's efficiency, when you divide an integer by 2 (or any power of 2), you could use the right shift operator
>> instead of the less efficient division operator.
Code:
int ConsolePosX = (rectScreen.right - consolewidth) >> 1;
int ConsolePosY = (rectScreen.bottom - consoleheight) >> 1;
Code:
HWND hwndScreen;
RECT rectScreen;
hwndScreen=GetDesktopWindow ();
GetWindowRect(hwndScreen,&rectScreen);
int ConsolePosX = (rectScreen.right - consolewidth) >> 1;
int ConsolePosY = (rectScreen.bottom - consoleheight) >> 1;
SetWindowPos(Console.WindowHandle,HWND_NOTOPMOST,ConsolePosX,ConsolePosY,500,600,SWP_NOOWNERZORDER);
error messages:
"Deleting intermediate files and output files for project 'test - Win32 Debug'.
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
c:\test\test\test.cpp(45) : error C2113: pointer can only be subtracted from another pointer
c:\test\test\test.cpp(46) : error C2113: pointer can only be subtracted from another pointer
Error executing cl.exe.
test.exe - 2 error(s), 0 warning(s)"
my C(i'm startung with C) manual dont have '<<' and '>>'.... what means?
anotherthing... why:
1 - why vertical is automatic showed(yes in console mode, the vertical scrollbar is showed)?
2 - why i can't resize the width of the window(puted 500, but is showed less:()?
-
Re: window on center of screen and fullscreen(on\off)
Quote:
my C(i'm startung with C) manual dont have '<<' and '>>'.... what means?
This will explain.
http://www.learncpp.com/cpp-tutorial...ise-operators/
<< and >> as bitwise operators are the same in c as c++
You say you are starting with c. But are you compiling as c or as c++? Although c and c++ are very similar (excluding classses, templates etc) there are some subtle differences. In c for instance, you can't declare variables in the middle of code like you can in c++. So if you are compiling as c then the code would be
Code:
HWND hwndScreen;
RECT rectScreen;
int ConsolePosX;
int ConsolePosY;
hwndScreen=GetDesktopWindow ();
GetWindowRect(hwndScreen,&rectScreen);
ConsolePosX = (rectScreen.right - consolewidth) >> 1;
ConsolePosY = (rectScreen.bottom - consoleheight) >> 1;
SetWindowPos(Console.WindowHandle,HWND_NOTOPMOST,ConsolePosX,ConsolePosY,500,600,SWP_NOOWNERZORDER);
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
This will explain.
http://www.learncpp.com/cpp-tutorial...ise-operators/
<< and >> as bitwise operators are the same in c as c++
You say you are starting with c. But are you compiling as c or as c++? Although c and c++ are very similar (excluding classses, templates etc) there are some subtle differences. In c for instance, you can't declare variables in the middle of code like you can in c++. So if you are compiling as c then the code would be
Code:
HWND hwndScreen;
RECT rectScreen;
int ConsolePosX;
int ConsolePosY;
hwndScreen=GetDesktopWindow ();
GetWindowRect(hwndScreen,&rectScreen);
ConsolePosX = (rectScreen.right - consolewidth) >> 1;
ConsolePosY = (rectScreen.bottom - consoleheight) >> 1;
SetWindowPos(Console.WindowHandle,HWND_NOTOPMOST,ConsolePosX,ConsolePosY,500,600,SWP_NOOWNERZORDER);
these lines give me an error:
Code:
ConsolePosX = (rectScreen.right - consolewidth) >> 1;
ConsolePosY = (rectScreen.bottom - consoleheight) >> 1;
error messages:
"Deleting intermediate files and output files for project 'test - Win32 Debug'.
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
c:\test\test\test.cpp(47) : error C2113: pointer can only be subtracted from another pointer
c:\test\test\test.cpp(48) : error C2113: pointer can only be subtracted from another pointer
Error executing cl.exe.
test.exe - 2 error(s), 0 warning(s)"
why these errors?
like you see the width is 500, but the window don't change it to 500.. why?
anotherthing: the console shows me a vertical scrollbar. can i hide it?
-
Re: window on center of screen and fullscreen(on\off)
What is the type definition of consolewidth and consoleheight? Where are they defined? Error c2113 implies that consolewidth and consoleheight are pointers.
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
What is the type definition of consolewidth and consoleheight? Where are they defined? Error c2113 implies that consolewidth and consoleheight are pointers.
they are normal int types
-
Re: window on center of screen and fullscreen(on\off)
Post the definitions as something somewhere is not right. Defining those two variables as int, that code compiles cleanly under my VS.
To get the same error as you are experiencing I have to define consolewidth and consoleheight as pointers to int
int *consolewidth;
int *consoleheight;
Please check your definitions carefully.
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
Post the definitions as something somewhere is not right. Defining those two variables as int, that code compiles cleanly under my VS.
To get the same error as you are experiencing I have to define consolewidth and consoleheight as pointers to int
int *consolewidth;
int *consoleheight;
Please check your definitions carefully.
sorry don't works...
i try these:
Code:
HWND hwndScreen;
RECT rectScreen;
hwndScreen=GetDesktopWindow ();
GetWindowRect(hwndScreen,&rectScreen);
SetWindowPos(Console.WindowHandle,HWND_NOTOPMOST,(rectScreen.right-rectScreen.left)\2-(500\2) ,(rectScreen.bottom- rectScreen.top)\2-(500\2),500,600,SWP_NOOWNERZORDER);
i count the '()' and they seem correct, but i get these errors:
"Deleting intermediate files and output files for project 'test - Win32 Debug'.
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
c:\test\test\test.cpp(48) : error C2017: illegal escape sequence
c:\test\test\test.cpp(48) : error C2143: syntax error : missing ')' before 'constant'
c:\test\test\test.cpp(48) : error C2660: 'SetWindowPos' : function does not take 3 parameters
c:\test\test\test.cpp(48) : error C2017: illegal escape sequence
c:\test\test\test.cpp(48) : error C2017: illegal escape sequence
c:\test\test\test.cpp(48) : error C2017: illegal escape sequence
c:\test\test\test.cpp(48) : error C2059: syntax error : ')'
Error executing cl.exe.
test.exe - 7 error(s), 0 warning(s)"
:(
-
Re: window on center of screen and fullscreen(on\off)
Code:
SetWindowPos(Console.WindowHandle,HWND_NOTOPMOST,(rectScreen.right-rectScreen.left)/2-(500/2) ,(rectScreen.bottom- rectScreen.top)/2-(500/2),500,600,SWP_NOOWNERZORDER);
/ is used for division
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
Code:
SetWindowPos(Console.WindowHandle,HWND_NOTOPMOST,(rectScreen.right-rectScreen.left)/2-(500/2) ,(rectScreen.bottom- rectScreen.top)/2-(500/2),500,600,SWP_NOOWNERZORDER);
/ is used for division
i forget that.. thanks
and now works.
the consolewidth() is a function, then i must use '()', it was my error too... thanks for all.
see these calculations:
Code:
HWND hwndScreen;
RECT rectScreen;
int ConsolePosX;
int ConsolePosY;
hwndScreen=GetDesktopWindow ();
GetWindowRect(hwndScreen,&rectScreen);
ConsolePosX = (rectScreen.right/2 - 1000/2);
ConsolePosY = (rectScreen.bottom/2 - 500/2) ;
SetWindowPos(Console.WindowHandle,HWND_NOTOPMOST,ConsolePosX,ConsolePosY,1000,500,SWP_NOOWNERZORDER);
do you think the
ConsolePosX = (rectScreen.right/2 - 1000/2);
ConsolePosY = (rectScreen.bottom/2 - 500/2) ;
are correct for calculate and put the window on center of screen?
-
Re: window on center of screen and fullscreen(on\off)
From my experience, GetWindowRect() always sets the left and top members of the RECT structure variable to 0, so subtracting them would not be needed. If this assumption bothers you, then you can subtract them as you did in post #12.
From a maintainability standpoint, you should not hard code the values of the console width or height. Also, since you do not want to resize the window, you should call SetWindowPos() with the SWP_NOSIZE flag so that the cx and cy arguments are ignored.
Code:
HWND hwndScreen;
RECT rectScreen;
int ConsolePosX;
int ConsolePosY;
hwndScreen = GetDesktopWindow();
GetWindowRect(hwndScreen, &rectScreen);
ConsolePosX = (rectScreen.right - rectScreen.left - consolewidth()) >> 1;
ConsolePosY = (rectScreen.bottom - rectScreen.top - consoleheight()) >> 1;
SetWindowPos(Console.WindowHandle, HWND_NOTOPMOST, ConsolePosX, ConsolePosY, 0, 0, SWP_NOOWNERZORDER | SWP_NOSIZE);
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
Coder Dave
From my experience,
GetWindowRect() always sets the
left and
top members of the
RECT structure variable to 0, so subtracting them would not be needed. If this assumption bothers you, then you can subtract them as you did in post #12.
From a maintainability standpoint, you should not hard code the values of the console width or height. Also, since you do not want to resize the window, you should call
SetWindowPos() with the
SWP_NOSIZE flag so that the
cx and
cy arguments are ignored.
Code:
HWND hwndScreen;
RECT rectScreen;
int ConsolePosX;
int ConsolePosY;
hwndScreen = GetDesktopWindow();
GetWindowRect(hwndScreen, &rectScreen);
ConsolePosX = (rectScreen.right - rectScreen.left - consolewidth()) >> 1;
ConsolePosY = (rectScreen.bottom - rectScreen.top - consoleheight()) >> 1;
SetWindowPos(Console.WindowHandle, HWND_NOTOPMOST, ConsolePosX, ConsolePosY, 0, 0, SWP_NOOWNERZORDER | SWP_NOSIZE);
honestly i'm tired loking some code in C\C++ that isn't compatible with my code:(
my problem is(for example) change the window size to 1000X100(try yourself and tell me).
-
Re: window on center of screen and fullscreen(on\off)
Are you trying to change the size of a console window in your code?
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
Are you trying to change the size of a console window in your code?
yes.. but the width is give problems, because i can't put 1000:(
-
Re: window on center of screen and fullscreen(on\off)
OK. Is it a console that your program has created or a console in which your program is running?
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
OK. Is it a console that your program has created or a console in which your program is running?
by defauld the Visual C++ 6 console application give a window. is what i use.
-
Re: window on center of screen and fullscreen(on\off)
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.
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
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.
try use 1000 in width;)
-
Re: window on center of screen and fullscreen(on\off)
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);
}
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
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);
}
instead const
Code:
#define CON_HEIGHT 40
#define CON_WIDTH 120
can i use a normal variables?
-
Re: window on center of screen and fullscreen(on\off)
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;
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
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;
}
Code:
system("mode CON: COLS=1000")
what you can tell me?
-
Re: window on center of screen and fullscreen(on\off)
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");
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
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?
-
Re: window on center of screen and fullscreen(on\off)
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);
}
-
Re: window on center of screen and fullscreen(on\off)
Quote:
Originally Posted by
2kaud
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
-
Re: window on center of screen and fullscreen(on\off)
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);
}
-
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.