Thursday, May 20, 2004

 

How to set the background color of a read only CEdit

How to set the background color of a read only CEdit is a really classical question.

For a ordinary CEdit control, we can just implement

virtual BOOL OnChildNotify(UINT message, WPARAM wParam,
LPARAM lParam, LRESULT* pLResult)

And for a CStatic ...

afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor)

You can read the article below for details
http://codeproject.com/miscctrl/colorcontols.asp

However, this will not do for a readonly CEdit.


Here we have two solutions.

1.
By 戚高
From http://www.vchelp.net/itbookreview/view_paper.asp?paper_id=1336

In this method, we use a normal CEdit to mimic a read-only CEdit
First, override the original CEdit as

BOOL CColReadOnlyEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN)
{
if(bReadOnly)
return TRUE;//short cut the key borad input
}
return CEdit::PreTranslateMessage(pMsg);
}

Then, change the background color as

void CColReadOnlyEdit::OnPaint()
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
GetWindowText(m_Text);

// Delete the old brush
m_Brush.DeleteObject();
m_Brush.CreateSolidBrush(m_BackColor);
CDC* pDC = GetDC();
pDC->SetBkMode(OPAQUE);
pDC->SetBkColor(m_BackColor);
pDC->SelectObject(&m_Brush);
CRect rc;
GetClientRect(&rc);
ScreenToClient(&rc);
pDC->Rectangle(0, 0, rc.Width(), rc.Height());
pDC->SetTextColor(m_TextColor);
pDC->TextOut(2, 2, m_Text.GetBuffer(m_Text.GetLength()));
// Do not call CEdit::OnPaint() for painting messages
}

Besides, we need to short cut the mouse input

void CColReadOnlyEdit::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(bReadOnly)
{
::AfxMessageBox("设置成为只读模式,右键菜单屏蔽!");
return;
}
else
CEdit::OnRButtonDown(nFlags, point);
}

2.
Form http://microsoft.ease.lsoft.com/scripts/wa-msn.exe?A2=ind9906c&L=mfc&T=0&F=&S=&P=2307

Date: Wed, 16 Jun 1999 13:09:34 -0800
Reply-To: MFC -- Microsoft Foundation Classes
Sender: MFC -- Microsoft Foundation Classes
From: dempsey@DEMPSEY.COM
Subject: Re: Read-Only CEdit background/text color
Moderator's Direct Message/Digest
William Dempsey dempsey@dempsey.com

Mesg: 6522051B
From: praveen.naik@bt.com
Subj: RE: Read-Only CEdit background/text color
Date: Wed, 16 Jun 1999 13:24:32 +0100

>Does anyone know how to change the background/text color of a read-only edit
>control ?

In OnCtlColor function try the following code

HBRUSH YourDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if ((pWnd->GetStyle() & ES_READONLY) == ES_READONLY)
{
nCtlColor = CTLCOLOR_EDIT;
}
.
.
.
}

===== Digest Separator =====

Mesg: 591C331D
From: SACHIN VAIDYA
Subj: Re: Read-Only CEdit background/text color
Date: Wed, 16 Jun 1999 10:55:29 +0530

I think the following code will work for you. This is not the exact solution
but it will turn background of every readonly control to black. But you can
modify it to work it for you.

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

// TODO: Change any attributes of the DC here
pDC->SetBkMode(TRANSPARENT);

if ((pWnd->GetStyle() & ES_READONLY) == ES_READONLY)
{ // ReadOnly Edit control
nCtlColor = CTLCOLOR_EDIT;
}
switch(nCtlColor)
{
case CTLCOLOR_EDIT :
{
pDC->SetTextColor(RGB(255,255,255));
hbr=(HBRUSH)(m_BlackBrush.GetSafeHandle());
}
}
// TODO: Return a different brush if the default is not desired
return hbr;
}

Create m_BlackBrush(object of CBrush) in OnInitDialog as
m_BlackBrush.CreateSolidBrush(RGB(0,0,0));

----------------------------------------------------------------
Mailing List User's Guide; includes instructions for unsubscribing:
http://msdn.microsoft.com/workshop/essentials/mail.asp .

I like the second method. There are some other discussions. Please see

http://www.codeguru.com/Cpp/controls/editctrl/backgroundcolor/article.php/c3929
http://www.codeproject.com/editctrl/enhfocusedit.asp



<< Home

This page is powered by Blogger. Isn't yours?