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

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

  1. #16
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

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

    Are you trying to change the size of a console window in your code?
    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.

  2. #17
    Join Date
    Apr 2009
    Posts
    626

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

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

  3. #18
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    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?
    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.

  4. #19
    Join Date
    Apr 2009
    Posts
    626

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

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

  5. #20
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    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.
    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.

  6. #21
    Join Date
    Apr 2009
    Posts
    626

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

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

  7. #22
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    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);
    }
    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.

  8. #23
    Join Date
    Apr 2009
    Posts
    626

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

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

  9. #24
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

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

  10. #25
    Join Date
    Apr 2009
    Posts
    626

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

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

  11. #26
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    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");
    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.

  12. #27
    Join Date
    Apr 2009
    Posts
    626

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

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

  13. #28
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    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);
    }
    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.

  14. #29
    Join Date
    Apr 2009
    Posts
    626

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

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

  15. #30
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    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);
    }
    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.

Page 2 of 3 FirstFirst 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