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();     pWnd->Invalidate();     pWnd->UpdateWindow();     pControlDC->SelectStockObject(BLACK_BRUSH);     pControlDC->Rectangle(0, 0, 10, 10);      // black square bullet     pWnd->ReleaseDC(pControlDC); } 

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:

Invalidate();


Programming Microsoft Visual C++
Programming Microsoft Visual C++
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net