15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Setting text color in a Dialog edit field

Jan Kodet -- Jan.Kodet@eu.pnu.com
Monday, December 09, 1996

     Environment: Win95, VC++ 4.2b (have MSDN :)
     
     I would like to set the text colour for one (or several, for that 
     matter) edit field in my dialogue (subclassed from CDialog).
     
     What I'm trying to accomplish is to edit several objects at the same 
     time. If one of the initial values for all objects was the same, the 
     text colour for this field would be standard black. If, however, a 
     values differed, I'd point this out by setting the text colour for this 
     edit field to e.g. grey (if you've seen it in other applications, you 
     know what I mean). 
     
     So far, I haven't managed to find out what to send to whom. Any ideas?
     
     Cheers,
     
     /JMK
     jan.kodet@eu.pnu.com



Bill Berry -- berry@cs.umass.edu
Friday, December 13, 1996

[Mini-digest: 3 responses]



----------
From: 	Jan Kodet
Sent: 	Monday, December 09, 1996 2:30 AM
To: 	mfc-l@netcom.com
Subject: 	Setting text color in a Dialog edit field

     Environment: Win95, VC++ 4.2b (have MSDN :)
     
     I would like to set the text colour for one (or several, for that 
     matter) edit field in my dialogue (subclassed from CDialog).
     
     What I'm trying to accomplish is to edit several objects at the same 
     time. If one of the initial values for all objects was the same, the 
     text colour for this field would be standard black. If, however, a 
     values differed, I'd point this out by setting the text colour for this 
     edit field to e.g. grey (if you've seen it in other applications, you 
     know what I mean). 
     
     So far, I haven't managed to find out what to send to whom. Any ideas?
     
     Cheers,
     
     /JMK
     jan.kodet@eu.pnu.com


Not sure quite what you want to do here. If you want to know how to
change the default colors in an edit control then you can use the
class I created below:

// ColorEdit.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CColorEdit window

// Description:
//
// CColorEdit is derived from CEdit and provides the added 
// functionality of setting and retrieving an edit control's
// back ground color and/or text color.
//
//
// Constructed by Bill Berry                   October 1996
//
// Modification Notes should be added here:
//
//

#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif

class CColorEdit : public CEdit
{
// Construction
public:
	CColorEdit();

// Attributes
private: // The following member data should be
         // declared public if conformance to
         // the MFC Coding Conventions for Extensions
         // is desired.
    CBrush m_brBkGround;
    COLORREF m_crBkColor;
    COLORREF m_crTextColor;

public:
    COLORREF SetBkColor( COLORREF );    
    COLORREF SetTextColor( COLORREF );

    COLORREF GetBkColor() const { _ASSERT(::IsWindow(m_hWnd)); return m_crBkColor; }   
    COLORREF GetTextColor() const { _ASSERT(::IsWindow(m_hWnd)); return m_crTextColor; }

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CColorEdit)
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CColorEdit();

	// Generated message map functions
protected:
	//{{AFX_MSG(CColorEdit)
		// NOTE - the ClassWizard will add and remove member functions here.
        afx_msg HBRUSH CtlColor( CDC*, UINT );
	//}}AFX_MSG

protected:
    virtual BOOL CreateBrushType();

	DECLARE_MESSAGE_MAP()
};


/////////////////////////////////////////////////////////////////////////////
     // ColorEdit.cpp : implementation file
     //

#include "stdafx.h"
#include "ColorEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Construction

CColorEdit::CColorEdit()
{
    BOOL bIsSuccess;
    
    // Sys colors used as the default text and back ground colors
    //
    m_crTextColor = ::GetSysColor( COLOR_WINDOWTEXT );
    m_crBkColor   = ::GetSysColor( COLOR_WINDOW     );

    // Create the brush type; SOLID
    //
    bIsSuccess = CreateBrushType();
    _ASSERT( bIsSuccess );

}

CColorEdit::~CColorEdit()
{
}

/////////////////////////////////////////////////////////////////////////////
// Attributes

// Set Attributes Definitions:

COLORREF CColorEdit::SetBkColor( COLORREF crColor )
{

    _ASSERT(::IsWindow(m_hWnd)); 

    BOOL bIsSuccess;

    COLORREF crPrevBkColor = m_crBkColor;
    m_crBkColor = crColor;
    
    bIsSuccess = m_brBkGround.DeleteObject();
    _ASSERT( bIsSuccess ); // Object Deletion Failed

    bIsSuccess = CreateBrushType();
    _ASSERT( bIsSuccess ); // Unable to Create Brush

    Invalidate();

    return crPrevBkColor;
}

COLORREF CColorEdit::SetTextColor( COLORREF crColor )
{
    _ASSERT(::IsWindow(m_hWnd)); 
    COLORREF crPrevTextColor = m_crTextColor;
    m_crTextColor = crColor;
    Invalidate();
    return crPrevTextColor;
}


/////////////////////////////////////////////////////////////////////////////
// Implementation

// CColorEdit message handlers:

HBRUSH CColorEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	// Set new attributes of the DC here
    //
    pDC->SetTextColor( m_crTextColor );
    pDC->SetBkColor( m_crBkColor );
    return (HBRUSH)m_brBkGround;
}

BOOL CColorEdit::CreateBrushType()
{
    return m_brBkGround.CreateSolidBrush( m_crBkColor );
}


