Answered by:
PNG transparent image on button in Win32 project.
Question
-
Hi,
Can anybody help me through a code sample which will place an PNG image on a button in Win32 project in vc++ with the help of Gdiplus. The image in resource.
Thanks,
Nikhil.
Tuesday, February 22, 2011 7:40 PM
Answers
-
Hi agrawalnikhil,
In fact, you are almost there. We can load the PNG and convert it as the ICON using BitMap::GetHICON. An ICON support transparent originally. Here is the sample code:
Gdiplus::Bitmap* m_pBitmap;
HICON hicon;
button= CreateWindow(_T("BUTTON"),_T("Test button"),
BS_ICON | WS_VISIBLE | WS_CHILD ,0,0,128,128,hWnd, NULL,GetModuleHandle(NULL),NULL);
m_pBitmap = Gdiplus::Bitmap::FromFile(L"test.png");
m_pBitmap->GetHICON(&hicon);
LRESULT lr = SendMessage(button,BM_SETIMAGE,IMAGE_ICON,(LPARAM)hicon );
ShowWindow(button,SW_SHOW);
Cheers,
Yi
Yi Feng Li [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

- Marked as answer by Yi Feng Li Wednesday, March 2, 2011 3:18 AM
Thursday, February 24, 2011 1:21 PM
All replies
-
Windows can natively handle bitmaps and icons (you would use SendMessage to send BM_SETIMAGE), if you can do the conversion up front, then that is best (ico files now support png encoding).
Microsoft Test - http://tester.poleyland.com/Tuesday, February 22, 2011 8:41 PM -
You can load the PNG from your resource using GDI+ as described here:
http://www.codeproject.com/KB/GDI-plus/cgdiplusbitmap.aspx
Once you have your PNG converted to a CBitmap, you know what to do.
-Seetharam
Tuesday, February 22, 2011 9:01 PM -
Hi,
Thanks for your reply, i think my half work is done but want to ask u some thing. here is my code
Gdiplus::Bitmap* m_pBitmap;
BOOL load = LoadStdImage(IDB_PNG2, _T("PNG")); ///Here return value i am getting is true
// HANDLE hbitmap= //LoadImage(GetModuleHandle(NULL),m_pBitmap,IMAGE_BITMAP,166,76,LR_DEFAULTCOLOR);
HWND button= CreateWindow(_T("BUTTON"),_T("Test button"),BS_BITMAP | WS_VISIBLE | WS_CHILD | BS_FLAT,0,0,128,128,hDlg, (HMENU) 5,GetModuleHandle(NULL),NULL);
::DWORD dr = ::GetLastError();
LRESULT lr = SendMessage(button,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)m_pBitmap);
dr = ::GetLastError();
::Sleep(90);Now i am able to successfully load an image and i am getting some numerical value in the object m_pBitmap. But with sendmessage the LRESULT vale is 0 and getlasterror is also 0. Think there is some problem with calling it. If u can help me with htis.
The application type is Win32 Windows application. The code is written in WM_initdialog in dialog box class
Thanks,
Nikhil.
Wednesday, February 23, 2011 5:09 AM -
Hi,
I have resolved the issue of loading the image and send message is also working fine i can see the desired image on the button, but now the problem is with the background. I used PNG image because it was transparent in nature. but what i can see in my dialog is on the back ground image a square button with a border around it and white background . What i except is the button in transparent mode. here is my code,
BOOL load = LoadStdImage(IDB_PNG3, _T("PNG"));
if(load == TRUE)
{
button= CreateWindow(_T("BUTTON"),_T("TestBACK"),BS_BITMAP | WS_VISIBLE | WS_CHILD | BS_FLAT ,100,100,128,128,hDlg, (HMENU) 7,GetModuleHandle(NULL),NULL);
::Gdiplus::Color color;// = ::GetBkColor(pDC);
color.SetValue(16777215);
m_pBitmap->
HBITMAP hbitmap1;
m_pBitmap->GetHBITMAP(color, &hbitmap1);
LRESULT lr = SendMessage(button,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hbitmap1 );
}Thanks,
Nikhil.
Wednesday, February 23, 2011 8:23 AM -
Try adding WS_EX_TRANSPARENT to your button style. That will ensure that your button gets a paint message after everything else has been drawn.
-Seetharam
Wednesday, February 23, 2011 2:32 PM -
Its still not working. Still the same result.
Thanks,
Nikhil.
Wednesday, February 23, 2011 3:05 PM -
I think that you need CreateWindowEx and also XP styles.Wednesday, February 23, 2011 4:57 PM
-
Hi,
have u been able to do it at your end. If yes then can u please give me your code here or email it to me on agrawalnikhil9@gmail.com.
Thanks,
Nikhil.Thursday, February 24, 2011 3:55 AM -
Hi,
Either there is no solution to this problem or i am dumb, because it still doesn't work. My reference to was the following code project link. I think the entire way i have trying needs to change because it seems to me it is not that easy and straight. Thanks for all the efforts given by all u guys.
http://www.codeproject.com/KB/buttons/GdipButton.aspx
Thanks,
Nikhil.
Thursday, February 24, 2011 11:21 AM -
Hi agrawalnikhil,
In fact, you are almost there. We can load the PNG and convert it as the ICON using BitMap::GetHICON. An ICON support transparent originally. Here is the sample code:
Gdiplus::Bitmap* m_pBitmap;
HICON hicon;
button= CreateWindow(_T("BUTTON"),_T("Test button"),
BS_ICON | WS_VISIBLE | WS_CHILD ,0,0,128,128,hWnd, NULL,GetModuleHandle(NULL),NULL);
m_pBitmap = Gdiplus::Bitmap::FromFile(L"test.png");
m_pBitmap->GetHICON(&hicon);
LRESULT lr = SendMessage(button,BM_SETIMAGE,IMAGE_ICON,(LPARAM)hicon );
ShowWindow(button,SW_SHOW);
Cheers,
Yi
Yi Feng Li [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

- Marked as answer by Yi Feng Li Wednesday, March 2, 2011 3:18 AM
Thursday, February 24, 2011 1:21 PM