2013-07-15 115 views
-1

看看這個代碼,並請你告訴我,爲什麼當我編譯它,我看到一個空白屏幕:我的LoadImage不工作 - 我需要知道爲什麼

#include <windows.h> 
#include <fstream.h> 
#include <stdlib.h> 

static char g_szClassName[] = "TEST"; 
static HINSTANCE g_hInst = NULL; 

int savenumber = 1; 
char savestring[] = "abc"; 

HWND hwnd; 
HDC hdcWindow; 
PAINTSTRUCT ps; 
RECT rc; 
//HBITMAP mario1,mario2,mario3,mario4,mario5,mario6, 
//  block1,block2,block3,scene1,scene1mask; 
HBITMAP scene1; 
BITMAP bm; 

LRESULT CALLBACK 
WndProc(HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam) 
{ 
     HBITMAP g_hbitmap = NULL; 

switch(nMsg) 
{ 
    case WM_CREATE: 
     g_hbitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), 
            "C:\\Users\\Onwer\\Desktop\\Cpp code Initiate\\Jacob       GRAPIC#3 cpp renewal!\\G3-Images\\scene.bmp", 
            IMAGE_BITMAP, 
            640, 
            320, 
            LR_LOADFROMFILE); 

     /* 
     mario1 = LoadBitmap(g_hInst, "MARIO1BMP"); 
     mario2 = LoadBitmap(g_hInst, "MARIO2BMP"); 
     mario3 = LoadBitmap(g_hInst, "MARIO3BMP"); 
     mario4 = LoadBitmap(g_hInst, "MARIO4BMP"); 
     mario5 = LoadBitmap(g_hInst, "MARIO5BMP"); 
     mario6 = LoadBitmap(g_hInst, "MARIO6BMP"); 
     block1 = LoadBitmap(g_hInst, "BLOCK1BMP"); 
     block2 = LoadBitmap(g_hInst, "BLCOK2BMP"); 
     block3 = LoadBitmap(g_hInst, "BLOCK3BMP"); 
     scene1 = LoadBitmap(g_hInst, "SCENE1BMP"); 
     scene1mask = LoadBitmap(g_hInst, "SCENE1MASKBMP"); 
     */ 
     scene1 = LoadBitmap(g_hInst, "SCENE1BMP"); 

    break; 
    case WM_TIMER: 
    break; 
    case WM_PAINT: 
     hdcWindow = BeginPaint(hwnd, &ps); 
     GetClientRect (hwnd, &rc); 

     drawScene (hdcWindow); 

     //DrawText(hdcWindow,"This Is a Test!", -1, &rc, 
     //DT_SINGLELINE | DT_TOP | DT_TOP); 

     EndPaint(hwnd, &ps);   
    break; 
    case WM_COMMAND: 
    break; 
    case WM_CLOSE: 
    DestroyWindow(hwnd); 
    break; 
    case WM_DESTROY: 

     /* 
     DeleteObject(mario1); 
     DeleteObject(mario2); 
     DeleteObject(mario3); 
     DeleteObject(mario4); 
     DeleteObject(mario5); 
     DeleteObject(mario6); 
     DeleteObject(block1); 
     DeleteObject(block2); 
     DeleteObject(block3); 
     */ 

     PostQuitMessage(0); 
    break; 
    default: 
    return DefWindowProc(hwnd, nMsg, wParam, lParam); 
} 

return 0; 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
LPSTR lpCmdLine, int nCmdShow) 
{ 
ofstream SaveFile ("save.txt"); 
SaveFile << savenumber << '\n' << savestring; 

WNDCLASSEX WndClass; 
HWND hwndMain; 
MSG Msg; 
g_hInst = hInstance; 

WndClass.cbSize  = sizeof(WNDCLASSEX); 
WndClass.style   = 0; 
WndClass.lpfnWndProc = WndProc; 
WndClass.cbClsExtra = 0; 
WndClass.cbWndExtra = 0; 
WndClass.hInstance  = g_hInst; 
WndClass.hIcon   = LoadIcon(NULL, IDI_APPLICATION); 
WndClass.hCursor  = LoadCursor(NULL, IDC_ARROW); 
WndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE); 
WndClass.lpszMenuName = NULL; 
WndClass.lpszClassName = g_szClassName; 
WndClass.hIconSm  = LoadIcon(NULL, IDI_APPLICATION); 


if(!RegisterClassEx(&WndClass)) 
{ 
    MessageBox(0, "Window Registration Failed!", "Error!", 
    MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL); 
    return 0; 
} 

hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE, 
    g_szClassName, 
    "TEST", 
    WS_OVERLAPPEDWINDOW, 
    //CW_USEDEFAULT, CW_USEDEFAULT, 677, 358, 
    CW_USEDEFAULT, CW_USEDEFAULT, 800, 500, 
    NULL, NULL, g_hInst, NULL); 


if(hwnd == NULL) 
{ 
    MessageBox(0, "Window Creation Failed!", "Error!", 
    MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL); 
    return 0; 
} 

ShowWindow(hwnd, nCmdShow); 
UpdateWindow(hwnd); 

while(GetMessage(&Msg, NULL, 0, 0)) 
{ 
    TranslateMessage(&Msg); 
    DispatchMessage(&Msg); 
} 
return Msg.wParam; 
} 

我需要知道爲什麼沒有按的LoadImage」 t顯示指定的圖片。它甚至不應該?如果它不應該t:告訴我什麼函數,我應該使用loadImage,這是C++的Windows.h的一部分。我正在使用Dev-C++編譯器。

+0

'drawScene'中的代碼被註釋掉了。如果這就是你當前的實現方式,那麼沒有任何東西可以吸引到設備上。 –

+0

...是啊我知道這是從早些時候,我會刪除... ...但IM特別談論WM_CREATE內的LoadImage。實際上,我沒有評論說這是無效的,因爲裏面的東西不起作用 - 所以如果你能告訴我如何讓它工作,這將是很好的,但我不指望你。 –

+0

任何看到隊長表現出色的人都應該知道,我剛剛刪除了他提到的那個部分,因爲它並不意味着在這裏。謝謝。 –

回答

2

全部​​它是否加載圖像,沒有別的。如果您希望應用程序在窗口中繪製圖像,則必須自己完成。

drawScene()的內容被註釋掉(因爲在編輯中被刪除),並且您的WM_PAINT處理程序不執行任何繪圖操作。 如果您的位圖已成功加載,則在處理WM_PAINT消息時,您需要執行類似下面的操作。

hdcMemory = CreateCompatibleDC(hdcWindow); 

HGDIOBJ oldBitmap = SelectObject(hdcMemory, g_hbitmap); 
BitBlt(hdcWindow, 0, 0, 640, 320, hdcMemory, 0, 0, SRCCOPY); 
SelectObject(hdcMemory, oldBitmap); 

DeleteDC(hdcMemory); 
+0

多數民衆贊成在偉大和所有,我知道它應該工作,我已經加載了一個圖像以不同的方式之前,然後使用它(在不同的程序),它運作良好,但在這個程序中,我得到一個鏈接器錯誤對應於「SelectObject @ 8「,」BitBlt @ 32「和」DeleteDC @ 4「。 –

+0

我真的希望這個程序能夠工作D:但是無論我使用那些東西時我做了什麼,我都會收到Linker錯誤。 –

+0

您需要鏈接gdi32.lib庫。檢查你的項目設置,確保它在鏈接器配置中。 –

相關問題