我试图找出无法从资源加载图片的原因。
当lpszName是文件名时,图像工作,当更改为资源时,图像不工作。
以相同的方式创建图像(可以互换,同样的问题)
图像位于Resource和Resource.rc文件中。
代码:
LRESULT CALLBACK WndProc(HWND hWinMain, UINT message, WPARAM wParam, LPARAM lParam)
{
DWORD
lastError;
static HDC
hdcFromResource,
hdcFromFilename;
HBITMAP
hFromResource,
hFromResourcePrevious,
hFromFilename,
hFromFilenamePrevious;
HDC
hdc;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hWinMain);
hdcFromFilename = CreateCompatibleDC(hdc);
hdcFromResource = CreateCompatibleDC(hdc);
ReleaseDC(hWinMain, hdc);
hFromFilename = (HBITMAP)LoadImageW(NULL, (L"filename.bmp"), IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE);
if (!hFromFilename)
{
// ERROR HANDELING
}
hFromFilenamePrevious = (HBITMAP)SelectObject(hdcFromFilename, hFromFilename);
DeleteObject(hFromFilename);
DeleteObject(hFromFilenamePrevious);
hFromResource = (HBITMAP)LoadImageW(NULL, MAKEINTRESOURCEW(IDB_RESOURCE), IMAGE_BITMAP, 100, 100, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
lastError = GetLastError();
lastError;
hFromResourcePrevious = (HBITMAP)SelectObject(hdcFromResource, hFromResource);
DeleteObject(hFromResource);
DeleteObject(hFromResourcePrevious);
return 0;
case WM_PAINT:
hdc = BeginPaint(hWinMain, &ps);
BitBlt(hdc, 0, 0, 100, 100, hdcFromFilename, 0, 0, SRCCOPY);
BitBlt(hdc, 100, 100, 100, 100, hdcFromResource, 0, 0, SRCCOPY);
EndPaint(hWinMain, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWinMain, message, wParam, lParam);
}
无论是否加载图像,都不会触发if(!hFromFilename)
。
失败的图像加载中断后没有任何断点(通过WM_CREATE断点,其他函数中的其他断点正常工作)
我不确定如何在没有断点的情况下轻松阅读GetLastError。
RESOURCE.H
#define IDB_RESOURCE 101
RESOURCE.RC
#define IDB_RESOURCE BITMAP DISCARDABLE "resource.bmp"
答案 0 :(得分:0)
Christopher Oicles回答的问题是不需要LR_LOADFROMFILE。
可能对其他人有用:
LoadImage(
NULL, // NULL for load image via filename
L("directory\\filename.bmp"), // String to filename
IMAGE_BITMAP // Flag for loading bitmap
0, // 0 Should use the images actual width
0, // 0 Should use the images actual height
LR_LOADFROMFILE // Flag for loading from file
);
LoadImage(
hInstance, // hInstance to the file containing the resource
MAKEINTRESOURCE(IDB_RESOURCE), // Resource definition
IMAGE_BITMAP // Flag for loading bitmap
0, // 0 Should use the images actual width
0, // 0 Should use the images actual height
LR_CREATEDIBSECTION // Do not use LR_LOADFROMFILE if it is a resource
);
Resourcefile.h
#define IDB_RESOURCE 101 // Resource ID, and number
Resourcescript.rc
#include "Resourcefile.h"
IDB_RESOURCE BITMAP "directory\\filename.bmp" // Resource ID, type, and filename
谢谢。