Link to home
Start Free TrialLog in
Avatar of CrypToniC
CrypToniCFlag for Sweden

asked on

GrayString in CDialog

I have a CDialog class which contains a ListBox, scrollable,
and a Checkbutton. When the Checkbutton isn't checked
I want the ListBox to be Grayed. This shouldn't be so hard,
however the only function I can find is in the CDC class,
and can't? be implemented. However there are several solutions in the CDC class such as SetTextColor, and GrayString, eigther one will be to my fullest satisfaction!

The MyDialog is : public CDialog
and operating under .DoModal()

Good luck :)
Avatar of MFCGuy
MFCGuy

It sounds like you might just want the list box turned on/off by the check box.  If this is so, just use myCheckBox->EnableWindow(myCheckBox->GetCheck()==1) to turn it on or off in a handler for the CheckBox BN_CLICKED message through ClassWizard.

If what you really want is the CListBox to be disabled/grayed out, let me know and I'll do a search for some code...


ASKER CERTIFIED SOLUTION
Avatar of MFCGuy
MFCGuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CrypToniC

ASKER

MFCGuy what I really wanted was the ListBox to be grayed out
but still readable & selectable ( but will follow now won't it )
I'm sure there are a few ways to do this but here is one:

1. Create a new class derived from CListBox, perahps called
CGrayListBox.
2. Add a member variable like m_fReadOnly to the new class
to indicate whether or not to draw the list box gray.
2. Add a handler for the =WM_CTLCOLOR through ClassWizard
and set the colors you want, using the flag as decision maker:
HBRUSH CGrayListBox::CtlColor(CDC* pDC, UINT nCtlColor)
{
      // TODO: Change any attributes of the DC here
      if(m_fReadOnly)
      {
            pDC->SetTextColor(RGB(128,128,128));
            pDC->SetBkMode(TRANSPARENT);
            return (HBRUSH)GetStockObject(LTGRAY_BRUSH);
      }
      
      // TODO: Return a non-NULL brush if the parent's handler should not be called
      return NULL;
}
This will make the background of the listbox gray and the text gray with a clear background.
3. When the user checks/unchecks the checkbox that controls the graying of the new CGrayListBox class, set the m_fReadOnly flag
in CGrayListBox and cause a refresh, perhaps by using UpdateWindow():

In your dialog .cpp
-------------------
void OnClickMyCheckBox()
{
   m_GrayListBox->SetReadOnly(m_myCheckBox->GetCheck()==1);
}

In GrayListbox header:
----------------------
void SetReadOnly(BOOL flag){m_fReadOnly = flag;UpdateWindow();}

Hope that helps!
Let me know if you have any other questions.

Regards,
MFCGuy
>Yes, that will work. However the problem seem to be the
>CDC class, how can one access it?

I have no idea what you are talking about now...

You can always use GetDC() to get a CDC to a CFormView window,
if that is what you want.  What exactly are you trying to do,
access the CDC of a CListBox from your CFormView?  If so,
you do not want to do that, you should use a derived class
as I have outlined above.

Let me know,
MFCGuy  

Hi,
well as it turns out the EnableWindow function almost
satisfies my problem, so I think I will stick with the current
solution. I didn't know that the EnableWindow function automatically "grayes" the text when disabled and I was so hung
up with the "gray" idea that I didn't even try the EnableWindow.
Thanks for all your help and sorry for wasting your time !
/CrypToniC