1

我使用这篇文章http://msdn.microsoft.com/en-us/library/bb384843.aspx在 Visual Studio 2010 中创建了 win32 应用程序 。之后,我向这个项目添加了一个 cpp 文件,粘贴了我从图形教程中获得的代码片段。此代码如下所示。

要编译此解决方案,我必须在项目属性中将字符集更改为使用多字节字符集。

之后,我在项目文件夹中插入了一张名为 truck.bmp 的图片。

当我使用这张照片时

在此处输入图像描述

它给出了这个错误信息

未加载位图 - 确保项目文件夹中存在文件“truck.bmp”,因为根据程序,hbitmap 为空。

但是当我使用这张照片时

在此处输入图像描述

它正确加载。程序使用 LoadImage 函数加载图像。

我不明白为什么这两张图片对于相同的功能表现不同。有人可以解释原因。抱歉我的问题中的大图

/* to demonstrate loading and displaying of bitmaps */
  #include < windows.h >
  LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsgId, WPARAM wParam,
                        LPARAM lParam);

  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)
{
static char name[]="Bitmap Display";

HWND hWnd;
MSG msg;
WNDCLASS wndClass;
wndClass.style=0;
wndClass.lpfnWndProc=WindowProc;
wndClass.cbClsExtra=0;
wndClass.cbWndExtra=0;
wndClass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
wndClass.hIcon=LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor=LoadCursor(NULL, IDC_ARROW);
wndClass.hInstance=hInstance;
wndClass.lpszMenuName=NULL;
wndClass.lpszClassName=name;

if(RegisterClass(&wndClass) == 0)
{
   MessageBox(0, "The Window is not registered", "Message", MB_OK);
   return 0;
}

    hWnd=CreateWindow(name, name, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,         CW_USEDEFAULT,  CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if(hWnd==0)
{
   return 0;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

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

   LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsgId, WPARAM wParam, LPARAM lParam)
 {
HBITMAP hbitmap;
BITMAP bitmap;
HDC hdc, hmemdc;

switch(uMsgId)
{
case WM_PAINT:
 PAINTSTRUCT paintStruct;
 hdc= BeginPaint(hWnd, &paintStruct);

 /* loading the bitmap from a BMP file (ensure the file is present under the    project folder) */
 hbitmap= (HBITMAP)LoadImage(NULL, "truck.bmp",IMAGE_BITMAP, 0,0, LR_LOADFROMFILE    );

 if(hbitmap == NULL)
 {
    MessageBox(NULL, "Bitmap not loaded- Ensure the file 'truck.bmp' is present in the project folder","Error",MB_OK);
 }

 hmemdc= CreateCompatibleDC(hdc);
 SelectObject(hmemdc, hbitmap);
 GetObject(hbitmap, sizeof(BITMAP), &bitmap);

 /* displaying the bitmap at certain x,y position on the window client area */
 BitBlt(hdc, 0, 0, bitmap.bmWidth,bitmap.bmHeight, hmemdc,0,0, SRCCOPY);

 /* displaying the bitmap with scaled dimensions at certain x,y position on the window client area */
 StretchBlt(hdc, bitmap.bmWidth+5, 0, bitmap.bmWidth*2,bitmap.bmHeight*2,  hmemdc,0,0, bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);

 DeleteDC(hmemdc);
 DeleteObject(hbitmap);
 EndPaint(hWnd, &paintStruct);
 return 0;

case WM_DESTROY:
 PostQuitMessage(0);
 return 0;
default:
 return DefWindowProc(hWnd, uMsgId, wParam, lParam);
}
  }
4

0 回答 0