Archived

This topic is now archived and is closed to further replies.

doctorsixstring

Dialog Painting in MFC

Recommended Posts

I have an MFC class derived from CDialog. I want to perform drawing operations on the dialog, but I seem to be running into some trouble. I added the OnPaint() function to the class (through the ClassWizard), and it is acting pretty strange. First of all, I simply add some code to draw a line, like so: ///////////// dc.SelectObject(CPen::FromHandle(CreatePen(PS_SOLID, 1, RGB(255,0,0)))); dc.MoveTo(0,0); dc.LineTo(68,68); ///////////// This code fails to draw anything. However, if I add a simple MessageBox right before that code, it will draw (after displaying the message box)! I have no idea why it only works with the message box. One clue may be a wizard-added comment reading "Do not call CDialog::OnPaint() for painting messages" into the function. I would assume this message means exactly what it says: Don''t use OnPaint() to paint into a dialog box. If this is the case, what function should I use? -Mike

Share this post


Link to post
Share on other sites
Actually, do not call CDialog::OnPaint means exactly that, inside of your OnPaint handler, do not call CDialog::OnPaint() . Normally, for CWnd and such, you would call CWnd::OnPaint() inside of your derived OnPaint function, however with dialog''s this is not the correct behaviour.

Now onto your drawing problem, the issue is that the dialog does not know it needs to refresh itself, so therefore is not drawing your items. Insert a call to InvalidateRect(NULL) to redraw the entire dialog window (do this outside of the paint function however). So in your OnInitDialog() you might want to call it, or call it whenever a button is hit that means the window must be refreshed.

Share this post


Link to post
Share on other sites