admin管理员组

文章数量:1123143

I'm reviving some really old Win32 API based code, and having a little trouble with images. The code below was derived from Microsoft's Hello World sample program at

I added lines 47 – 50 to test access to resource files. PlaySoundA works fine, it will play the sound whether yeow2.wav is located in the project directory OR in the Debug directory below it. But LoadImageA is not working for me no matter where Laser3.bmp is located. hh always comes out NULL, with GetLastError always returning zero. Really? Oh isn't that helpful!

Can anyone see something that I'm lacking, perhaps some prerequisite that needed to happen first? Or is there more than one .BMP format? I’ve tried this with multiple BMP files, which open just fine in Paint. So they’re not corrupted.

Visual Studio 17.11.4, Windows 11, 64-bit ARM, not using wide characters

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{

    // Register the window class.
    const wchar_t CLASS_NAME[] = L"Sample Window Class";

    WNDCLASS wc = { };

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // test code
    bool ok = PlaySoundA("yeow2.wav", NULL, SND_FILENAME | SND_ASYNC);
    HBITMAP hh = (HBITMAP)LoadImageA(hInstance, "Laser3.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    DWORD err = GetLastError();

    // Run the message loop.
    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);

        // All painting occurs here, between BeginPaint and EndPaint.
        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_GRAYTEXT + 1));
        EndPaint(hwnd, &ps);
    }
    return 0;
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

本文标签: imageWhat does LoadImageA actually requireStack Overflow