How would I load a PNG image using Win32/GDI (no GDI+ if possible)?

前端 未结 4 505
轮回少年
轮回少年 2020-11-28 10:32

Is it possible to load a PNG from a file into an HBITMAP using Win32 GDI functions? If not, what would be the lightest solution without using external libraries (like libpn

4条回答
  •  清酒与你
    2020-11-28 11:10

    There is no need to use Windows Imaging Component, GDI+ or PNG library. You can use Icon functionality.

    1. Add new icon (ICO_PNG) to VC project resources with custom Width and Height (Resource Editor->Image->New Image Type). Copy Your png image here and use Fill Tool+transparent color to make icon transparent.

    2. Add Picture Control (IDC_PNG) to Your dialog (Type = Owner draw).

    3. Dialog procedure code:

    switch (msg)
    {
        ...
    
        case WM_DRAWITEM:
        {
            LPDRAWITEMSTRUCT pDIS = (LPDRAWITEMSTRUCT)lParam;
            if (pDIS->CtlID == IDC_PNG)
            {
                HICON hIcon = (HICON)LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(ICO_LOGO), IMAGE_ICON, 0, 0, LR_LOADTRANSPARENT); 
                DrawIconEx(pDIS->hDC, 0, 0, hIcon, 0, 0, 0, NULL, DI_NORMAL);
                DestroyIcon(hIcon);
                return TRUE;
            }
        }
    }
    

提交回复
热议问题