-
March 7th, 2013, 06:12 PM
#1
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)"
???
-
March 8th, 2013, 09:55 AM
#2
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.
-
March 8th, 2013, 10:03 AM
#3
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.
test.exe - 4 error(s), 0 warning(s)"
-
March 8th, 2013, 10:47 PM
#4
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;
-
March 9th, 2013, 05:42 AM
#5
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 05:46 AM.
-
March 9th, 2013, 09:03 AM
#6
Re: window on center of screen and fullscreen(on\off)
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);
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 9th, 2013, 09:08 AM
#7
Re: window on center of screen and fullscreen(on\off)
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?
Last edited by Cambalinho; March 9th, 2013 at 09:12 AM.
-
March 9th, 2013, 11:20 AM
#8
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 9th, 2013, 11:21 AM
#9
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.
they are normal int types
-
March 9th, 2013, 12:00 PM
#10
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 9th, 2013, 12:10 PM
#11
Re: window on center of screen and fullscreen(on\off)
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)"
-
March 9th, 2013, 12:20 PM
#12
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 9th, 2013, 12:30 PM
#13
Re: window on center of screen and fullscreen(on\off)
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?
-
March 9th, 2013, 09:02 PM
#14
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);
-
March 10th, 2013, 02:11 PM
#15
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.
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).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|