c++ - BitBlt 将位图放到启动屏幕上

标签 c++ visual-c++ visual-studio-2008 bitmap

我正在为一个简单的 Direct3D 游戏开发启动屏幕,尽管屏幕本身已正确创建和销毁,但要投影到启动屏幕上的 BITMAP 并未显示。到目前为止我有这个:

//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。位图 bm 正在加载,通过窗口的 900x600 尺寸显示(与splash.bmp 相同)。然而,该窗口只是一个黑屏,而不是splash.bmp 中包含的 Herobrine 脸部 xD

最佳答案

这是您几乎肯定会盯着代码看太久而错过了明显内容的情况之一。

您正在创建hdcMem,然后立即BitBlt进入启动屏幕。在这些之间,您无疑需要 SelectObject 将您的位图选择到 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);

关于c++ - BitBlt 将位图放到启动屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17762783/

相关文章:

C++ WndProc 获取消息的X和Y坐标

c++ - STL 算法是否针对速度进行了优化?

c++ - Qt 滑动动画到其他 .ui 文件

C++ 类依赖 typedef 在类外部声明

c++ - 使用带链表的复制构造函数

c++ - Visual Studio 2008 出现错误 C3861 : '__cpuidex' : identifier not found when building OpenCV 2. 4.11

c++ - 如何在 C++ 中组合 vector 的元素

c++ - 构造错误; VS2017 C++ 编译器回归?

c# - 什么相当于 Visual Studio 2010 中的 Visual Studio 2008 对象测试台?

visual-studio - 自动化 Visual Studio 安装项目