一直試驗不成功,不知道原因在哪裡,用窗口hdc就可以,用創建的hMemDc就不行...一直顯示白屏,在這裡求教?
http://pan.baidu.com/s/1ntIhANB 帖了百度盤的鏈接,裡面有代碼.
代碼就是加載了背景圖,和一張圖片.然後移動的圖片,出現閃屏,所以想用雙緩沖來弄.
懂的人,幫個忙,謝謝.折騰了三天了.
下面是核心代碼,真心不知道哪裡出錯了!!!鏈接裡有工程代碼,有點亂,見諒...在學習中..麻煩懂的人幫我看一下.真心請教,謝謝大家了.
static HBITMAP hCloudBMP;
static HDC cloudDC;
BITMAP bm; //for get cloud bmp size
static HBITMAP hBgBMP;
static HDC bgDC;
BITMAP bgBm;
static HDC hMemDC;
static HBITMAP hTemp;
BITMAP bgTest;
HDC hdc;
int x,y=0;
int step=0;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
RECT rect;
switch (message)
{
case WM_CREATE:
hCloudBMP=(HBITMAP)LoadImage(NULL,"1.bmp",IMAGE_BITMAP,0,0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
cloudDC =CreateCompatibleDC(hdc);
SelectObject(cloudDC,hCloudBMP);
ReleaseDC(hWnd, hdc);
GetObject(hCloudBMP,sizeof(BITMAP), &bm);
//x=bm.bmWidth;
//y=bm.bmHeight;
hBgBMP=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,0,0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
bgDC=CreateCompatibleDC(hdc);
SelectObject(bgDC,hBgBMP);
ReleaseDC(hWnd, hdc);
GetObject(hBgBMP,sizeof(BITMAP),&bgBm);
//hTemp=(HBITMAP)LoadImage(NULL,"test.bmp",IMAGE_BITMAP,0,0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
hMemDC=CreateCompatibleDC(hdc);
hTemp=CreateCompatibleBitmap(hdc, 1280, 720);
SelectObject(hMemDC, hTemp);
//FillRect(hMemDC,
//DeleteObject(hTemp);
ReleaseDC(hWnd,hdc);
SetTimer(hWnd,100,100,NULL);
break;
case WM_TIMER:
x+=5;
y+=3;
InvalidateRect(hWnd,NULL,true);
//UpdateWindow(hWnd);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
PAINTSTRUCT ps;
hdc=BeginPaint(hWnd, &ps);
BitBlt(hMemDC,0,0,bgBm.bmWidth,bgBm.bmHeight,bgDC,0,0,SRCCOPY);
if(step==0)
{
BitBlt(hMemDC,x,y,bm.bmWidth,bm.bmHeight,cloudDC,0,0,SRCCOPY);
}
else
{
//BitBlt(hdc,x,y,bm.bmWidth,bm.bmHeight,hMemDC,0,0,SRCCOPY);
}
//BitBlt(hdc,0,0,100,100,hMemDC,0,0,SRCCOPY);
BitBlt(hdc,0,0,1280,720,hMemDC,0,0,SRCCOPY);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
DeleteDC(cloudDC);
DeleteObject(hCloudBMP);
DeleteDC(bgDC);
DeleteObject(hBgBMP);
DeleteDC(hMemDC);
KillTimer(hWnd,10);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
看這四句:
ReleaseDC(hWnd, hdc);
GetObject(hBgBMP,sizeof(BITMAP),&bgBm);
//hTemp=(HBITMAP)LoadImage(NULL,"test.bmp",IMAGE_BITMAP,0,0, LR_DEFAULTSIZE|LR_LOADFROMFILE);
hMemDC=CreateCompatibleDC(hdc);/////////你這句的hdc早就被第一句ReleaseDC掉了。所以這個api調用肯定是失敗的。
hTemp=CreateCompatibleBitmap(hdc, 1280, 720);
建議樓主多做下返回值檢查,失敗的時候調用GetLastError(), 因為這樣可以省下你兩三天的時間。