BEGIN_MESSAGE_MAP(CColorEdit, CEdit)
	//{{AFX_MSG_MAP(CColorEdit)
    ON_WM_CTLCOLOR_REFLECT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


To use the CColorEdit control just subclass it when you initialize
your dialog.

void CCustomEditCtrlDlg::SubClassEditCtrls()
{
    m_CColorEdit.SubclassDlgItem( IDC_EDIT1, this );
    COLORREF crBk   = m_CColorEdit.SetBkColor( RGB( 0, 0 ,255 ) );
    COLORREF crText = m_CColorEdit.SetTextColor( RGB( 255, 255 , 255 ) );
}
-----From: Mike Blaszczak 

At 08:30 12/9/96 +0100, Jan Kodet wrote:
>     Environment: Win95, VC++ 4.2b (have MSDN :)
     
>     I would like to set the text colour for one (or several, for that 
>     matter) edit field in my dialogue (subclassed from CDialog).

If you have MSDN, use it: read up on the CTLCOLOR messages.  You'll find,
I'm sure, that the response time you get from a local copy of MSDN is
staggeringly faster than the response time you get from this list.
     
>     What I'm trying to accomplish is to edit several objects at the same 
>     time. If one of the initial values for all objects was the same, the 
>     text colour for this field would be standard black. If, however, a 
>     values differed, I'd point this out by setting the text colour for this 
>     edit field to e.g. grey (if you've seen it in other applications, you 
>     know what I mean). 

In your handling of WM_CTLCOLOR, write if() statements that implement your
rules and decide which color to select and which brush to return.
     
>     So far, I haven't managed to find out what to send to whom. Any ideas?

You don't need to send anything to anybody.  You need to respond to what's
being sent to you.
     
.B ekiM
http://www.nwlink.com/~mikeblas/
I'm afraid I've become some sort of speed freak.
These words are my own. I do not speak on behalf of Microsoft.

-----From: "Dmitry A. Dulepov" 

        [Mailer: "Groupware E-Mail". Version 1.02.048]


Use ClassWizard to add handler for WM_CTLCOLOR in your CDialog-deri=
ved class and process it something like this:


HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH    hBrush =3D CDialog::OnCtlColor(pDC, pWnd, nCtlColor);=

    if( (pWnd->m_hWnd =3D=3D ::GetDlgItem(m_hWnd, IDC_EDIT2) ||
        pWnd->m_hWnd =3D=3D ::GetDlgItem(m_hWnd, IDC_EDIT3))
        && bSomeConditionTrue)
        pDC->SetTextColor(RGB(127, 127, 127));
    =

    return hBrush;
}


This will return correct brush for background and set the text colo=
r you want.

Dmitry A. Dulepov
Samsung Electronics Co., Ltd.
Russian Research Center
Phone: +7 (095) 213-9207
Fax: +7 (095) 213-9196
E-mail: dima@src.samsung.ru
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
-----------------------------
>        [From: Jan Kodet
>        [Address: Jan.Kodet@eu.pnu.com
>        [To: Dmitry A. Dulepov
>        [Date: Sat Dec 14 08:00:36 1996
>     Environment: Win95, VC++ 4.2b (have MSDN :)
>     =

>     I would like to set the text colour for one (or several, for =
that =

>     matter) edit field in my dialogue (subclassed from CDialog).
>     =

>     What I'm trying to accomplish is to edit several objects at t=
he same =

>     time. If one of the initial values for all objects was the sa=
me, the =

>     text colour for this field would be standard black. If, howev=
er, a =

>     values differed, I'd point this out by setting the text colou=
r for this =

>     edit field to e.g. grey (if you've seen it in other applicati=
ons, you =

>     know what I mean). =

>     =

>     So far, I haven't managed to find out what to send to whom. A=
ny ideas?
>     =

>     Cheers,
>     =

>     /JMK
>     jan.kodet@eu.pnu.com=




Mike Blaszczak -- mikeblas@nwlink.com
Sunday, December 15, 1996

>-----From: "Dmitry A. Dulepov" 

>Use ClassWizard to add handler for WM_CTLCOLOR in your CDialog-deri=
>ved class and process it something like this:

>HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
>{
>    HBRUSH    hBrush =3D CDialog::OnCtlColor(pDC, pWnd, nCtlColor);=
>
>    if( (pWnd->m_hWnd =3D=3D ::GetDlgItem(m_hWnd, IDC_EDIT2) ||
>        pWnd->m_hWnd =3D=3D ::GetDlgItem(m_hWnd, IDC_EDIT3))
>        && bSomeConditionTrue)
>        pDC->SetTextColor(RGB(127, 127, 127));
>    =
>
>    return hBrush;
>}

It would be vastly more efficient to code this as:

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

    int nID = pWnd->GetDlgCtrlID();
    if ((nID == IDC_EDIT2 || nID == IDC_EDIT3) && bSomeCondition)
    {
       pDC->SetTextColor(RGB(127, 127, 127));
    }

    return hBrush;
}

(Hey! "Vastly more efficient" is a lot like "Woefully inefficient",
isn't it? Flame away, code piggies!)

.B ekiM
http://www.nwlink.com/~mikeblas/
I'm afraid I've become some sort of speed freak.
These words are my own. I do not speak on behalf of Microsoft.





| Вернуться в корень Архива |