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

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

  1. #31
    Join Date
    Apr 2009
    Posts
    660

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

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

  2. #32
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,455

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

  3. #33
    Join Date
    Apr 2009
    Posts
    660

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

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

  4. #34
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,455

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

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

  5. #35
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

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

    Quote Originally Posted by Cambalinho View Post
    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 View Post
    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;
    }

  6. #36
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,990

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

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

Page 3 of 3 FirstFirst 123

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center