-
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);
}
-
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.
-
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;
-
"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.
-
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?
-
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.
-
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?
-
Yes. First do ChooseFont(&cf), then set rgbCurrent, then return.
-
@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.
-
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;
-
Count the number of times "ChooseFont(&cf)" appears in your function.
-
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!