0

I'm trying to load a png to my button, but it's not showing the png in my button.

I create my button with this function:

HWND MyControls::CreateButton(LPCTSTR text, int x, int y, int w, int h, HMENU id, HWND hwnd, HINSTANCE hInst, bool png){
    if (png){
        return CreateWindow("BUTTON", text, BS_BITMAP | WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
            x, y, w, h, hwnd, id, hInst, NULL);
    }
    else{
        return CreateWindow("BUTTON", text, WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
            x, y, w, h, hwnd, id, hInst, NULL);
    }   
}

Add Png to button (WM_CREATE):

void MyControls::AddPngBtn(HWND hwnd, const WCHAR* fileName){
    Bitmap bmp(fileName);
    HBITMAP tBmp;
    bmp.GetHBITMAP(Color(0, 0, 0, 0), &tBmp);
    SendMessage(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)tBmp);
    ShowWindow(hwnd, SW_SHOW);
    DeleteObject(tBmp);
}

how i initialize GDI+:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(szCmdLine);
    UNREFERENCED_PARAMETER(iCmdShow);

    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    if (!Wnd.RegClassEx(hInstance)) return 0;
    if (!Wnd.CreateMyWindow(hInstance)) return 0;

    GdiplusShutdown(gdiplusToken);

    return Wnd.MyMsg();
}

The LPARAM is not NULL;
My button size: 25x25;
My png size: 24x24;
And i don't have a error in the "Error List".

What i should do, or what am i doing wrong?

Ty.

Edited by juliocesar.schincariolfilho

2
Contributors
1
Reply
15
Views
2 Years
Discussion Span
Last Post by triumphost
1
/*
    Creates a "Bitmap" on the heap so you can explicitly delete it
    before shutting down. Otherwise you must wait until it goes out of
    scope before shutting down.
*/
HBITMAP loadBitmap(const wchar_t* path)
{
    HBITMAP tBmp = NULL;
    ULONG_PTR token = 0;
    Gdiplus::GdiplusStartupInput input = NULL;
    Gdiplus::GdiplusStartup(&token, &input, NULL);

    if (token != 0)
    {
        Gdiplus::Bitmap* bmp = new Gdiplus::Bitmap(path);
        bmp->GetHBITMAP(Gdiplus::Color::Transparent, &tBmp);
        delete bmp;
        Gdiplus::GdiplusShutdown(token);
    }
    return tBmp;
}


/*
    Loads a PNG into an HBITMAP and uses BM_SETIMAGE to set the image
    of a button.
*/
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HBITMAP tBmp = NULL;

    switch(message)
    {
        case WM_CREATE:
        {
            HWND btn = CreateWindow("BUTTON", "TEXT", BS_BITMAP | WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                         100, 100, 95, 25, hwnd, (HMENU)1, NULL, NULL);


            tBmp = loadBitmap(L"C:/Users/Kira/Desktop/Test.png");
            SendMessage(btn, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)tBmp);
            ShowWindow(btn, SW_SHOW);
        }
        break;


    case WM_DESTROY:
        DeleteObject(tBmp);
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

Edited by triumphost

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.