Thread: Font size?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Font size?

    I was wondering is there a way to change font size and color with code?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    How exactly,I tried searching for it and it gave me a lot of unrelated things on google.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What GUI are you using, and on what platform?
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Actually I trying to attemp this in the console,im not using a gui.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Ah... I see.

    Is this console connected to an IBM mainframe? If it is, you're out of luck. However, if it is connected to some operating system that supports the ncurses library, then there is hope.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    I pulled this of MS's website:

    Code:
    #include <windows.h> 
     
    void NewLine(void); 
    void ScrollScreenBuffer(HANDLE, INT); 
     
    HANDLE hStdout, hStdin; 
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
     
    void main(void) 
    { 
        LPSTR lpszPrompt1 = "Type a line and press Enter, or q to quit: ";
        LPSTR lpszPrompt2 = "Type any key, or q to quit: ";
        CHAR chBuffer[256]; 
        DWORD cRead, cWritten, fdwMode, fdwOldMode; 
        WORD wOldColorAttrs; 
    
        // Get handles to STDIN and STDOUT. 
    
        hStdin = GetStdHandle(STD_INPUT_HANDLE); 
        hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
        if (hStdin == INVALID_HANDLE_VALUE || 
            hStdout == INVALID_HANDLE_VALUE) 
        {
            MessageBox(NULL, TEXT("GetStdHandle"),
                TEXT("Console Error"), MB_OK);
            return;
        }
    
        // Save the current text colors. 
    
        if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) 
        {
            MessageBox(NULL, TEXT("GetConsoleScreenBufferInfo"), 
                TEXT("Console Error"), MB_OK); 
            return;
        }
    
        wOldColorAttrs = csbiInfo.wAttributes; 
    
        // Set the text attributes to draw red text on black background. 
    
        if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED | 
                FOREGROUND_INTENSITY))
        {
            MessageBox(NULL, TEXT("SetConsoleTextAttribute"), 
                TEXT("Console Error"), MB_OK);
            return;
        }
    
        // Write to STDOUT and read from STDIN by using the default 
        // modes. Input is echoed automatically, and ReadFile 
        // does not return until a carriage return is typed. 
        // 
        // The default input modes are line, processed, and echo. 
        // The default output modes are processed and wrap at EOL. 
    
        while (1) 
        { 
            if (! WriteFile( 
                hStdout,               // output handle 
                lpszPrompt1,           // prompt string 
                lstrlenA(lpszPrompt1), // string length 
                &cWritten,             // bytes written 
                NULL) )                // not overlapped 
            {
                MessageBox(NULL, TEXT("WriteFile"),
                    TEXT("Console Error"), MB_OK); 
                return;
            }
    
            if (! ReadFile( 
                hStdin,    // input handle 
                chBuffer,  // buffer to read into 
                255,       // size of buffer 
                &cRead,    // actual bytes read 
                NULL) )    // not overlapped 
            break; 
            if (chBuffer[0] == 'q') break; 
        } 
    
        // Turn off the line input and echo input modes 
    
        if (! GetConsoleMode(hStdin, &fdwOldMode)) 
        {
           MessageBox(NULL, TEXT("GetConsoleMode"),
               TEXT("Console Error"), MB_OK); 
           return;
        }
    
        fdwMode = fdwOldMode & 
            ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT); 
        if (! SetConsoleMode(hStdin, fdwMode)) 
        {
           MessageBox(NULL, TEXT("SetConsoleMode"),
               TEXT("Console Error"), MB_OK); 
           return;
        }
    
        // ReadFile returns when any input is available.  
        // WriteFile is used to echo input. 
    
        NewLine();
    
        while (1) 
        { 
            if (! WriteFile( 
                hStdout,               // output handle 
                lpszPrompt2,           // prompt string 
                lstrlenA(lpszPrompt2), // string length 
                &cWritten,             // bytes written 
                NULL) )                // not overlapped 
            {
                MessageBox(NULL, TEXT("WriteFile"),
                    TEXT("Console Error"), MB_OK);
                return;
            }
    
            if (! ReadFile(hStdin, chBuffer, 1, &cRead, NULL)) 
                break; 
            if (chBuffer[0] == '\r')
                NewLine();
            else if (! WriteFile(hStdout, chBuffer, cRead, 
                &cWritten, NULL)) break;
            else
                NewLine();
            if (chBuffer[0] == 'q') break; 
        } 
    
        // Restore the original console mode. 
    
        SetConsoleMode(hStdin, fdwOldMode);
    
        // Restore the original text colors. 
    
        SetConsoleTextAttribute(hStdout, wOldColorAttrs);
    }
    
    // The NewLine function handles carriage returns when the processed 
    // input mode is disabled. It gets the current cursor position 
    // and resets it to the first cell of the next row. 
     
    void NewLine(void) 
    { 
        if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) 
        {
            MessageBox(NULL, TEXT("GetConsoleScreenBufferInfo"), 
                TEXT("Console Error"), MB_OK); 
            return;
        }
    
        csbiInfo.dwCursorPosition.X = 0; 
    
        // If it is the last line in the screen buffer, scroll 
        // the buffer up. 
    
        if ((csbiInfo.dwSize.Y-1) == csbiInfo.dwCursorPosition.Y) 
        { 
            ScrollScreenBuffer(hStdout, 1); 
        } 
    
        // Otherwise, advance the cursor to the next line. 
    
        else csbiInfo.dwCursorPosition.Y += 1; 
     
        if (! SetConsoleCursorPosition(hStdout, 
            csbiInfo.dwCursorPosition)) 
        {
            MessageBox(NULL, TEXT("SetConsoleCursorPosition"), 
                TEXT("Console Error"), MB_OK); 
            return;
        }
    } 
    
    void ScrollScreenBuffer(HANDLE h, INT x)
    {
        SMALL_RECT srctScrollRect, srctClipRect;
        CHAR_INFO chiFill;
        COORD coordDest;
    
        srctScrollRect.Left = 0;
        srctScrollRect.Top = 1;
        srctScrollRect.Right = csbiInfo.dwSize.X - x; 
        srctScrollRect.Bottom = csbiInfo.dwSize.Y - x; 
     
        // The destination for the scroll rectangle is one row up. 
     
        coordDest.X = 0; 
        coordDest.Y = 0; 
     
        // The clipping rectangle is the same as the scrolling rectangle. 
        // The destination row is left unchanged. 
     
        srctClipRect = srctScrollRect; 
     
        // Set the fill character and attributes. 
     
        chiFill.Attributes = FOREGROUND_RED|FOREGROUND_INTENSITY; 
        chiFill.Char.AsciiChar = (char)' '; 
     
        // Scroll up one line. 
     
        ScrollConsoleScreenBuffer( 
            h,               // screen buffer handle 
            &srctScrollRect, // scrolling rectangle 
            &srctClipRect,   // clipping rectangle 
            coordDest,       // top left destination cell 
            &chiFill);       // fill character and color 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. How to Change Text File Font Size
    By space in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2006, 11:42 PM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Increasing Font Size when printing to the screen
    By array in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2004, 01:30 PM
  5. The relation between Font Size & Dialog Units
    By Templario in forum Windows Programming
    Replies: 4
    Last Post: 02-27-2003, 05:32 PM