我正在学习教程。我正在尝试在屏幕上绘制一个.bmp文件。它生成没有错误,但没有图像出现。根据这本书,我应该看到图像在随机的地方弹出。下面是我的代码。作者不建议将此技术用于绘制对象,而只是为了演示。万一你想知道。

图像是25x25正方形的红色正方形。

#include <windows.h>
#include <iostream>
#include <time.h>

using namespace std;

const string APPTITLE = "Game Loop";
HWND window;
HDC device;
bool gameover = false;


void DrawBitmap(char *filename, int x, int y)
{
 //load the bitmap image
 HBITMAP image = (HBITMAP)LoadImage(0,"c.bmp", IMAGE_BITMAP,0,0, LR_LOADFROMFILE);

 BITMAP bm;
 GetObject(image, sizeof(BITMAP), &bm);

 HDC hdcImage = CreateCompatibleDC(device);
 SelectObject(hdcImage,image);

 BitBlt(
  device,
  x,y,
  bm.bmWidth, bm.bmHeight,
  hdcImage,
  0,0,
  SRCCOPY);

 //deletec the device context and bitmap
 DeleteDC(hdcImage);
 DeleteObject((HBITMAP)image);
}

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

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("c.bmp",x,y);
}

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

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM WParam, LPARAM lparam)
{
 switch(message)
 {
  case WM_DESTROY:
   gameover = true;
   PostQuitMessage(0);
  break;
 }

 return DefWindowProc(hWnd, message, WParam, lparam);
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
 //set the new windows properties

 WNDCLASSEX wc;

 wc.cbSize  = sizeof(WNDCLASSEX);
 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;

 return RegisterClassEx(&wc);
}

bool InitInstance(HINSTANCE hInstance, int nCmdShow)
{
 //create a  new window
 window = CreateWindow(
  APPTITLE.c_str(),
  APPTITLE.c_str(),
  WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  640,480,
  NULL,
  NULL,
  hInstance,
  NULL);

 //was there an error creating the window ?
 if(window == 0) return 0;

 //display the window
 ShowWindow(window, nCmdShow);
 UpdateWindow(window);
 device = GetDC(window);

 return 1;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
      LPSTR lpCmdLine, int nCmdShow)
{
 //declare variables
 MSG msg;

 //register the class
 MyRegisterClass(hInstance);

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

 //initilize 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;
}

我不确定是否是因为我的图片位置错误。但是如果是这样的话。我认为这会引发错误。我将图像放在源文件夹的根目录下。

[编辑]

另外,当我重建时,我会收到警告,可能是原因,但这是警告
1>------ Rebuild All started: Project: Begin, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Begin', configuration 'Debug|Win32'
1>Compiling...
1>main.cpp
1>c:\users\numerical25\documents\visual studio 2008\projects\begin\begin\main.cpp(39) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Linking...
1>LINK : C:\Users\numerical25\Documents\Visual Studio 2008\Projects\Begin\Debug\Begin.exe not found or not built by the last incremental link; performing full link
1>Embedding manifest...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Build log was saved at "file://c:\Users\numerical25\Documents\Visual Studio 2008\Projects\Begin\Begin\Debug\BuildLog.htm"
1>Begin - 0 error(s), 1 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

最佳答案

该代码确实有效,您只是忘记将c.bmp放在正确的位置。如果要从资源管理器启动程序,则将其放在项目输出文件夹(即bin / Debug)中;如果要从Visual Studio中启动程序,则将其置于项目文件夹中。

09-10 05:25
查看更多