Painting Inside the Dialog Window

You can paint directly in the client area of the dialog window, but you'll avoid overwriting dialog elements if you paint only inside a control window. If you want to display text only, use the dialog editor to create a blank static control with a unique ID and then call the CWnd::SetDlgItemText function in a dialog member function such as OnInitDialog to place text in the control.

Displaying graphics is more complicated. You must use ClassWizard to add an OnPaint member function to the dialog; this function must convert the static control's ID to a CWnd pointer and get its device context. The trick is to draw inside the control window while preventing Windows from overwriting your work later. The Invalidate/UpdateWindow sequence achieves this. Here is an OnPaint function that paints a small black square in a static control:

void CMyDialog::OnPaint()
    CWnd* pWnd = GetDlgItem(IDC_STATIC1);     // IDC_STATIC1 specified
                                              //  in the dialog editor
    CDC* pControlDC = pWnd->GetDC();

    pControlDC->Rectangle(0, 0, 10, 10);      // black square bullet

As with all windows, the dialog's OnPaint function is called only if some part of the dialog is invalidated. You can force the OnPaint call from another dialog member function with the following statement: