Re: Subclassing dynamically created controls

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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);
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.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



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)
  • Re: Help with SendKeys
    ... That's a very good suggestion. ... MS Paint myself and so in my previous response I was just going by the ... handles by the fairly straightforward VB Sendkeys function. ... apparent that there are too many bridges to cross to control MSPaint ...
    (comp.lang.basic.visual.misc)