![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 17
| Changing Font color 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 | |
| | #2 |
| and the Hat of Guessing 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 | |
| | #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 | |
| | #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); */ |
| TAZIN is offline | |
| | #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 | |
| | #6 |
| and the Hat of Guessing 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 | |
| | #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);
.
.
.
}
|
| TAZIN is offline | |
| | #8 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Yes. First do ChooseFont(&cf), then set rgbCurrent, then return. |
| tabstop is offline | |
| | #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 | |
| | #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);
}
Code: case CM_FONTSELECT: FontChooseFont (hwnd); break; |
| TAZIN is offline | |
| | #11 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Count the number of times "ChooseFont(&cf)" appears in your function. |
| tabstop is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |