C++绘制位图失败

根据游戏编程入门(第四版)的范例文件写的程序,无法绘制出位图,可以创建一个黑色背景窗口

// 请把代码文本粘贴到下方(请勿用图片代替代码)
#include <windows.h>
#include <iostream>
#include <time.h>

using namespace std;
const string APPTITLE = "Game Loop";
HWND window;
HDC device;
bool gameover = false;
//load and draws a bitmap from a title and then frees the memory
//(not really suitale for a game loop but it's self contained)
void DrawBitmap(char *filename,int x,int y)
{
    //load the bitmap image
    HBITMAP image = (HBITMAP)LoadImage(0,filename,IMAGE_BITMAP,
    0,0,LR_LOADFROMFILE);
    //read the bitmap's properties
    BITMAP bm;
    GetObject(image,sizeof(BITMAP),&bm);
    //create a device context for the bitmap
    HDC hdcImage = CreateCompatibleDC(device);
    SelectObject(hdcImage,image);
    //draw the bitmap to the window (bit block transfer)
    BitBlt(
        device,                    //destination device context    
        x,y,                    //x,y location on destination
        bm.bmWidth,bm.bmHeight,    //width,height of source bitmap
        hdcImage,                //source bitmap device context
        0,0,                    //start x,y on source bitmap
        SRCCOPY);                 //blit method
    //delete the device context and bitmap
    DeleteDC(hdcImage);
    DeleteObject((HBITMAP)image);
}
//    startup and loading code goes here
bool Game_Init()
{
    //start up the random number generator
    srand(time(NULL));
    return 1; 
} 
//Update function called from inside game loop
void Game_Run()
{
    if(gameover == true) return;
    //get the drawing surface
    RECT rect;
    GetClientRect(window,&rect);
    //draw bitmap at random location
    int x = rand()%(rect.right-rect.left);
    int y = rand()%(rect.bottom-rect.top);
    
    DrawBitmap("C.bmp",x,y);
}
//shutdown code
void Game_End(){
    //free the device
    ReleaseDC(window,device);
}
/* This is where all the input to the window goes to */
LRESULT CALLBACK WinProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    
    switch(Message) {
        
        /* Upon destruction, tell the main thread to stop */
        case WM_DESTROY:
        
            gameover = true;
            PostQuitMessage(0);
            break;
    }

        /* All other messages (a lot of them) are processed using default procedures */
        return DefWindowProc(hwnd, Message, wParam, lParam);
}

//MyRegisterClass function sets program window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    //create the window class structure
    WNDCLASSEX wc;
    wc.cbSize        = sizeof(WNDCLASSEX);
    //fill the struct with info
    wc.style        = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc    = (WNDPROC)WinProc;
    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= APPTITLE.c_str();
    wc.hIconSm        = NULL;
    //set up the window with the class info

    return RegisterClassEx(&wc);
 
}
// Helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
{
    //create a new window
    window = CreateWindow(
    APPTITLE.c_str(),    //window class
    APPTITLE.c_str(),    //title bar
    WS_OVERLAPPEDWINDOW,//window style
    CW_USEDEFAULT,        //x position of window
    CW_USEDEFAULT,        //y position of window
    640,                //width of the window
    480,                //height of the window
    NULL,                //parent window
    NULL,                //menu
    hInstance,            //application instance
    NULL);                //window parameters
//was there an error creating the window?
if (window == 0) return 0;
    //display the window
    ShowWindow(window,nCmdShow);
    UpdateWindow(window);
    //get device context for drawing
    device = GetDC(window);
    return 1;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
     
    MSG msg; /* A temporary location for all messages */
    //create window
    MyRegisterClass(hInstance);
    if (!InitInstance (hInstance,nCmdShow)) return 0;
    //initialize the game
    if(!Game_Init()) return 0;
    //main message loop
    while (!gameover)
    {
        //process Windows events
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        //process game loop
        Game_Run();
    }
    //free game resources
    Game_End();
    return msg.wParam;
}

困扰了很长时间了,希望有C++大神解答

阅读 3.8k
1 个回答

代码没有问题,怀疑是你的图片文件C.bmp没放对地方。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题