[[image]]
 

https://stackoverflow.com/questions/70248510/how-do-i-load-a-bitmap-into-a-win32-application

#ifndef UNICODE
#define UNICODE
#endif
 
#include <windows.h>
#include <iostream>
 
HANDLE hImg = NULL;
 
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    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, nShowCmd);
 
 
    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_CREATE:
        if (!hImg)
            hImg = LoadImage(NULL, L"c:\\zc\\test_image.bmp", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
        if (!hImg) { DWORD err = GetLastError(); std::cout << err; }
        return 0;
 
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
 
        if (hImg) {
            BITMAP bm;
            GetObject(hImg, sizeof(bm), &bm);//get bitmap dimension
            auto memdc = CreateCompatibleDC(hdc);
            auto oldbmp = SelectObject(memdc, (HBITMAP)hImg);
            BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, memdc, 0, 0, SRCCOPY);
            SelectObject(memdc, oldbmp);//restore memdc
            DeleteDC(memdc);//delete memdc, we don't need it anymore
        }
 
        EndPaint(hwnd, &ps);
        return 0;
    }
 
    case WM_DESTROY:
        if (hImg) DeleteObject(hImg);//release resource
        hImg = NULL;
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

LIBS += -lgdi32

SOURCES +=  main.cpp
 
image.txt · Last modified: 2024/05/28 23:29 by 109.81.160.124
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki