Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing color of CStatic text

843 views
Skip to first unread message

Job Seeker

unread,
Aug 15, 1997, 3:00:00 AM8/15/97
to

In "Programming Windows 95 with MFC" by Jeff Prosise, has a good example of
this.
I would include sample code but I left the book at work.
Maybe on Monday if you are still interested.
--
S Chipman

Joe Wasik <jcw...@pacbell.dot_com> wrote in article
<33F49F...@pacbell.dot_com>...
> Does anyone have any idea on how to change the text
> color of a CStatic control in MFC/C++ ??
>
> I need to do it programmatically and conditionally, but
> without effecting the background color.
>
> Preferrably, the code could change the color of text
> already there, but I'd settle for having to re-insert
> the text.
>
>
> --
> Note: I have spam proofed the "From" and "Reply-To" addresses.
> Please make an educated guess. You'll get it right.
> Think of this as "Spambonics".
>
>

Davide Marcato

unread,
Aug 15, 1997, 3:00:00 AM8/15/97
to

On Fri, 15 Aug 1997 11:25:56 -0700 Joe Wasik <jcw...@pacbell.dot_com>
wrote:

>Does anyone have any idea on how to change the text
>color of a CStatic control in MFC/C++ ??
>
>I need to do it programmatically and conditionally, but
>without effecting the background color.
>
>Preferrably, the code could change the color of text
>already there, but I'd settle for having to re-insert
>the text.

Message WM_CTLCOLORSTATIC is your friend.
Check it out in the documentation, it's easy to handle.

Davide Marcato.
----------------------------------------------------------
| -- Freelance International Consultant
| -- C/C++ & Windows Developer
| -- Internet Developer & Administrator
| -- Tech. Writer: "Computer Programming", "Login", "DEV"
----------------------------------------------------------
| -- E-Mail: dav...@ashnet.it - mar...@programmers.net
| -- Web: http://www.ashnet.it http://www.programmers.net
----------------------------------------------------------

Patrick Doyle

unread,
Aug 15, 1997, 3:00:00 AM8/15/97
to

In article <33F49F...@pacbell.dot_com>,

Joe Wasik <jcwasik_at_pacbell_dot_com> wrote:
>Does anyone have any idea on how to change the text
>color of a CStatic control in MFC/C++ ??
>
>I need to do it programmatically and conditionally, but
>without effecting the background color.
>
>Preferrably, the code could change the color of text
>already there, but I'd settle for having to re-insert
>the text.

I don't know if there's a simple way to do this, but you could
override OnPaint and draw the thing yourself. That way, if you
need to make further customizations in the future, the framework
is already in place.

But get a second opinion before you dive into this one... :-)

-PD

--
--
Patrick Doyle
doy...@ecf.utoronto.ca

Joe Wasik

unread,
Aug 15, 1997, 3:00:00 AM8/15/97
to

I've seen some examples of trapping color messages in
OnChildNotify(). However, none of them solve the problem
of changing text without effecting background.

If the background happens to be white, then maybe you
would not notice a problem. But statics and read-only
edits have a grey background.

The best I've been able to do is to change the color
of the text and have only the characters surrounded
by white.

Hope you have better code. I'll make a note of that book.

Job Seeker wrote:
>
> In "Programming Windows 95 with MFC" by Jeff Prosise, has a good example of
> this.
> I would include sample code but I left the book at work.
> Maybe on Monday if you are still interested.
> --
> S Chipman
>
> Joe Wasik <jcw...@pacbell.dot_com> wrote in article
> <33F49F...@pacbell.dot_com>...

Joe Wasik

unread,
Aug 15, 1997, 3:00:00 AM8/15/97
to

Does anyone have any idea on how to change the text
color of a CStatic control in MFC/C++ ??

I need to do it programmatically and conditionally, but


without effecting the background color.

Preferrably, the code could change the color of text
already there, but I'd settle for having to re-insert
the text.

Cecil A. Galbraith

unread,
Aug 16, 1997, 3:00:00 AM8/16/97
to jcw...@pacbell.com


