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.
Re: window on center of screen and fullscreen(on\off)
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.
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)
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)?
Last edited by Cambalinho; March 9th, 2013 at 04:46 AM.
<< 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);
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
<< 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);
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?
Last edited by Cambalinho; March 9th, 2013 at 08:12 AM.
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.
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Re: window on center of screen and fullscreen(on\off)
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.
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.
Re: window on center of screen and fullscreen(on\off)
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.
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).
Bookmarks