1

I created a custom control whose class has CStatic as base class. Currently I handle the drawing using WM_PAINT event. But there is a strange behavior. When I re-enable the window after disabling it using CWnd::EnableWindow function, it refuses to draw what I written in OnPaint function. It draws the static control instead.

I agree that there is this standard method of overriding DrawItem and using SS_OWNERDRAW style. But what's wrong with WM_PAINT?

void XXControl::OnPaint()
{
    CPaintDC PaintDC( this );
    // ** draw the control to PaintDC**
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Ragesh Chakkadath
  • 1,521
  • 1
  • 14
  • 32
  • CStatic is parent-control or base-class? – Ajay Aug 25 '11 at 09:05
  • Did you try calling Invalidate after EnableWindow? Place a breakpoint in OnPaint if it actually receives the message or not. – Ajay Aug 25 '11 at 10:22
  • Obviously not! OnPaint in that case is not getting called. Also, what's the need of Invalidate when on that particular call, the control that is drawn perfectly is turning in to a normal static control? so a draw happens there, but not through my OnPaint in that case. – Ragesh Chakkadath Aug 25 '11 at 13:34

2 Answers2

6

Here is exactly what I have written:

class CMyStatic : public CStatic
{
    DECLARE_MESSAGE_MAP()
public:
    void OnPaint(void);
};

BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
    ON_WM_PAINT()
END_MESSAGE_MAP()

void CMyStatic::OnPaint(void)
{
    CPaintDC dc(this);
    CRect rect;
    GetClientRect(&rect);

    dc.FillSolidRect(&rect, RGB(120,255,0));
}

And subclassed:

class CMyDlg : public CDialog
{
// Construction
    CMyStatic my_static;
...
};


BOOL CCMyDlg::OnInitDialog()
{
   CDialog::OnInitDialog();

   my_static.SubclassDlgItem(IDC_DRAW, this);

   return true;
}

Where IDC_DRAW is static control on resource for this dialog. I wrote two button handlers:

void CMyDlg::OnBnClickedOk()
{
    my_static.EnableWindow(FALSE);
    my_static.Invalidate();
}

void CMyDlg::OnBnClickedOk2()
{
    my_static.EnableWindow();
    my_static.Invalidate();
}

And it works flawlessly! Remove Invalidate call and it would fail.

Ajay
  • 18,086
  • 12
  • 59
  • 105
  • "Remove Invalidate call and it would fail." exception? or is it like no draw happening? I realized that its not the static control being drawn ( it had no text ), but the draw wasn't happening. So Invalidate might be the solution here. lemme check! – Ragesh Chakkadath Aug 25 '11 at 15:49
  • No exception. It would render using default Windows painting. Call Invalidate and it would use your OnPaint. – Ajay Aug 25 '11 at 15:55
1

Try turning off Aero. I'm having a similiar problem where I'm drawing a static control and when it goes from disabled to enabled the WM_PAINT message is never received, but if I turn Aero off it works fine.