RE: Subclassing dynamically created controls

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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();
m_Brush.CreateSolidBrush(m_StatusColour);
pDC->SetBkColor(m_StatusColour);
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.
.



Relevant Pages

  • Re: Subclassing dynamically created controls
    ... I have achieved being able to paint the background colour of the CEdit box ... painting of the control in MFC by making a derived class will fail at this ... so this is the colour that you want to paint the background of the ... I call pDC->SetBkColor which tells the GDI pointer for this control, ...
    (microsoft.public.vc.mfc)
  • Re: How to setup self-contained ToolTip coverage in CEdit control
    ... the CEdit control instead of the CEdit control itself. ... way to handle this with the CEdit derived class without any involvement by ... To contain the functionality of the tooltip within your control, ...
    (microsoft.public.vc.mfc)
  • Re: How to setup self-contained ToolTip coverage in CEdit control
    ... the CEdit control instead of the CEdit control itself. ... way to handle this with the CEdit derived class without any involvement by ... To contain the functionality of the tooltip within your control, ...
    (microsoft.public.vc.mfc)
  • Re: how to change the pen in cstatic frame?
    ... I have a CStatic frame created with flag SS_BLACKFRAME. ... To make any changes to the default painting in the control you will have to paint it yourself. ... You derive a class from CStatic, then subclass the control by creating a control variable in the parent dialog. ... Add a WM_PAINT message handler in your derived class and paint it using a CPaintDC. ...
    (microsoft.public.vc.mfc)
  • Custom CEdit Text Control
    ... This is the custom class .h file which inherits from CEdit. ... This is the custom class cpp file which inherits from CEdit. ... // Set the custom control member variables. ... //return the brush used for background this sets control background ...
    (microsoft.public.vc.mfc)