Changing Font color

This is a discussion on Changing Font color within the Windows Programming forums, part of the Platform Specific Boards category; I've been trying to figure out how to change the font color when using a common dialog box. I've tried ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    87

    Changing Font color

    I've been trying to figure out how to change the font color when using a common dialog box. I've tried a bunch of stuff but nothing seems to have worked. Unfortunately, I don't know much about programming in C (or any other language for that matter), so that hasn't help much either. Anyhow, here's the code I have so far.

    Code:
    #define INCLUDE_COMMDLG_H       1
    #include <windows.h>
    
    static LOGFONT lf;       /* logical font structure */
    static HFONT hFont;      /* Handle */
    HDC hdc;                 /* display device context of owner window */
    
    /* DWORD rgbCurrent = RGB(255, 0, 0); */    /* current text color set to red */
    COLORREF rgbCurrent;     /* current text color */
    
    
    /****************************************************************************
     *                                                                          *
     *  FUNCTION   : FontChooseFont()                                           *
     *                                                                          *
     *  PURPOSE    : Invokes the Font dialog box.	                            *
     *									                      *
     ****************************************************************************/
    
    BOOL FontChooseFont (HWND hwnd)
    {
       CHOOSEFONT cf;    /* common dialog box structure */
    
       /* Set all structure fields to zero */
       memset(&cf, 0, sizeof(CHOOSEFONT));
    
       cf.lStructSize = sizeof (CHOOSEFONT);
       cf.hwndOwner   = hwnd;
       cf.lpLogFont   = &lf;
       cf.Flags       = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_EFFECTS;
       cf.rgbColors   = rgbCurrent;   /* was 0L ; */
       cf.nFontType   = 0;        /* Returned from ChooseFont */
    
       rgbCurrent = cf.rgbColors;
       return ChooseFont (&cf);
    }
    
    /****************************************************************************
     *                                                                          *
     *  FUNCTION   : FontInitialize ()                                          *
     *                                                                          *
     *  PURPOSE    : Gets the current active font.                              *
     *									                      *
     ****************************************************************************/
    
    void PASCAL FontInitialize (HWND hwndEdit)
    {
       GetObject (GetStockObject (SYSTEM_FONT), sizeof (LOGFONT),
                                                (PSTR) &lf);
       hFont = CreateFontIndirect (&lf);
       SendMessage (hwndEdit, WM_SETFONT, (WPARAM) hFont, 0);
    }
    
    /****************************************************************************
     *                                                                          *
     *  FUNCTION   : FontSetFont ()                                             *
     *                                                                          *
     *  PURPOSE    : Sets the new user defined font in the active window.       *
     *									                      *
     ****************************************************************************/
    
    void PASCAL FontSetFont (HWND hwndEdit)
    {
       HFONT hFontNew;
       RECT  rect;
       DWORD dwColor;
    
      /* dwColor = GetTextColor(hdc);
       if (dwColor == RGB(0, 0, 0)) */    /* if current color is black */
       /* SetTextColor(hdc, RGB(255, 0, 0)); */ /* sets color to red  */
       /* SetTextColor(hdc, rgbCurrent); */
    
       hFontNew = CreateFontIndirect (&lf);
       SendMessage (hwndActiveEdit, WM_SETFONT, (WPARAM) hFontNew, 0);
       DeleteObject (hFont);
       hFont = hFontNew;
       GetClientRect (hwndEdit, &rect);
       InvalidateRect (hwndEdit, &rect, TRUE);
    }
    
    /****************************************************************************
     *                                                                          *
     *  FUNCTION   : FontDeinitialize ()                                        *
     *                                                                          *
     *  PURPOSE    : Deinitializes font attributes upon close.                  *
     *									                      *
     ****************************************************************************/
    
    void PASCAL FontDeinitialize (void)
    {
       DeleteObject (hFont);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you allow the user to pick a font and style et cetera, but don't ever set that font? Perhaps the code that calls FontChooseFont puts the font somewhere, but we'll never know. SetTextColor works for TextOut calls, if you have any, but would work for hdc which may or may not be the same as hwndActiveEdit.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    You've got the code to do change the colour, albeit commented, but it won't be permanent unless you do it inside a WM_CTLCOLOREDIT handler.

    Code:
    case WM_CTLCOLOREDIT:
    {
        HDC hdcEdit = (HDC)wParam;
        // changes the colour to green
        SetTextColor(hdcEdit, RGB(0, 255, 0));
        // leave the background the default colour
        return (LRESULT)GetCurrentObject(hdcEdit, OBJ_BRUSH);
    }
    break;

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    "tabstop"

    The font dialog works correctly with the exception of the color option. Typeface, font style, size, strikeout, and underline all work.


    "adeyblue"

    Are you refering to this code when you said "albeit commented"?

    Code:
      DWORD dwColor;
    
      /* dwColor = GetTextColor(hdc);
       if (dwColor == RGB(0, 0, 0)) */    /* if current color is black */
       /* SetTextColor(hdc, RGB(255, 0, 0)); */ /* sets color to red  */
       /* SetTextColor(hdc, rgbCurrent); */
    The program I'm messing around with is an MDI application so I'm not quite sure where I should place the WM_CTLCOLOREDIT part. Should it go with the CALLBACK MDIChildWndProc or the CALLBACK WndProc? The initialization of the font dialog box is in the MDIChildWndProc.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    I placed "case WM_CTLCOLOREDIT" in the "CALLBACK MDIChildWndProc" and it does change the font color but only if it's hard-coded. For example:

    SetTextColor(hdc, RGB(0, 255, 0));

    For some reason "rgbCurrent" is not recieving a value from the font dialog function as shown in my first post. "rgbCurrent" ends up with a value of "0" compare to the normal "ff000" for example. Anyone have a clue as to why "rgbCurrent" isn't receiving a value?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Where in your code in the original post did you set it? I see you setting it before presenting the dialog in your FontChooseFont function. I don't see you setting it after the dialog was presented to the user, since you return right away without giving yourself a chance to do anything.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    I kinda see what your talking about. That's probably why the Microsoft example shows this as part of the font dialog.

    Code:
    if (ChooseFont(&cf)==TRUE)
    {
        hfont = CreateFontIndirect(cf.lpLogFont);
        hfontPrev = SelectObject(hdc, hfont);
        rgbCurrent= cf.rgbColors;
        rgbPrev = SetTextColor(hdc, rgbCurrent);
     .
     .
     .
    }
    Can I put something in the FontSetFont function to solve this problem?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes. First do ChooseFont(&cf), then set rgbCurrent, then return.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    @TAZIN
    If you're still struggling with this issue, I would suggest you post in the Win32 newsgroups. To quote Alex31, this has been discussed many times in the newsgroups. But anyway, a solution awaits you in the Win32 news groups.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    Thanks "BobS0327" for the suggestion. Ok, what I did (have done) was basically take everything from the FontSetFont function (refer to original post) and toss it into the FontChooseFont function.

    Code:
    BOOL FontChooseFont (HWND hwnd)
    {
       HWND hwndActiveEdit, hwndEdit;
       HFONT hFontNew;
       RECT rect;
       CHOOSEFONT cf;    /* common dialog box structure */
    
       /* Set all structure fields to zero */
       memset(&cf, 0, sizeof(CHOOSEFONT));
    
       cf.lStructSize = sizeof (CHOOSEFONT);
       cf.hwndOwner   = hwnd;
       cf.lpLogFont   = &lf;
       cf.Flags       = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_EFFECTS;
       cf.rgbColors   = rgbCurrent;   /* was 0L ; */
       cf.nFontType   = 0;        /* Returned from ChooseFont */
    
       if (ChooseFont (&cf) == TRUE){
           hFontNew = CreateFontIndirect (&lf);
           rgbCurrent = cf.rgbColors;
           SendMessage (hwndActiveEdit, WM_SETFONT, (WPARAM) hFontNew, 0);
           DeleteObject (hFont);
           hFont = hFontNew;
           GetClientRect (hwndEdit, &rect);
           InvalidateRect (hwndEdit, &rect, TRUE);
       }
    
       return ChooseFont (&cf);
    }
    I also add the WM_CTLCOLOREDIT part as suggested by "adeyblue" and now the font does change color.....But, now for some reason the font dialog box pops up twice???? It pops up after you pick the font option from the pulldown menu like normal, and you make your selections like normal. Then after hitting the OK button the text in the child window changes to the new settings, but for some strange reason the font dialog box pops back up again. If I hit the OK button or the CANCLE button again box goes away and all changes remain intact. My call to the font dialog box from CALLBACK MDIChildWndProc is:

    Code:
    case CM_FONTSELECT:
    FontChooseFont (hwnd);
    break;

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Count the number of times "ChooseFont(&cf)" appears in your function.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    I see what you mean "tabstop". I have to laugh at myself for all the somewhat silly mistakes I made regarding the addition of this somewhat simple color feature. I changed the return to TRUE and all seems fine now. My thanks go out to "tabstop" and "adeyblue" for your help and patiance. Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Towr of Hanoi move the disc
    By WatchTower in forum C Programming
    Replies: 9
    Last Post: 07-17-2009, 04:48 AM
  2. Critique my lighting model.
    By psychopath in forum Game Programming
    Replies: 4
    Last Post: 08-12-2006, 07:23 PM
  3. Changing font style and color of text in C
    By bigKIDmarie in forum C Programming
    Replies: 5
    Last Post: 07-30-2006, 06:04 AM
  4. problem with my font manager
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 03-07-2006, 08:03 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM

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