0

UPDATE: Thanks for IInspectable's reply. I have edited my problem.

I am writing a dialog-based application which has a static control. The app draws a line according to the points captured by OnLButtonDown() and OnLButtonUp().

I create a class CCanvasStatic derived from CStatic and the static control variable is of the type CCanvasStatic. I add OnPaint(), OnLButtonDown() and OnLButtonUp() to the class CCanvasStatic.

My OnLButtonDown() and OnLButtonUp() are like these:

void CCanvasStatic::OnLButtonDown(UINT nFlags, CPoint point)
{
    SetCapture ();
    if (this == GetCapture ())  
        (*m_pBGDlg->m_Shape).m_ptStart = point;

    CStatic::OnLButtonDown(nFlags, point);
}

void CCanvasStatic::OnLButtonUp(UINT nFlags, CPoint point)
{
    if (some expression)
    {
        (*m_pBGDlg->m_Shape).m_ptEnd = point;  
        Invalidate ();
        UpdateWindow ();
    }   
    ReleaseCapture ();

    CStatic::OnLButtonUp(nFlags, point);
}

I expect Invalidate() to erase previous drawing so that each time I draw a new line, all lines I have drawn are redrawn (I want this because I use an array to record all lines and use a loop in OnPaint() to deal with the issue of repaint).

But Invalidate() does not do what I expect. How can erase previous drawing? Thank you!

  • 1
    There aren't any simpler solutions. I'm having a feeling, that you aren't worried about complexity, though, but something else. What issue are you really trying to solve? – IInspectable Dec 07 '17 at 09:08
  • Dear @IInspectable Thank you very much. I am a newbie in MFC. I have edited my problem. – user8903219 Dec 07 '17 at 13:15
  • You will get lots and lots of messages. Only redraw if the point actually changes and use InvalidateRect. – Andrew Truckle Dec 07 '17 at 14:42
  • The [CWnd::Invalidate](https://msdn.microsoft.com/en-us/library/ax04k970.aspx) call has a *bErase* parameter, that's set to `TRUE` by default. Your control *does* get a chance to erase its contents. If it doesn't, you have either overridden the [CWnd::OnEraseBkgnd](https://msdn.microsoft.com/en-us/library/a0a52fkz.aspx) member, without properly clearing the background, or you assigned a null brush when registering your class. Since you subclassed a standard control, the latter is not the case. – IInspectable Dec 07 '17 at 14:55
  • 1
    P.S.: Being new to MFC is not a problem in and of itself. Being new to MFC *and* the Windows API is. You need to understand the underlying API, to make any sense out of the abstraction. Otherwise, you'll keep running into simple issues, that you have no way of analyzing, comprehending, or solving. – IInspectable Dec 07 '17 at 15:08
  • A [mcve] would help in understanding the problem. – zett42 Dec 07 '17 at 16:21
  • What's the type of the static control? A frame, an image, an owner-drawn one? Each has its own painting implementation. Does it provide a mechanism to erase the background? Maybe check whether the `WM_ERASEBKGND` message is actually processed, eg check the return value of the `OnEraseBkgnd` event ot the base class. Pls also note that a frame control only paints the... frame not its interior. – Constantine Georgiou Dec 07 '17 at 21:37

0 Answers0