2016-10-10 40 views
1

社區,我有一個小問題,我需要幫助。 我想加載一個位圖,並想用UpdatelayerdWindow來顯示它。我寫了下面的源代碼。但沒有出現。 當我使用BitBlt功能時,圖片出現。 我在哪裏有我的源代碼中的錯誤?C++,GDI +,加載位圖並使用UpdateLayeredWindow來顯示位圖

ATOM SplashRegisterClass(HINSTANCE hInstance) { 
    WNDCLASSEX wcex; 
    wcex.cbSize = sizeof(WNDCLASSEX); 
    wcex.style = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = WndProc; 
    wcex.cbClsExtra = 0; 
    wcex.cbWndExtra = 0; 
    wcex.hInstance = hInstance; 
    wcex.hIcon = NULL; 
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); 
    wcex.lpszMenuName = NULL; 
    wcex.lpszClassName = szWindowClass; 
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 
    return RegisterClassEx(&wcex); 
} 

void ShowBitMap(HWND hwnd, HINSTANCE hInstance) { 
    int nuXPos = 0; 
    int nuYPos = 0; 
    int width = 0; 
    int height = 0; 
    BITMAP bm; 

    hbmpSplash = (HBITMAP)::LoadImage(hInstance, L"software.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); 
    if (hbmpSplash == NULL) 
    { 
     ::MessageBox(NULL, __T("Image not Found"), __T("Error"), MB_OK); 
    } 

    GetObject(hbmpSplash, sizeof(bm), &bm); 
    SIZE size = { bm.bmHeight, bm.bmWidth }; 
    width = bm.bmWidth; 
    height = bm.bmHeight; 

    POINT ptZero = { 0 }; 
    HMONITOR hmonPrimary = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY); 
    MONITORINFO monitorinfo = { 0 }; 
    monitorinfo.cbSize = sizeof(monitorinfo); 
    GetMonitorInfo(hmonPrimary, &monitorinfo); 

    const RECT & rcWork = monitorinfo.rcWork; 

    nuXPos = rcWork.left + (rcWork.right - rcWork.left)/2; 
    nuYPos = rcWork.top + (rcWork.bottom - rcWork.top)/2; 

    nuXPos = nuXPos - (175/2); 
    nuYPos = nuYPos - (170/2); 

    HDC hdcScreen = GetDC(NULL); 
    HDC hdcneu = CreateCompatibleDC(hdcScreen); 
    HDC hdcMem = CreateCompatibleDC(hdcScreen); 
    HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpSplash); 

    POINT position = { nuXPos, nuYPos }; 

    BLENDFUNCTION blend = { 0 }; 
    blend.BlendOp = AC_SRC_OVER; 
    blend.SourceConstantAlpha = 255; 
    blend.AlphaFormat = AC_SRC_ALPHA; 
    //Next line only for test! 
    //::BitBlt(hdcScreen, nuXPos, nuYPos, width, height, hdcMem, 0, 0, SRCCOPY); 

    //Show the Bitmap 
    if (!UpdateLayeredWindow(splashOwner, hdcScreen, &position, &size, hdcMem, NULL, RGB(0, 0, 0), &blend, ULW_ALPHA)) 
    { 
     //int LastError = GetLastError(); 
    } 

    SelectObject(hdcMem, hbmpOld); 
    DeleteDC(hdcMem); 
    ReleaseDC(NULL, hdcScreen); 
} 

HWND CreateSplashWindow(HINSTANCE hInstance, int nCmdShow) { 
    SplashRegisterClass(hInstance); 

    splashOwner = CreateWindow(szWindowClass, NULL, WS_POPUP, 0, 0, 100, 100, NULL, NULL, hInstance, NULL); 
    if (!splashOwner) 
    { 
     ::MessageBox(NULL, __T("Error"), __T("Error"), MB_OK); 
    } 

    ShowBitMap(splashOwner, hInstance); 

    return CreateWindowEx(WS_EX_LAYERED, szWindowClass, 0, WS_POPUP, 0, 0, 100, 100, splashOwner, NULL, hInstance, NULL); 
} 

我想在主程序運行時刪除所有內容。我可以用這個功能嗎?

void DeleteSplashWindow(){ 
    ReleaseDC(splashOwner, 0); 
} 

感謝您的幫助!

+0

你好,這是一個小更新。我忘了「ShowWindow(hWnd,nCmdShow)」命令,現在我可以看到窗口了!但是我不能刪除/銷燬窗口,也不能刪除/銷燬主窗口。有人有解決這個問題的辦法嗎? –

回答

0
hbmpSplash = (HBITMAP)::LoadImage(hInstance, L"software.bmp", 
    IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); 

此函數失敗,應該給出錯誤。將其更改爲

hbmpSplash = (HBITMAP)::LoadImage(0, L"software.bmp", 
    IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); 

創建分層窗口時,您必須致電SetLayeredWindowAttributes。示例

HWND CreateSplashWindow(HINSTANCE hInstance, int nCmdShow) { 
    ... 
    HWND hwnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass, 
      0, WS_POPUP, 0, 0, 100, 100, 
      splashOwner, NULL, hInstance, NULL); 

    SetLayeredWindowAttributes(hwnd, transparentColor, alpha, LWA_COLORKEY | LWA_ALPHA); 
    return hwnd; 
} 
+0

感謝您的幫助!這兩個功能都在工作! –

+0

但是,當我調用UpdateLayeredWindow時,主要問題是我無法看到位圖!我不知道爲什麼?! –

0

感謝您的幫助,我自己到了解決方案。 1.我忘了ShowWindow(hwnd,SW_SHOW);和 2. void DeleteSplashWindow(){ ReleaseDC(splashOwner, NULL); ShowWindow(splashOwner, NULL); }