我的LoadImage不工作 - 我需要知道原因

时间:2013-07-15 21:57:30

标签: c++ function bitmap

看看这段代码,请告诉我为什么编译时我会看到一个空白的白色屏幕:

#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:告诉我我应该使用的loadImage有哪些函数,这是C ++中Windows.h的一部分。我正在使用Dev-C ++编译器。

1 个答案:

答案 0 :(得分:2)

所有LoadImage都会加载图片,没有别的。如果您希望应用程序在窗口中绘制图像,则必须自己完成。

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);