Below is the text from an article about changing the colors for an edit
control. The technique is the same for a static control, but make sure
that you assign a unique ID (something other than IDC_STATIC) to the
static control.

This article is located, with others, at the URL below.

Cecil

////////////////////////////////

How to change the background color in a single edit control

There are instances where you might have multiple edit
controls in a dialog box, but want only one
of the controls to have a different background color. This
article shows how to accomplish that.
The technique applies to other types of controls as well.

This example assumes that you have created a red brush
(m_Brush) at such a place in your code
that the brush will have an adequate lifetime. It is also
assumed that you are responsible for
deleting the brush object and freeing up any memory that the
brush required.

Using Class Wizard, implement a handler for the WM_CTLCOLOR
message in your dialog object.

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT
nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd,
nCtlColor);

return hbr;

// TODO: Return a different brush if the
default is not desired
// return hbr;
}

Check the value of nCtlColor against CTLCOLOR_EDIT and the
handle of the window pointed to by
pWnd against the handle of the edit control which color you
want to change. The trick is to use the
window handles for the comparison rather than the pointers
because the pointers are temporary -
sometimes too temporary - but the handles are permanent.

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT
nCtlColor)
{

// get a pointer to the specific edit control which
color you want to control

CEdit * pEdit = (CEdit
*)GetDlgItem(IDC_REDEDIT);

if(nCtlColor == CTLCOLOR_EDIT &&
pEdit->GetSafeHwnd() == pWnd->GetSafeHwnd()) {

// set the back mode so that new background color
shows through the text

pDC->SetBkMode(TRANSPARENT);

// make the text white so that it shows up well over
red

pDC->SetTextColor(RGB(255,255,255));

// return the red brush

return m_Brush;
}

HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd,
nCtlColor);

return hbr;
}


This approach will work with edit controls that are
designated as READONLY by comparing
nCtlColor with CTLCOLOR_STATIC rather than CTLCOLOR_EDIT.
You should also set the text
back color to an appropriate color rather than setting the
back mode to TRANSPARENT.

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT
nCtlColor)
{

// get a handle to the specific READ ONLY edit
control which color you want to control

CEdit * pEditReadOnly = (CEdit
*)GetDlgItem(IDC_READONLYEDIT);
HWND hWndReadOnly =
pEditReadOnly->GetSafeHwnd();

if(nCtlColor == CTLCOLOR_STATIC &&
hWndReadOnly == pWnd->GetSafeHwnd()) {

// set the text back color to the same as the brush
(red)

pDC->SetBkColor(RGB(255,0,0));

// make the text white so that it shows up well over
red

pDC->SetTextColor(RGB(255,255,255));

// return the red brush

return m_Brush;

}

HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd,
nCtlColor);

return hbr;
}

/////////////////////////////////////////
--
Cecil Galbraith
CustomSoft mail to cgal...@concentric.net

Free programmer's utilities and MFC tips at
http://www.concentric.net/~cgalbrai

Jim and Alaine Allison

unread,
Aug 16, 1997, 3:00:00 AM8/16/97
to

Here's some legacy code from Win16, but it could be a starting point - it
sets the text and background colors for controls based on user-defined
color selections (set in a color selection dialog)...

CBaseApp is a common base application class - the relevant items for this
class are the member vars that define the colors used to paint controls....

Wax the stuff for Ctl3d... you could use GetClassLong( GCL_HBRBACKGROUND)
to retrieve the
default brush, since Win32 doesn't require the same 3-d shenanigans as
Win16.

--
jim allison
Origin Technology in Business, Inc.
jall...@otb.com

//----------------------------------------------------------------------
// handle WM_CTLCOLOR messages used to update controls
//----------------------------------------------------------------------
HBRUSH CCustomFormView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hBr;
CBaseApp* pApp = (CCDMApp*)AfxGetApp();

