How to define onDraw() in MFC

5.9k views Asked by At

I understand that the onDraw() function is virtual void which is called automatically via OnPaint(), which is itself triggered by the WM_PAINT message.

My declaration goes something like this:

myDialog.cpp

void myDialog::OnDraw(CDC* dc)
{
    CCustomMemDC pDC(dc);
    CExampleDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
}

myDialog.h

afx_msg void OnDraw(CDC* dc);

and in my OnPaint()

CDialog::OnPaint();

However I could not get OnDraw to be triggered at all... Could anyone enlighten if this method is correct?

Thanks!

2

There are 2 answers

0
Joseph Willcoxson On

It is not correct. As others have pointed out, OnDraw() is a virtual method of CView and not CDialog. Therefore, your OnDraw() method will never be called.

I don't know why your dialog would have a flicker problem that you would need to mitigate it. If you have a custom control on the dialog that is causing flicker, then you probably need to fix the drawing code in it and possibly add WS_CLIPCHILDREN as a Window Style to your dialog. That will make sure that the background of the custom control is not erased when the dialog executes its WM_ERASEBKGND message.

It makes no sense to override the WM_PAINT of the dialog unless you are really planning to do custom drawing on it. Unless you have a very good reason, don't do it.

If you choose to ignore the advice then add an ON_WM_PAINT() to your message handler to the dialog, and then draw the dialog yourself:

void myDialog::OnPaint()
{
   CPaintDC dc(this);
   CCustomMemDC pDC(&dc);
   // do your specialized code knowing you are responsible for drawing the whole dialog
}

If you really want to use your OnDraw() method, then you could also write OnPaint() like this:

void myDialog::OnPaint()
{
   CPaintDC dc(this);
   OnDraw(&dc);
}

But, the first thing I would do in your situation is try adding the style WS_CLIPCHILDREN to your dialog style.

0
xMRi On

A dialog has no OnDraw. This is a virtual function in views to allow printing and drawing to a screen.

In a dialog just use OnPaint. There isno redirection to a virtual OnDraw function in a dialog.