I am trying to load a bitmap in a Win32 application, but for some strange reason the bitmap does not load. Here is what I have so far:
HANDLE hImg = LoadImageW(
NULL,
L"img.bmp",
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE
);
if (hImg == NULL) {
std::cout << GetLastError();
}
Compiled on GCC 8.1.0 with -Wall -municode
.
Nothing is output to the console, so there are no errors. However, the image never shows up. These questions seem to address a similar issue, but I have had a look at them and couldn't find a solution:
- I cannot load image from folder using win32
- Win32 application. HBITMAP LoadImage fails to load anything
Where could the problem be?
Full code:
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <iostream>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
const wchar_t CLASS_NAME[] = L"Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"My Application",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
HANDLE hImg = LoadImageW(
NULL,
L"img.bmp",
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE
);
if (hImg == NULL) {
std::cout << GetLastError();
}
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
EndPaint(hwnd, &ps);
break;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}