C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-24-2009, 11:54 AM   #1
Registered User
 
Join Date: Jun 2009
Posts: 17
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);
}
TAZIN is offline   Reply With Quote
Old 10-24-2009, 05:06 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-24-2009, 05:44 PM   #3
Hat seller extraordinaire
 
Join Date: Apr 2008
Posts: 159
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;
adeyblue is offline   Reply With Quote
Old 10-24-2009, 06:14 PM   #4
Registered User
 
Join Date: Jun 2009
Posts: 17
"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.
TAZIN is offline   Reply With Quote
Old 10-25-2009, 10:31 AM   #5
Registered User
 
Join Date: Jun 2009
Posts: 17
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?
TAZIN is offline   Reply With Quote
Old 10-25-2009, 12:49 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-25-2009, 01:29 PM   #7
Registered User
 
Join Date: Jun 2009
Posts: 17
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?
TAZIN is offline   Reply With Quote
Old 10-25-2009, 01:44 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Yes. First do ChooseFont(&cf), then set rgbCurrent, then return.
tabstop is offline   Reply With Quote
Old 10-25-2009, 07:44 PM   #9
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,054
@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.
BobS0327 is offline   Reply With Quote
Old 10-26-2009, 10:04 AM   #10
Registered User
 
Join Date: Jun 2009
Posts: 17
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;
TAZIN is offline   Reply With Quote
Old 10-26-2009, 11:47 AM   #11
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Count the number of times "ChooseFont(&cf)" appears in your function.
tabstop is offline   Reply With Quote
Old 10-26-2009, 03:31 PM   #12
Registered User
 
Join Date: Jun 2009
Posts: 17
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!
TAZIN is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:04 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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