Question

Hello guys I have a problem with double buffering. I don't know why, but my text isn't drawing (without double buffering text is drawing).

Here is code:

m_hDC = BeginPaint(m_hWnd, &m_ps);

m_graphics = new Graphics(m_hDC);
memDC = CreateCompatibleDC(m_hDC);
pMemGraphics = new Graphics(memDC);

pMemGraphics->DrawString(L"Hello world!", -1, font, PointF(100, 100), &brush);

BitBlt(m_hDC, 0, 0, 500, 200, memDC, 0, 0, SRCCOPY);
EndPaint(m_hWnd, &m_ps);

delete(pMemGraphics);
delete(m_graphics);

Whats wrong?

Était-ce utile?

La solution

CreateCompatibleDC does not create a canvas on which you can draw. You have to create a bitmap and assign it to the context.

Try this:

m_hDC = BeginPaint(m_hWnd, &m_ps);

memDC = CreateCompatibleDC(m_hDC);
HBITMAP hBM = CreateCompatibleBitmap(m_hDC, 500, 200);
SelectObject(memDC, hBM);   
// Now you can draw on memDC

// Fill with white color
RECT r;
SetRect(&r, 0, 0, 500, 200);
FillRect(memDC, &r, GetStockObject(WHITE_BRUSH));

// Draw text
::TextOut(memDC, 100, 100, "Hello world!", 12);

// Paint on window
BitBlt(m_hDC, 0, 0, 500, 200, memDC, 0, 0, SRCCOPY);

DeleteObject(hBM);
DeleteDC(memDC);

EndPaint(m_hWnd, &m_ps);

Autres conseils

This isn't GDI+ related. See the comments @ http://www.cplusplus.com/forum/windows/35484/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top