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.
>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
****
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.
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
Don't get me started :-)
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email