Re: Subclassing dynamically created controls
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Fri, 28 Dec 2007 23:00:21 -0500
See below...
On Fri, 28 Dec 2007 06:18:00 -0800, James Simpson <JamesSimpson@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
Dear Sujay,****
I have achieved being able to paint the background colour of the CEdit box
using a derived class perfectly! Here is a snippet of code that I use for
this purpose, because I know that alot of people who attempt to take over the
painting of the control in MFC by making a derived class will fail at this
step:
HBRUSH CEditTest::CtlColor(CDC *pDC, UINT nCtlColor)
{
m_Brush.DeleteObject();
Why? Unnecessary, unless you are changing the brush, which you are not. Once the brush
exists, you apparently don't need to do anything. So deleting it is either redundant
(becuase it doesn't exist) or pointless (requiring you re-create it on the next line).
****
m_Brush.CreateSolidBrush(m_StatusColour);****
Do this once, in your constructor, or alternatively, in the PreSubclassWindow handler
****
pDC->SetBkColor(m_StatusColour);Joseph M. Newcomer [MVP]
return (HBRUSH)m_Brush;
}
To briefly explain what the code does:
1) I start by "clearing" the CBrush object I am using, removing the colour
and style I am using via DeleteObject
2) I call CreateSolidBrush passing to the function a color value of type
COLORREF, so this is the colour that you want to paint the background of the
edit box to.
3) I call pDC->SetBkColor which tells the GDI pointer for this control, to
use the correct color when drawing the edit control again.
4) I call return (HBrush)m_Brush which basically returns a handle to the
brush that I used to paint the control, which may be needed later.
Additional note : as mentioned earlier you will need to create member
variables within the derived CEdit class that will hold the Brush that you
will be using to paint the colour onto the control and a COLORREF value that
gives MFC the precise colour you want to use when painting the control.
Here are some additional code snippets to look at:
m_StatusColour = RGB(211,211,211); // this code here causes the control to
turn grey in colour
void CEditTest::UpdateCtrl()
{
CWnd* pParent = GetParent();
CRect rect;
GetWindowRect(rect);
pParent->ScreenToClient(rect);
pParent->InvalidateRect(rect, FALSE);
}
You will want to create a member function called UpdateCtrl in your derived
CEdit class. You will call this function each and every time you want to
update the colour of the control. Essentially what the code does is it tells
Windows that the current control (your derived control's) background area is
dirty and needs to be updated. e.g. telling Windows that your control's
background color needs to be changed.
Hope this helps,
Regards,
James Simpson
Straightway Technologies Inc.
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- RE: Subclassing dynamically created controls
- From: Sujay Ghosh
- RE: Subclassing dynamically created controls
- From: James Simpson
- RE: Subclassing dynamically created controls
- Prev by Date: Re: Internationalization
- Next by Date: Re: CString to LPCSTR
- Previous by thread: RE: Subclassing dynamically created controls
- Next by thread: Re: Subclassing dynamically created controls
- Index(es):
Relevant Pages
|