Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a simple code which draws a PNG image with alpha layer in a dialogbox & used this code:
void CBmpPngDlg::OnPaint()
{
    CPaintDC dc(this); // device context for painting
	
	HRSRC hResource = ::FindResource(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_PNG2), _T("PNG"));
	CXTPGraphicBitmapPng bmpPNG;
	if (!bmpPNG.LoadFromResource(::AfxGetInstanceHandle(), hResource))
	{
		TRACE0("Failed to load PNG from resource\n");
		return;      // fail load PNG from resource
	}
	CBitmap bmp;
	if (bmp.Attach(CXTPImageManagerIcon::PreMultiplyAlphaBitmap(bmpPNG)))
	{
		CXTPImageManagerIcon::DrawAlphaBitmap(&dc, bmp, CPoint(0, 0), CSize(344, 344));
		bmp.Detach();
		bmpPNG.DeleteObject();
	}
}

& I am confused on using the CGdiObject::Attach, CGdiObject::Detach & CGdiObject::DeleteObject GDI functions. So is this code correct?:
bmp.Attach(CXTPImageManagerIcon::PreMultiplyAlphaBitmap(bmpPNG));
bmp.Detach();
bmpPNG.DeleteObject();

or should i use this:
bmp.Attach(CXTPImageManagerIcon::PreMultiplyAlphaBitmap(bmpPNG));
bmpPNG.Detach();
bmpPNG.DeleteObject();

or it is not necessary to use it.
Is the code bmpPNG.DeleteObject(); necessary?
Help me please :confused:

[Original Question Title]
CGdiObject::Attach & CGdiObject::Detach & CGdiObject::DeleteObject
[/Original Question Title]
Posted
Updated 18-Dec-10 23:36pm
v4
Comments
Slacker007 19-Dec-10 5:37am    
Abbreviated question title.

1 solution

In general CGdiObject (and its derivatives) have constructors that create the OS internal object and a destructor that destroys it.

The Attach / Detach / DeleteObject functions are in the case you have to do some "extra management":

Attach gives an externally created HGDIOBJECT (HBITMAP derives from it) to the CGdiObject instance. It will delete it on destruction if left attached.

Detach makes the HGDIOBJECT to be anymore managed by the CGdiObject instance

DeleteObject makes the HGDIOBJECT deletion to be forced before the GdiObject wrapper comes to its own destruction.

In your case, calling Attach makes sense if CXTPImageManagerIcon::PreMultiplyAlphaBitmap retuns a newly created HBITMAP, (and you don't have to detach / delete anything - note: "have" not "need") otherwise, you don't even need the bmp object.
 
Share this answer
 
Comments
Mr. Tomay 19-Dec-10 14:04pm    
CXTPImageManagerIcon::PreMultiplyAlphaBitmap(bmpPNG) returns an HBITMAP object.
Emilio Garavaglia 19-Dec-10 15:04pm    
Your comment is not sufficient: returns A NEW HBITMAP object or just THE SAME you pass as a parameter after it has been ... premutiplyed?
The two cases are completely different.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900