BitBlt位图到初始屏幕 [英] BitBlt a bitmap onto a splash screen

查看:69
本文介绍了BitBlt位图到初始屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个简单的Direct3D游戏制作启动画面,尽管屏幕本身是正确创建和销毁的,但要投影到启动画面上的 BITMAP 却没有显示.到目前为止,我已经知道了:

I am working on a splash screen for a simple Direct3D game and although the screen itself is created and destroyed properly, the BITMAP meant to be projected onto the splash screen isn't showing up. I have this so far:

//variable declarations
HWND splashWindow = NULL;
BITMAP bm;

//window procedure
LRESULT WINAPI SplashProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(splashWindow, &ps);
            HDC hdcMem = CreateCompatibleDC(hdc);
            BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
            DeleteDC(hdcMem);
            EndPaint(splashWindow, &ps);
        }
    }
    return DefWindowProc( hWnd, msg, wParam, lParam );
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    //initialize splash window settings
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX); 
    wc.style         = CS_VREDRAW | CS_HREDRAW;
    wc.lpfnWndProc   = SplashProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "Splash Window";
    wc.hIconSm       = NULL;
    RegisterClassEx(&wc);

    //create and draw the splash window
    HBITMAP splashBMP = (HBITMAP)LoadImage(hInstance, "assets\\textures\\splash.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    GetObject(splashBMP, sizeof(bm), &bm);
    //^-splashBMP to bm - used here so the window has proper dimensions
    splashWindow = CreateWindow("Splash Window", NULL,
        WS_POPUPWINDOW | WS_EX_TOPMOST, CW_USEDEFAULT, CW_USEDEFAULT,
        bm.bmWidth, bm.bmHeight, NULL, NULL, hInstance, NULL);
    if (splashWindow == 0) return 0;

    ShowWindow(splashWindow, nCmdShow);
    UpdateWindow(splashWindow);
    //after the game loads the splash screen is destroyed through DestroyWindow(splashWindow);
    //and splashBMP is released by calling DeleteObject(splashBMP);

真的,唯一重要的代码是处理 WM_PAINT 消息的 SplashProc .通过窗口的900x600尺寸(与splash.bmp相同)可以很好地显示位图 bm .但是,该窗口只是一个黑屏,而不是splash.bmp xD

Really, the only important code is SplashProc handling the WM_PAINT message. The bitmap bm is loading fine shown through the window's 900x600 dimensions (same as splash.bmp). However, that window is just a black screen instead of the herobrine face contained in splash.bmp xD

推荐答案

这是您几乎可以肯定地盯着代码的地方之一,只要您错过了显而易见的地方即可.

This is one of those where you've almost certainly stared at the code so long you missed the obvious.

您正在创建 hdcMem ,然后立即 BitBlt 进入初始屏幕.在这两者之间,您无疑希望使用 SelectObject 将您的位图选择为 hdcMem .

You're creating hdcMem, and then immediately BitBlting to the splash screen. Between those you undoubtedly want a SelectObject to select your bitmap into hdcMem.

PAINTSTRUCT ps;
HDC hdc = BeginPaint(splashWindow, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);

// You need to add this
HBITMAP oldBmp = SelectObject(hdcMem, splashBMP);    

BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

// ...and this
SelectObject(hdcMem, OldBmp);

DeleteDC(hdcMem);
EndPaint(splashWindow, &ps);

这篇关于BitBlt位图到初始屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