Window编程闪屏双缓冲就显示白屏问题,求教,谢谢

一直试验不成功,不知道原因在哪里,用窗口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;
}

2个回答

看这四句:
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(), 因为这样可以省下你两三天的时间。

多谢提醒,今天早上发现有不少小错误,
1) HDC声明出错
2)CASE 块没加{}
学习的有点急躁,所以出错了许多.
改正以上错误,成功玩出来了.
结帖.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问