Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can some expert tell me how to put a PNG in a Button?

13 views
Skip to first unread message

AntiBNI

unread,
Jan 6, 2009, 12:14:36 PM1/6/09
to
I've been trying to draw a PNG on a Button for a long time without any luck.

There is no documentation, Tutorial, Post or any kind of information abut
how to draw a Transparent PNG image on a CButton or CBitmapButton.

So, I really need some help on how to do it.

I tried:

----------------------------------------------Code-----------------------------------

void MyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{

// TODO: Add your code to draw the specified item
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
CRect rt;
rt = lpDrawItemStruct->rcItem;


Graphics grpx(dc);

Rect rect(50,20,100,100); //Size and Pos of the Image


Image * Img = Image::FromFile(L"C:\\Img.png",FALSE);//Image
Destination

grpx.DrawImage(Img,rect);//Draw The PNG on the Button

dc.SetTextColor(RGB(0,0,255));

CString Temp;
GetWindowText(Temp);

dc.DrawText(Temp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE);

dc.Detach();
}

----------------------------------------End of
Code-------------------------------------------------------

But it doesn't work at all...


This will Improve my GUIs dramatically.

Joseph M. Newcomer

unread,
Jan 6, 2009, 12:47:25 PM1/6/09
to
On Tue, 6 Jan 2009 13:14:36 -0400, "AntiBNI" <elite...@hotmail.com> wrote:

>I've been trying to draw a PNG on a Button for a long time without any luck.
>
>There is no documentation, Tutorial, Post or any kind of information abut
>how to draw a Transparent PNG image on a CButton or CBitmapButton.
>
>So, I really need some help on how to do it.
>
>I tried:
>
>----------------------------------------------Code-----------------------------------
>
>void MyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
>{
>
> // TODO: Add your code to draw the specified item
> CDC dc;
> dc.Attach(lpDrawItemStruct->hDC);

****
int save = dc.SaveDC();
****


> CRect rt;
> rt = lpDrawItemStruct->rcItem;
>
>
> Graphics grpx(dc);
>
> Rect rect(50,20,100,100); //Size and Pos of the Image

*****
How do you know this? How can you know the size of the image required for the button?
This will differ depending on the operating system, font selected by the user, screen
size, screen resolution, phase of the moon, and angular relationship of Mars and Jupiter.
The only possible way you can know the size of the rectangle to be drawn is to look at rt.
Nothing else can matter.
*****


>
>
> Image * Img = Image::FromFile(L"C:\\Img.png",FALSE);//Image
>Destination
>
> grpx.DrawImage(Img,rect);//Draw The PNG on the Button
>
> dc.SetTextColor(RGB(0,0,255));
>
> CString Temp;
> GetWindowText(Temp);
>
> dc.DrawText(Temp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE);

****
And where did you SetBkMode(TRANSPARENT)?
****
>
*****
Before doing this, do
dc.RestoreDC(save);
****


> dc.Detach();
>}
>
>----------------------------------------End of
>Code-------------------------------------------------------
>
>But it doesn't work at all...

****
No, this is a useless description. What do you see or not see? There is no possible way
for anyone reading this question to know what "doesn't work at all" could mean!

Replace all the graphics with a simple FillSolidRect, for example. If it still "doesn't
work" (whatever THAT means!) then your problem is somewhere else.
joe
****

elite...@hotmail.com

unread,
Jan 6, 2009, 10:04:12 PM1/6/09
to
On Jan 6, 1:47 pm, Joseph M. Newcomer <newco...@flounder.com> wrote:
> On Tue, 6 Jan 2009 13:14:36 -0400, "AntiBNI" <elitescr...@hotmail.com> wrote:
> >I've been trying to draw aPNGon aButtonfor a long time without any luck.

>
> >There is no documentation, Tutorial, Post or any kind of information abut
> >how to draw a TransparentPNGimage on a CButton or CBitmapButton.

Well, If i fill the button with a solid color it works.

The problem is i don't have any idea on how to load the PNG on the
button.

The above code doesn't display anything.

Joseph M. Newcomer

unread,
Jan 6, 2009, 10:36:50 PM1/6/09
to
By "doesn't display anything" do you mean that there is no visible display of *anything*
or that what you get is a black rectangle, white rectangle, or what?

Even if your bitmap drawing logic is flawed, you should have seen the DrawText output. If
you aren't seeing the text, there are other problems.

When I have odd problems like this, I'm likely to do a MoveTo/LineTo of some distinctive
line color (e.g., red) to make sure core logic is working correctly. Have you
singled-stepped this code? (note that you can't have your app and your debugger
overlapping on the screen when you do this). I notice that you are not actually testing
for success for most of these calls.
joe

Mihai N.

unread,
Jan 7, 2009, 1:24:33 AM1/7/09
to

> This will Improve my GUIs dramatically.

Don't get me started :-)


--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

0 new messages