• 5 Subscribers
  • 114 Views
0

In my MFC dialog program, i want to update a bitmap in the dialog window every 2 seconds. I use a memory bitmap buffer for drawing into, and OnTimer() is called from an MFC timer every 2seconds, and calls UpdateRateGraphics() which draws to a bitmap in memory. The OnPaint() function copys the memory bitmap to the screen window with bitblt(), whenever necessary. Initialization is done in OnInitDialog(). Drawing works, as long as i call the drawing function UpdateRateGraphics() from within OnPaint(), directly before the bitblt. When I call the drawing function from the timer function, which is what i want, only a black square is shown on the screen from the bitblt, but the bitmap content, at the moment only a somewhat shifted green square, is missing. Why is that?

GlobVars.h:
EXTERN HDC          memDC;                      // Device Context of a memory 
EXTERN HBITMAP      memBitmap;                  // A bitmap to draw onto, will be placed in the memory device context
EXTERN HBITMAP      *memBitmapOld;              // A bitmap to draw onto, will be placed in the memory device context
EXTERN HBRUSH       brush;                      // A GDI brush to define fill color etc.
EXTERN HBRUSH       brushOld;                   //


// Initialization, at the end of OnInitDialog():
CPaintDC    dc(this);
memDC = CreateCompatibleDC(dc.m_hDC);
memBitmap = CreateCompatibleBitmap(dc.m_hDC, 200, 200);
// Select it. An application cannot select a single bitmap into more than one DC at a time.
SelectObject(memDC, memBitmap);



void CmfritzmonDlg::UpdateRateGraphics()
{
    // Setup a GDIO brush object with fill color
    brush = CreateSolidBrush(RGB(0, 0xff, 0));
    // Select it
    brushOld = (HBRUSH) SelectObject(memDC, brush);
    // draw a rectangle using brush and memDC as destination
    Rectangle(memDC, 10, 10, 200, 200);
    SelectObject(memDC, brushOld);
    DeleteObject(brush);

    Invalidate();   // NEW, after adding this, it is working!
}



void CmfritzmonDlg::OnTimer(UINT_PTR nIDEvent)
{
    // cyclically called from SetTimer()
    UpdateRateGraphics();       // when called here, not ok !!!
    CDialogEx::OnTimer(nIDEvent);
}


void CmfritzmonDlg::OnPaint()
{
    if (IsIconic()) {
    } else {
        UpdateRateGraphics();       // When called here, OK !!!
        CPaintDC dc(this);
        BitBlt(dc.m_hDC, 0, 0, 200, 200, memDC, 0, 0, SRCCOPY);
    //  CDialogEx::OnPaint();
    }
}


void CmfritzmonDlg::OnDestroy()
{
    CDialogEx::OnDestroy();
    // Delete Objects for bitmap drawing
    SelectObject(memDC, memBitmapOld);
    DeleteObject(memBitmap);
    DeleteDC(memDC);
}

0 answers in total

Answer this questsion