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

Value Based Colorization of Dialog Controls

1 view
Skip to first unread message

mcde...@walla.com

unread,
May 2, 2005, 9:22:27 PM5/2/05
to
I'm currently working with an MFC dialog box that displays numerical
data dynamically from a remote server in a series of text control
boxes. I'd like to be able to change the background color of these
boxes based upon the values in the boxes.

So far, I've been able to change the default color of the background of
the entire dialog box and modify all text colors. However, modifying
colors for specific boxes and making changes based on values called
from those boxes is a bit challenging for me. If anyone could give me
any tips or ideas on how I might approach this, I would be very
grateful.

Kind regards,
Marcus

David Lowndes

unread,
May 3, 2005, 3:00:51 AM5/3/05
to
Marcus,

>So far, I've been able to change the default color of the background of
>the entire dialog box and modify all text colors.

Presumably you're doing that by handling WM_CTLCOLOR?

>However, modifying
>colors for specific boxes and making changes based on values called
>from those boxes is a bit challenging for me. If anyone could give me
>any tips or ideas on how I might approach this, I would be very
>grateful.

The WM_CTLCOLOR (OnCtlColor) handler is passed a CWnd pointer to the
control so use that and the nCtlColor parameter something like this:

if ( nCtlColor == CTLCOLOR_EDIT )
{
// It's an edit control - get the value
int Val = GetDlgItemInt( pWnd->GetWindowLong( GWL_ID ) )

// Do something with the value

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Asko Telinen

unread,
May 3, 2005, 3:03:35 AM5/3/05
to

You should handle a WM_CTLCOLOR message in dialog box. Editboxes sends this
message to its parent window for painting parameters.

Asko.

Marcus

unread,
May 5, 2005, 11:40:47 PM5/5/05
to
Hi Dave, thanks for your suggestions. I've had a few more days to work
with this and with your help I understand how to access values from a
text control in my dialog. My main problem is referencing the
particular control during painting. My current code looks like this:

HBRUSH CTestAPIDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
switch (nCtlColor) {
case CTLCOLOR_STATIC:
{
int Val = GetDlgItemText( IDC_BOX1, FALSE, FALSE );

if( Val = '0' )
{
pDC->SetTextColor(RGB(255, 255, 255));
pDC->SetBkColor(RGB(16, 53, 120));
}
////

Unfortunately, this code paints all static control boxes instead of the
particualr box IDC_BOX1. I'm uncertain how to proceed in colorizing
this, or any specific box. I'm sure it's somewhat easy to do, but I
just can't get my head around it. Any suggestions would be very
valuable.

Best regards and many thanks,
Marcus

Marcus

unread,
May 6, 2005, 1:17:04 AM5/6/05
to
Ok, I think I got it figured out. Just learned about the GetDlgCtrlID()
parameter that I can use to specify the specific control. So an "if
(pWnd->GetDlgCtrlID()==BOX1)" statement should work out ok.

Thanks agin you guys.

Marcus

Scott McPhillips [MVP]

unread,
May 6, 2005, 9:16:21 AM5/6/05
to

The GetDlgItemText does not get any text because you are not passing it
a buffer. The return Val would be the number of bytes copied to the
buffer, which isn't going to mean anything useful here. And then you
attempt to compare the number with a character, which is apples and
oranges. But the comparison is actually an assignment (= vs. ==) which
will always return true.

In other words, you're having a whole lot of trouble with language
basics here. It is much easier to learn the language first, practicing
with simple console programs from a C++ textbook, before diving in to
the intricacies of Windows and MFC.

--
Scott McPhillips [VC++ MVP]

Marcus

unread,
May 6, 2005, 12:39:45 PM5/6/05
to
Hi Scott, yes you're right... the code I posted was extremely rough.
The GetDlgItemTxt should be GetDlgItemInt as Dave had mentioned and the
value should be referenced by an == 0. It compiles fine.

Thanks for the suggestions, but I tend to jump right in... I have
experience with other languages, so syntax I feel I can learn on the
fly.

Best regards,
Marcus

0 new messages