所以我的老师给了我们一大堆用于双缓冲的代码。
他说“在这里,使用这个代码,所以你不必坐在那里几个小时,找出如何” 除了他的代码不起作用。
他最初使用hdc是未定义的。我尝试将它放在参数列表中,但这是不行的。
这是他给我们的代码:
// Create a backbufer bmp bufer to draw to in memory.
RECT rcClient;
::GetClientRect(hwnd, &rcClient);
int left = rcClient.left;
int top = rcClient.top;
int width = rcClient.right - rcClient.left;
int height = rcClient.bottom - rcClient.top;
HDC hdcMem = ::CreateCompatibleDC(hdc);
const int nMemDC = ::SaveDC(hdcMem);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hdc, width, height);
::SelectObject(hdcMem, hBitmap);
Graphics graphics(hdcMem);
SolidBrush back(Color(255,255,255));
graphics.FillRectangle(&back, left, top, width, height);
// Draw to backbufer bitmap here.
// End draw to backbufer bitmap bufer.
// Swap bufers ie. push memory backbufer to the screen frontbufer
RECT rcClip;
::GetClipBox(hdc, &rcClip);
left = rcClip.left;
top = rcClip.top;
width = rcClip.right - rcClip.left;
height = rcClip.bottom - rcClip.top;
::BitBlt(hdc, left, top, width, height, hdcMem, left, top, SRCCOPY);
::RestoreDC(hdcMem, nMemDC);
::DeleteObject(hBitmap);
这里是我遇到错误的地方:HDC hdcMem = ::CreateCompatibleDC(hdc);
我试图宣布像这样的HDC
HDC hdc = (HDC)BeginPaint((LPPAINTSTRUCT)AfxGetApp()->m_pMainWnd->GetSafeHwnd());
但那不编译。我该怎么处理这个hdc?
答案 0 :(得分:0)
由BeginPaint返回HDC,可能在此代码之前立即调用。 BeginPaint有两个参数,你试图只用一个参数调用它。你有早期练习处理BeginPaint吗?
答案 1 :(得分:0)
所以感谢这里的各种答案。我对此的了解仍然相当新,但在我的同学们的帮助下,我能够找到解决方案。可悲的是,我仍然不知道如何处理HDC,这是我的第一个问题,
HDC hdcMem = ::CreateCompatibleDC(hdc); was able to be replaced by
HDC hdcMem = ::CreateCompatibleDC(dc);
并且许多产生警告的其他代码(例如hwnd)被删除了,它运行正常。