程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> windows編程-Window編程閃屏雙緩沖就顯示白屏問題,求教,謝謝

windows編程-Window編程閃屏雙緩沖就顯示白屏問題,求教,謝謝

編輯:編程綜合問答
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;
}

最佳回答:


看這四句:
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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved