2013-04-02 117 views
1

該程序只顯示一個空白窗口。窗口無法加載位圖圖像

我想將位圖圖像隨機加載到屏幕上。我無法弄清楚這一點。即使我在這個程序中給圖像賦予不同的名字,程序仍然執行時沒有任何錯誤,並顯示空白窗口。

請幫幫我!

#ifndef UNICODE 
#define UNICODE 
#endif 

/*have used unicode */ 

#include<Windows.h> 
#include<iostream> 
#include<time.h> 
using namespace std; 

bool gameover =FALSE; 
const wchar_t classname[] = L"My first procedure and main"; 
HWND window; 
HDC device; 
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam); 


ATOM MyRegisterClass(HINSTANCE hInstance) 
{ 
    //set the new windows properties 
    WNDCLASS wc = {}; 

    wc.lpfnWndProc=(WNDPROC)WindowProc; 
    wc.hInstance=hInstance; 
    wc.style=CS_HREDRAW|CS_VREDRAW; 
    wc.hbrBackground=(HBRUSH)GetStockObject(COLOR_WINDOW +1); 
    wc.lpszClassName=classname; 

    return RegisterClass(&wc); 
} 

bool InitInstance(HINSTANCE hInstance,int nCmdShow) 
{ 
    //Create a new window 
    window=CreateWindowEx(0, 
          classname, 
          L"My Program", 
          WS_OVERLAPPEDWINDOW, 
          CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, 
          NULL, 
          NULL, 
          hInstance, 
          NULL); 

    //Any error in creating the window ? 
    if(window==0) 
     return 0; 

    //Display the window 
    ShowWindow(window,nCmdShow); 
    UpdateWindow(window); 
    device=GetDC(window); 
    return 1; 
} 

bool Game_Init() 
{ 
    srand(time(NULL)); 
    return 1; 
} 

// This function handles the loading of bitmap image into the window 
void DrawBitmap(char *filename,int x,int y) 
{ 
    //Load the bitmap image 
    HBITMAP image=(HBITMAP)LoadImage(0,L"image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); 
    BITMAP bm; 
    GetObject(image,sizeof(BITMAP),&bm); 
    HDC hdcdevice=CreateCompatibleDC(device); 
    SelectObject(hdcdevice,image); 
    BitBlt(device,x,y,bm.bmWidth,bm.bmHeight,hdcdevice,0,0,SRCCOPY); 

    //delete the device context and bitmap 
    DeleteDC(hdcdevice); 
    DeleteObject((HBITMAP)image); 
} 

void Game_Run() 
{ 
    if(gameover==true) 
     return; 
    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("image.bmp",x,y); 
} 

void Game_End() 
{ 
    //Free the device 
    ReleaseDC(window,device); 
} 

int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE,PWSTR PCmdLine,int nCmdShow) 
{ 
    //Register the class  
    MyRegisterClass(hInstance); 

    //Initialize application 
    if(!InitInstance(hInstance,nCmdShow)) 
     return 0; 

    //Declare variables 
    MSG msg ={ }; 

    //Initialize the game 
    if(!Game_Init()) 
     return 0; 

    //main message loop 
    while(!gameover) 
    { 
     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) 
     { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
     Game_Run(); 
    } 
    Game_End(); 

    return msg.wParam; 
} 

//This is window procedure function for handling closing of windows. 
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) 
{ 
    switch(uMsg) 
    { 
     case WM_CLOSE: 
      if(MessageBox(hwnd,L"Do you want to close this window",NULL,MB_OKCANCEL)==IDOK)   
       gameover=true; 
      DestroyWindow(hwnd); 


    } 
    return DefWindowProc(hwnd,uMsg,wParam,lParam); 
} 

This is the output of my program

+0

我試圖添加圖像調試文件夾也不過 –

+0

如果程序顯示一個空白窗口,它正在執行。你的問題的標題很混亂。 – Oswald

+4

要得到任何錯誤,你首先應該檢查錯誤。或者,使用調試器並通過它檢查所有返回值。 –

回答

3

傳遞給函數的LoadImage BMP文件的完整路徑。同時檢查LoadImage函數的返回值。

對於如。

HBITMAP image=(HBITMAP)LoadImage(0,L"c:\\wherever\\whatever\\image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); 
if(image == NULL) 
    MessageBox(0, "Couldn't load the image", "Error", MB_OK); 
else 
    .... Whatever else ...... 

幾乎可以肯定,您的LoadImage無法找到「image.bmp」,因此返回NULL。

+0

消息框彈出顯示無法加載image.I嘗試包括路徑也但它不工作。 –

+0

@NaveenGabriel - 你可以複製粘貼你的確切LoadImage調用這裏的路徑? – user93353

+0

確定圖像的值是0xcccccccc。它是什麼意思? –

0

檢查手柄的值,如「設備」和「圖像」。

嘗試並瀏覽代碼,看看是否一切正常。

您也可以選擇使用LoadBitmap()。

+0

我正在使用64位Windows 8操作系統。 –

+0

你的位圖文件的格式是什麼? – james82345

+0

它是一個8位位圖嗎? – james82345