switch (nCtlColor)
{
case CTLCOLOR_LISTBOX:
{
const int nClassNameLength = 64;
char szClassName[nClassNameLength];

if( ::GetClassName( pWnd->m_hWnd,
szClassName,
nClassNameLength
) > 0
&& !lstrcmp( szClassName, "ComboBox")
)
{
hBr = Ctl3dCtlColorEx( WM_CTLCOLOR,
(WPARAM)pDC->m_hDC,
MAKELPARAM( (pWnd->m_hWnd),
nCtlColor)
);

if( hBr != (HBRUSH)FALSE)
return hBr;
break;
}
//***** fall thru to Edit and MsgBox handler *****
}
case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX:
// if control is enabled, paint in EDITABLE FIELD colors
if( pWnd->IsWindowEnabled())
{
pDC->SetTextColor( pApp->GetEditableFgdColor() );
pDC->SetBkColor( pApp->GetEditableBgdColor());

return (HBRUSH) pApp->GetEditableBgdBrush();
}
else
{
// if control is disabled, paint in STATIC TEXT FIELD
colors
pDC->SetTextColor( pApp->GetStaticFieldFgdColor() );
pDC->SetBkColor( pApp->GetStaticFieldBgdColor());

return (HBRUSH) pApp->GetStaticFieldBgdBrush();
}

break;

case CTLCOLOR_STATIC:

// if we assigned a control id other than (-1), treat it as a
"display field"
if( pWnd->GetDlgCtrlID() != -1)
{
pDC->SetTextColor( pApp->GetStaticFieldFgdColor() );
pDC->SetBkColor( pApp->GetStaticFieldBgdColor());

return (HBRUSH) pApp->GetStaticFieldBgdBrush();
}
// id = -1, treat as a "title/heading"
else
{
hBr = Ctl3dCtlColorEx( WM_CTLCOLOR,
(WPARAM)pDC->m_hDC,
MAKELPARAM( (pWnd->m_hWnd),
nCtlColor)
);

if ( hBr != (HBRUSH)FALSE)
return hBr;
}
break;

default:
hBr = Ctl3dCtlColorEx( WM_CTLCOLOR,
(WPARAM)pDC->m_hDC,
MAKELPARAM( (pWnd->m_hWnd), nCtlColor)
);

if( hBr != (HBRUSH)FALSE)
return hBr;

break;
}

return CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
}

=======================================================================

Joe Wasik <jcw...@pacbell.dot_com> wrote in article
<33F49F...@pacbell.dot_com>...

Sam Chipman

unread,
Aug 19, 1997, 3:00:00 AM8/19/97
to

Try this it is derived from CStatic.

It's been awhile since I've used it and don't think it works in 16 bit.
--
S Chipman

Joe Wasik <jcw...@pacbell.dot_com> wrote in article

<33F4F1...@pacbell.dot_com>...


> I've seen some examples of trapping color messages in
> OnChildNotify(). However, none of them solve the problem
> of changing text without effecting background.
>
> If the background happens to be white, then maybe you
> would not notice a problem. But statics and read-only
> edits have a grey background.
>
> The best I've been able to do is to change the color
> of the text and have only the characters surrounded
> by white.
>
> Hope you have better code. I'll make a note of that book.
>
> Job Seeker wrote:
> >
> > In "Programming Windows 95 with MFC" by Jeff Prosise, has a good
example of
> > this.
> > I would include sample code but I left the book at work.
> > Maybe on Monday if you are still interested.
> > --
> > S Chipman
> >

> > Joe Wasik <jcw...@pacbell.dot_com> wrote in article
> > <33F49F...@pacbell.dot_com>...
> > > Does anyone have any idea on how to change the text
> > > color of a CStatic control in MFC/C++ ??
>

end

begin 600 cstatic.cpp
<encoded_portion_removed>
end


cstatic.h

Joe Wasik

unread,
Aug 20, 1997, 3:00:00 AM8/20/97
to

This is definately a little more than I had planned. We are not
going to draw our own buttons.

To get the sollution right turned out to be somewhat tricky.
Originally, we missed setting the background mode. But it's all
working now.

0 new messages