Thread: coloring text

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    42
    Rep Power
    12

    Question coloring text


    you know when you are programming in MSVC that C++ keywords like int, double, float, etc are colored differently? well i want to do something like that but i am at a loss as how to do it. any guidence would be great.:)
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    It is not at all clear what you are asking.

    If you are asking about changing the color settings in Visual C++:

    In Visual C++ v1.52 (which I always associate with MSVC), the colors are set through the main menu: Options -> Color.

    In VC++6, the menu path is: Tools -> Options. The Options property sheet opens and you need to scroll the tabs all the way to the right to the Format page.


    Now, if instead you want to change the color of the text you are writing to a device context, then consider this sample code:
    Code:
    void CSomeClass::Draw(CDC* pDC, BOOL Select)
    {
        COLORREF Color(RGB(0,0,255));  // blue
    
        // set the text color and output string in blue
        pDC->SetTextColor(Color);
        pDC->TextOut(m_StartPoint.x,m_StartPoint.y, m_aString);
    }
    Of course, if you are drawing with a pen, then the color is set in the CPen constructor, again using the RGB() function.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    42
    Rep Power
    12
    i am making an editor for a script type language and i want to change to color of the keywords like is done in Microsoft Visual C++ (MSVC) but i am not sure how to do it as the code is typed in.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by Combat
    i am making an editor for a script type language and i want to change to color of the keywords like is done in Microsoft Visual C++ (MSVC) but i am not sure how to do it as the code is typed in.
    Observe how the editor does it. You start typing in the keyword and it's in black. But as soon as it is a complete keyword, it turns blue. However, if you add another letter or modify it in any manner so that it is no longer a keyword, it turns black again. Modify it back or modify a non-keyword into a keyword and it turns blue again.

    Assuming that you are using the Document/View model, I would suggest that the View have a method for changing the color of the word. The actual mechanics would probably involve blanking out the region containing the word (but not any letters around it), setting the text color appropriately, and rewriting the word. Then the Document would need to parse on-the-fly the text being typed in. When it recognizes that the word is a keyword, it will need to command the View to change its color. Likewise if a keyword gets changed to a non-keyword.

    Or when the View is updating the edit window, which it has to do every single time you press a key, it could ask the Document whether it needs to change the color of the word.
    Last edited by dwise1_aol; June 4th, 2003 at 02:31 PM.
  8. #5
  9. No Profile Picture
    Overly white
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Fresno, CA
    Posts
    83
    Rep Power
    12

    Lightbulb


    I'm not a c++ guy by any means, just starting to get to the point of being a newbie, but I thought I would just throw this idea out anyway.

    Maybe you could capture the space bar event then grab all the text from the current position back to the next space. Then look up the word in your keyword list and see if it is a keyword then color and replace it.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by defjamninja
    I'm not a c++ guy by any means, just starting to get to the point of being a newbie, but I thought I would just throw this idea out anyway.

    Maybe you could capture the space bar event then grab all the text from the current position back to the next space. Then look up the word in your keyword list and see if it is a keyword then color and replace it.
    Basically the right idea, though the latest key might not be the space bar. It could be an asterix, as in char*. Or it could be almost any key if the cursor is positioned within the word and the word has been modified (a character deleted or added).

    He doesn't really have to worry about capturing keys. In Windows programming, every time a key is pressed (or, in the more restricted case, a character key is pressed), Windows sends a message to the application which notifies the app of the event and which key/character it is. Windows does not automatically update the display within a client window, but rather it is going to be up to the application to do the updating, so the application already has to look at each and every character that comes through. So it makes sense to me that this is where it will test for keywords.
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    You need to use a RichTextEdit Control to colour different parts of your text. If you want to save yourself a lot of coding effort, there are several third party active-x controls available, that do most of the work for you. All you have to do is specify a list of keywords and colours to use. You can possibly google for them (or look at the advertisements in Dr. Dobbs Journal -- there's always at least one ad for this).

    Hope this helps :)
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    You can also get some free code from here:
    http://www.codeguru.com/richedit/SyntaxColorizer.html

    Enjoy!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    42
    Rep Power
    12
    wow!!! that was exactly what i needed!!!! i didnt think i would get something THAT helpful. thank you so much!!!:)

IMN logo majestic logo threadwatch logo seochat tools logo