Not drawing CBitmap* in OnDraw

I am writing my first SDI Windows application using Visual C++ 2008. I have never used the Document/Vew architect before. The application is new card game.

Since the card deck will contain a lot of cards, I would like the CView to draw the card image using a pointer to a ‘pre-loaded’ CBitmap object instead of the bitmap file name.

The bitmap image is not being drawn. I’m using the following code in the view’s OnDraw() method.

void View::OnDraw(CDC* pdcDC)
{
		// get the document

	Docment* pDocment = GetDocument();
	ASSERT_VALID( poGame );

		// draw the background (this works)

	CBrush* pbrOldBrush = (CBrush*)pdcDC->SelectObject(pDocument->m_pbrTableTopBrush);

	RECT rcClientSize;	// size of the view
	GetClientRect(&rcClientSize);

	pdcDC->PatBlt(rcClientSize.left,
			 rcClientSize.top,
			 rcClientSize.right,
			 rcClientSize.bottom,
			 PATCOPY);

	pdcDC->SelectObject(pbrOldBrush);

		// draw the card image

	CDC memDC;
	memDC.CreateCompatibleDC(pdcDC);

	memDC.SelectObject(pDocment->pmbCardImage);
	pdcDC->BitBlt(10, 20, 122, 177, &memDC, 0, 0, SRCCOPY);
}

Open in new window


The card Cbitmap object is being loaded with the following code sniplet in the Docment.

	CBitmap* m_pmbCardImge = new CBitmap();
	CImage iCardImage;

	iCardImage.Load("<bitmap file path>");

	m_pmbCardImage->Attach(iCardImage.Detach());

Open in new window


Why is the image not being drawn in OnDraw? Is there a more proficient to do this?

Thank you in advance for your help.
LVL 1
robear7ntAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi robear7nt,

is this really the code you sue? It's a bit confuzing which variable is intended to hold the bitmap pointer: In the first code snippet you use pDocment->pmbCardImage, but in the second snippet two different variables are used (m_pmbCardImge is created locally but never used, the loaded bitmap is attached m_pmbCardImage, hard to say if this is instantiated before).

I assumed the member you want to use from your document is the last one, so I made a test project where I added this member and implemented code similar to yours (with some added error handling) which works:
// in document's header
class CTestSDI2Doc : public CDocument
{
public:
	CBitmap* m_pmbCardImage;
	...
}

// in document's CPP file
CTestSDI2Doc::CTestSDI2Doc()
: m_pmbCardImage( NULL )
{
	CImage iCardImage;

	if ( SUCCEEDED( iCardImage.Load("d:/temp/cards.png") ) )
	{
		m_pmbCardImage = new CBitmap();
		m_pmbCardImage->Attach(iCardImage.Detach());
	}

	ASSERT( m_pmbCardImage != NULL );
}

// in view's SPP file
void CTestSDI2View::OnDraw(CDC* pdcDC)
{
	...
	// draw the card image
	if ( NULL !=pDoc->m_pmbCardImage )
	{
		CDC memDC;
		memDC.CreateCompatibleDC(pdcDC);

		CBitmap* pOldBmp = memDC.SelectObject(pDoc->m_pmbCardImage);

		pdcDC->BitBlt(10, 20, 122, 177, &memDC, 0, 0, SRCCOPY);

		memDC.SelectObject( pOldBmp );
	}
}

Open in new window

I even added the last line because in your code the bitmap wasn't un-selected from the memory DC. It's always a good practice to un-select everything previously selected into a memory DC to avoid possible GDI leaks.

I hope this helps,

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robear7ntAuthor Commented:
Hi ZOPPO,

No, it wasn't really the actually code, I had snipped it to make it much less code to wade through. I had mis-typed in the sniplet I posted. Sorry!

I was not doing the:
   
m_pmbCardImage = new CBitmap();

Open in new window

I honestly didn't realize that was necessary, now I do.

It now works!

Thank you so much for the quick reply. I'm one happy coder now  ... lol :-)

Robert
0
ZoppoCommented:
:o) great, I'm glad I could help.

Have a nice day,

best regards,

ZOPPO
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.