changing font size and colour

This is a discussion on changing font size and colour within the C Programming forums, part of the General Programming Boards category; I would like to know how I change the colour of font and the size. The simple code below would ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    26

    changing font size and colour

    I would like to know how I change the colour of font and the size. The simple code below would be nice to see displayed on a white background and the text red and bigger. Is this possible in the command prompt window and if so how
    Code:
    1 2 3 4 5 6 7 8 9 10 11 12 #include<stdio.h>int main(){printf("\n"); printf("------------\n"); printf("HELLO WORLD\n"); printf("------------\n"); printf("\n");return (0);}

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    the easiest way i found to change WINDOWS CONSOLE font and background colors is by including WINDOWS.H, then using the command ONCE like whne you declare a variable
    Code:
     HANDLE hConsole;
     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    then everytime you want a color change use

    Code:
    SetConsoleTextAttribute(hConsole, NUMBER);
    NUMBER is a number 0-255, there arranged in "blocks"

    groups of 16

    first 16, black background, then 16 colors of text
    second 16, next color background, then 16 colors of text
    etc

    set a number, then print text so see what colors it is, then change number and run again till you get the desired colors!

    this does NOT change the entire background to a color, if you print "HELLO" the background will be the color for ONLY the 5 spaces that the letters are, and then the text would be the reg color.

    hope this helps, and have fun!

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    26
    am I correct in assuming I need to run my code in a WINDOWS CONSOLE currently I compile it in the command prompt which windows console do you recommend I use.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    command prompt IS the windows console............

    as command.exe (or CMD.exe depending on which windows you use) is what handles your "msdos" programs for console mode, wither you call the prompt first, or your program calls it to run.
    Last edited by Crossfire; 06-30-2013 at 12:29 PM.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    26
    so the CMD.exe I cant change the colour or text size. stumped I am where do I write code and compile in c and be able to change colour and size of text

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Usually, you write the code in an editor that comes with your compiler - that's what they mean by "IDE" - Integrated Development Environment. Then, when you run your code, your console program will automatically open up a console window - a DOS type window, aka "Command Prompt window", and that window will have it's own color for background and text, and font settings.

    The IDE will also have other tools for helping you code better and faster - typically all key words in C are highlighted for you in a different color, it will have a debugger you can use to step through your code and watch variable values, set up breakpoints you can run to, etc. It may even have a profiler.

    You change the console window, by opening it, and then right clicking on the top bar of the window, and choosing either "Properties" or "Default". They both have the same settings for both color and font, but any change you make will be temporary, (one time only), if you choose "Properties". For permanent changes, choose "Default", and make your changes there, and save them.

    I'm old fashioned, so I like the blue background and white letters, and 8x12 font size.
    Last edited by Adak; 06-30-2013 at 03:42 PM.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Salem View Post
    Salem must be Sherlock Holmes
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    Quote Originally Posted by piczim View Post
    so the CMD.exe I cant change the colour or text size. stumped I am where do I write code and compile in c and be able to change colour and size of text
    Do you not read what people respond??? i broke it down to the easiest way for a 3rd grader to understand.....and your response, "so i cant do it?"

    i give up, no more help from me

  10. #10
    Registered User
    Join Date
    Jun 2013
    Posts
    26
    Quote Originally Posted by Crossfire View Post
    Do you not read what people respond??? i broke it down to the easiest way for a 3rd grader to understand.....and your response, "so i cant do it?"

    i give up, no more help from me
    I read your post and tried it and could not get it to work. nor did I understand it. Thank you for your help but I still cant change font size !!

  11. #11
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Quote Originally Posted by piczim View Post
    I read your post and tried it and could not get it to work. nor did I understand it. Thank you for your help but I still cant change font size !!
    Here is a simple example of setting the console's font size:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <Windows.h>
    
    int GetFontSize(HANDLE windowHandle, COORD *size)
        {
        CONSOLE_FONT_INFOEX font = { sizeof(CONSOLE_FONT_INFOEX) };
    
        if (!GetCurrentConsoleFontEx(windowHandle, 0, &font))
            {
            return 0;
            }
    
        *size = font.dwFontSize;
    
        return 1;
        }
    
    int SetFontSize(HANDLE windowHandle, COORD size)
        {
        CONSOLE_FONT_INFOEX font = { sizeof(CONSOLE_FONT_INFOEX) };
    
        if (!GetCurrentConsoleFontEx(windowHandle, 0, &font))
            {
            return 0;
            }
    
        font.dwFontSize = size;
    
        if (!SetCurrentConsoleFontEx(windowHandle, 0, &font))
            {
            return 0;
            }
    
        return 1;
        }
    
    int main(void)
        {
        HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD size;
    
        if (GetFontSize(h, &size))
            {
            /* Grow by 50% */
            size.X += (SHORT)(size.X * .5);
            size.Y += (SHORT)(size.Y * .5);
            SetFontSize(h, size);
            }
    
        puts("Blah blah text");
    
        return EXIT_SUCCESS;
        }
    But note that the console is not rich text. Setting the font size with change everything.
    Last edited by sonjared; 07-01-2013 at 11:12 AM.

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    333
    Colours are quite easy. Just use the conio.h fucntions.

    Changing fonts is much more difficult. Basically the console isn't designed to handle mixed fonts. You can write your own console (medium hard), on top of the Windows GDI functions, or you can hack into the console (very hard), or you can just display text in a normal windows (relatively easy).
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  13. #13
    Registered User
    Join Date
    Jun 2013
    Posts
    26
    Quote Originally Posted by sonjared View Post
    Here is a simple example of setting the console's font size:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <Windows.h>
    
    int GetFontSize(HANDLE windowHandle, COORD *size)
        {
        CONSOLE_FONT_INFOEX font = { sizeof(CONSOLE_FONT_INFOEX) };
    
        if (!GetCurrentConsoleFontEx(windowHandle, 0, &font))
            {
            return 0;
            }
    
        *size = font.dwFontSize;
    
        return 1;
        }
    
    int SetFontSize(HANDLE windowHandle, COORD size)
        {
        CONSOLE_FONT_INFOEX font = { sizeof(CONSOLE_FONT_INFOEX) };
    
        if (!GetCurrentConsoleFontEx(windowHandle, 0, &font))
            {
            return 0;
            }
    
        font.dwFontSize = size;
    
        if (!SetCurrentConsoleFontEx(windowHandle, 0, &font))
            {
            return 0;
            }
    
        return 1;
        }
    
    int main(void)
        {
        HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD size;
    
        if (GetFontSize(h, &size))
            {
            /* Grow by 50% */
            size.X += (SHORT)(size.X * .5);
            size.Y += (SHORT)(size.Y * .5);
            SetFontSize(h, size);
            }
    
        puts("Blah blah text");
    
        return EXIT_SUCCESS;
        }
    But note that the console is not rich text. Setting the font size with change everything.
    many thanks for this example. I cut and pasted the vim text editor and saved it as a C file. when I tried to compile it in the command prompt I got several errors in function get font size CONSOLE_FONT_INFOEX un declared not really sure where to go from here

  14. #14
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Quote Originally Posted by piczim View Post
    many thanks for this example. I cut and pasted the vim text editor and saved it as a C file. when I tried to compile it in the command prompt I got several errors in function get font size CONSOLE_FONT_INFOEX un declared not really sure where to go from here
    Are you linking with Kernel32.lib or Kernel32.dll? The example uses the Windows API to hook into the console, and if you're building manually from the command line you'll need to link with the appropriate libraries.

  15. #15
    Registered User
    Join Date
    Jun 2013
    Posts
    26
    Quote Originally Posted by sonjared View Post
    Are you linking with Kernel32.lib or Kernel32.dll? The example uses the Windows API to hook into the console, and if you're building manually from the command line you'll need to link with the appropriate libraries.
    Thank you for your reply! I don't think I am using a windows API. This has always been a question and a concern of mine! I would like some advice and guidance here. I am using the text editor Vim 7.3 and then manually compiling the code in the windows CMD PROMPT. I save the code in Vim with the extension c. (Example save as code.c) I then go to the CMD PROMPT go to the correct directory and type gcc code.c -o code once the code is compiled I type code in the prompt line and the program runs. So getting back to your advice or question I have no idea if I am linking with kernel32.lib or kernel32.dll I think I am building from the command line but will stand corrected. I will also stand guided on recommendation of other software to write and compile code.. Many thanks Rodney

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. changing font size in C++
    By Mukul Kumar in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2013, 10:47 AM
  2. Changing the font size in dev c++
    By Sabra khan in forum Tech Board
    Replies: 3
    Last Post: 06-01-2012, 09:17 AM
  3. changing font size
    By f6ff in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2006, 03:41 AM
  4. changing font size
    By rajesh23 in forum Linux Programming
    Replies: 2
    Last Post: 03-03-2005, 03:51 PM
  5. Changing Font colour for C++
    By dutch's finest in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2004, 07:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21