window on center of screen and fullscreen(on\off)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: window on center of screen and fullscreen(on\off)

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    609

    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)"
    ???

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,782

    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.

  3. #3
    Join Date
    Apr 2009
    Posts
    609

    Re: window on center of screen and fullscreen(on\off)

    Quote Originally Posted by OReubens View Post
    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)"

  4. #4
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Resolved 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;

  5. #5
    Join Date
    Apr 2009
    Posts
    609

    Re: window on center of screen and fullscreen(on\off)

    Quote Originally Posted by Coder Dave View Post
    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.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,269

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 2009
    Posts
    609

    Re: window on center of screen and fullscreen(on\off)

    Quote Originally Posted by 2kaud View Post
    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 08:12 AM.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,269

    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.

  9. #9
    Join Date
    Apr 2009
    Posts
    609

    Re: window on center of screen and fullscreen(on\off)

    Quote Originally Posted by 2kaud View Post
    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

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,269

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #11
    Join Date
    Apr 2009
    Posts
    609

    Re: window on center of screen and fullscreen(on\off)

    Quote Originally Posted by 2kaud View Post
    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)"

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,269

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Apr 2009
    Posts
    609

    Re: window on center of screen and fullscreen(on\off)

    Quote Originally Posted by 2kaud View Post
    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?

  14. #14
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Resolved 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);

  15. #15
    Join Date
    Apr 2009
    Posts
    609

    Re: window on center of screen and fullscreen(on\off)

    Quote Originally Posted by Coder Dave View Post
    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).

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center