-1

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

#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++ 编译器。

4

1 回答 1

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);
于 2013-07-15T22:16:26.977 回答