Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WIN32 Console application colour ?

WIN32 Console application colour ?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
12 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J James Laing

    Hi. Right, I have a WIN32 console application. I would like to know how to change the background from black to white. So that I can have black text on a white background. I know how to alter individual pieces if text foreground and background colour, but I would like to be able to alter the entire console window foreground and background colour. Many regards. James.

    W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #2

    just run your console app, right click the title bar to open the proerties dialog and set it from there. Remember to click yes when it prompts you to save for all future windows.

    J 1 Reply Last reply
    0
    • W Waldermort

      just run your console app, right click the title bar to open the proerties dialog and set it from there. Remember to click yes when it prompts you to save for all future windows.

      J Offline
      J Offline
      James Laing
      wrote on last edited by
      #3

      Sorry, I was not clear, I meant programmatically, at the console initialisation stage. James.

      J 1 Reply Last reply
      0
      • J James Laing

        Sorry, I was not clear, I meant programmatically, at the console initialisation stage. James.

        J Offline
        J Offline
        James Brown
        wrote on last edited by
        #4

        FillConsoleOutputAttribute


        http://www.catch22.net

        J 1 Reply Last reply
        0
        • J James Brown

          FillConsoleOutputAttribute


          http://www.catch22.net

          J Offline
          J Offline
          James Laing
          wrote on last edited by
          #5

          Thank you very much for the response. The function is indeed useful, however, I would like to know if there is a way to define the console output attributes at creation time, as my application writes an indeterminate number of lines to its output console. I would like to avoid the overhead of having to determine if the next write will be ‘outside’ the currently defined range of cells covered by the last call to function. Is there a way to specify at console initialisation, for example, the background colour of all console line output ? James.

          J 1 Reply Last reply
          0
          • J James Laing

            Thank you very much for the response. The function is indeed useful, however, I would like to know if there is a way to define the console output attributes at creation time, as my application writes an indeterminate number of lines to its output console. I would like to avoid the overhead of having to determine if the next write will be ‘outside’ the currently defined range of cells covered by the last call to function. Is there a way to specify at console initialisation, for example, the background colour of all console line output ? James.

            J Offline
            J Offline
            James Brown
            wrote on last edited by
            #6

            Use FillConsoleOutputAttribute when your program starts to fill the entire console with one colour. Use SetConsoleTextAttribute to set the current colour used for text-output these are fully documented in MSDN, I really don't know why people don't read the documentation before asking questions - it would save you a lot of time.


            http://www.catch22.net

            J 1 Reply Last reply
            0
            • J James Brown

              Use FillConsoleOutputAttribute when your program starts to fill the entire console with one colour. Use SetConsoleTextAttribute to set the current colour used for text-output these are fully documented in MSDN, I really don't know why people don't read the documentation before asking questions - it would save you a lot of time.


              http://www.catch22.net

              J Offline
              J Offline
              James Laing
              wrote on last edited by
              #7

              Thanks again for the response. I really do not understand your need to be critical of my question whilst at the same time attempting to answer it. Why did you bother if the question is so trivial?. Your answer is not even directed at me, it is a cheap arogant point scroring excercise and very childish. There are people of differing levels of experience and competence that post questions on this forum and it is people like you that most of us in the industry despise. Please feel free to ignore any further questions that I might post, I would rather not have your help. And, your response does not answer my question - try re-reading it. James.

              J 1 Reply Last reply
              0
              • J James Laing

                Thanks again for the response. I really do not understand your need to be critical of my question whilst at the same time attempting to answer it. Why did you bother if the question is so trivial?. Your answer is not even directed at me, it is a cheap arogant point scroring excercise and very childish. There are people of differing levels of experience and competence that post questions on this forum and it is people like you that most of us in the industry despise. Please feel free to ignore any further questions that I might post, I would rather not have your help. And, your response does not answer my question - try re-reading it. James.

                J Offline
                J Offline
                James Brown
                wrote on last edited by
                #8

                Excuse me, but being overly sensitive and calling people arrogant and childish is what the 'industry' despise. I am suggesting you read the documentation as this is surely the starting point of any programming question. Going by the limited description of your problem I provided an accurate solution which you would also be advised to investigate by reading the relevant MSDN pages. If you still insist that this is 'wrong' then try articulting your question a little more clearly.


                http://www.catch22.net

                J 1 Reply Last reply
                0
                • J James Brown

                  Excuse me, but being overly sensitive and calling people arrogant and childish is what the 'industry' despise. I am suggesting you read the documentation as this is surely the starting point of any programming question. Going by the limited description of your problem I provided an accurate solution which you would also be advised to investigate by reading the relevant MSDN pages. If you still insist that this is 'wrong' then try articulting your question a little more clearly.


                  http://www.catch22.net

                  J Offline
                  J Offline
                  James Laing
                  wrote on last edited by
                  #9

                  Prior to posting my original question, I had read all the MSDN documentation regarding console functionality. I could not find what I wanted, so I posted the question. If there is anyone that knows if there is a way to set console output to display in a particular colour without having to specify the number of output bytes, ie) to be able to avoid the overhead of having to keep track of the number of bytes written so far? If there is not, then so be it, but if there is, then I would very much like some pointers. James.

                  J 1 Reply Last reply
                  0
                  • J James Laing

                    Prior to posting my original question, I had read all the MSDN documentation regarding console functionality. I could not find what I wanted, so I posted the question. If there is anyone that knows if there is a way to set console output to display in a particular colour without having to specify the number of output bytes, ie) to be able to avoid the overhead of having to keep track of the number of bytes written so far? If there is not, then so be it, but if there is, then I would very much like some pointers. James.

                    J Offline
                    J Offline
                    James Brown
                    wrote on last edited by
                    #10

                    James, I really do believe the FillConsoleOutputAttribute will solve your problem - and do shouldn't need to know how many characters/bytes have been written. I had assumed you just wanted to fill the entire console buffer with the same colour? You can use FillConsoleOutputAttribute to colour all cell-positions within the console, not just those characters already written (think about how the 'CLS' command might work). So the number of characters to fill = width_of_console * height_of_console. Filling the entire console does not alter the 'cursor position' it just sets the cell-colours. GetConsoleScreenBufferInfo API will return you information regarding the size of the output-window (in the CONSOLE_SCREEN_BUFFER_INFO::dwSize field). So your number of characters. This is untested code: HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole, &csbi); COORD top = { 0, 0 }; DWORD written; WORD attr = << colour value here >> FillConsoleOutputAttribute(hConsole, attr, csbi.dwSize.x * csbi.dwSize.y, top, &written); Documentation for FillConsoleOutputAttribute states: "If the number of character cells whose attributes are to be set extends beyond the end of the specified row in the screen buffer, the cells are written up to the end of the screen buffer". So you could in fact omit the GetConsoleScreenBufferInfo and just specify a "big" number for the number-of-characters. (i.e. 2^31). Hope this was of some help, and sorry that we got off on the wrong foot. regards, James


                    http://www.catch22.net

                    J 1 Reply Last reply
                    0
                    • J James Brown

                      James, I really do believe the FillConsoleOutputAttribute will solve your problem - and do shouldn't need to know how many characters/bytes have been written. I had assumed you just wanted to fill the entire console buffer with the same colour? You can use FillConsoleOutputAttribute to colour all cell-positions within the console, not just those characters already written (think about how the 'CLS' command might work). So the number of characters to fill = width_of_console * height_of_console. Filling the entire console does not alter the 'cursor position' it just sets the cell-colours. GetConsoleScreenBufferInfo API will return you information regarding the size of the output-window (in the CONSOLE_SCREEN_BUFFER_INFO::dwSize field). So your number of characters. This is untested code: HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole, &csbi); COORD top = { 0, 0 }; DWORD written; WORD attr = << colour value here >> FillConsoleOutputAttribute(hConsole, attr, csbi.dwSize.x * csbi.dwSize.y, top, &written); Documentation for FillConsoleOutputAttribute states: "If the number of character cells whose attributes are to be set extends beyond the end of the specified row in the screen buffer, the cells are written up to the end of the screen buffer". So you could in fact omit the GetConsoleScreenBufferInfo and just specify a "big" number for the number-of-characters. (i.e. 2^31). Hope this was of some help, and sorry that we got off on the wrong foot. regards, James


                      http://www.catch22.net

                      J Offline
                      J Offline
                      James Laing
                      wrote on last edited by
                      #11

                      James Thank you for the reply. I do not want an argument and I do appreciate the response, dealing with WIN32 is a nightmare and I really do value your help. I do applogise for calling you arrogant and childish, I just sometimes find it frustrating on this web-site because there are so many people that post questions that are clearly homework and I can understand experienced members like yourself that get tired of the endless ill thought through quick fix demands from those that do want to exhaust other channels before posting a help me help question. I will look into what you have advised and I appreciate the help. Best regards. James. As a point of interest, I cannot believe that you cannot create a console with a default foreground/background output attribute.

                      L 1 Reply Last reply
                      0
                      • J James Laing

                        James Thank you for the reply. I do not want an argument and I do appreciate the response, dealing with WIN32 is a nightmare and I really do value your help. I do applogise for calling you arrogant and childish, I just sometimes find it frustrating on this web-site because there are so many people that post questions that are clearly homework and I can understand experienced members like yourself that get tired of the endless ill thought through quick fix demands from those that do want to exhaust other channels before posting a help me help question. I will look into what you have advised and I appreciate the help. Best regards. James. As a point of interest, I cannot believe that you cannot create a console with a default foreground/background output attribute.

                        L Offline
                        L Offline
                        Laing James
                        wrote on last edited by
                        #12

                        Sorry..that should have been "...from those that do NOT want to exhaust..." Bit of a typo problem there!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups