Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CDC::BitBlt()

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
I'm trying to figure out how to paste a bitmap into the client area of a CView MFC class. I started with the Scribble MFC tutorial in VC++ 6 and attempted to alter the DrawStroke member function of the CStroke class to draw a CBitmap on its points rather than using a CPen to draw lines between the points. First, I created a Bitmap resource called IDB_WHYNOT, then I altered the CStroke::DrawStroke() function as follows:
Code:
BOOL CStroke::DrawStroke(CDC *pDC)
{
	CPoint pPrev=CPoint(m_pointArray[0]);
	CRect bound;
	CRect clipBox;
	pDC->GetClipBox(&clipBox);
	pDC->LPtoDP(&clipBox);
	clipBox.InflateRect(1,1);

[COLOR=green]//	CPen penStroke;[/color]
[COLOR=green]//	if (!penStroke.CreatePen(PS_SOLID,m_nPenWidth,RGB(0,0,0)))[/color]
[COLOR=green]//		return FALSE;[/color]

	CBitmap penStroke;
	if (!penStroke.LoadBitmap(IDB_WHYNOT))
		return FALSE;
[COLOR=green]//	CPen* pOldPen=pDC->SelectObject(&penStroke);[/color]
[COLOR=green]//	pDC->MoveTo(m_pointArray[0]);[/color]
	CBitmap* pOldPen=pDC->SelectObject(&penStroke);
	pDC->BitBlt(m_pointArray[0].x,m_pointArray[0].y,32,32,NULL,0,0,PATCOPY);

	for (int i=1;i<m_pointArray.GetSize(); i++)
	{
		bound=CRect(pPrev.x,pPrev.y,m_pointArray[i].x,m_pointArray[i].y);
		pDC->LPtoDP(&bound);
		bound.InflateRect(m_nPenWidth,m_nPenWidth);
		bound.NormalizeRect();
[COLOR=green]		// if (bound.IntersectRect(&bound,&clipBox))[/color]
		{
[COLOR=green]//			pDC->MoveTo(pPrev);[/color]
[COLOR=green]//			pDC->LineTo(m_pointArray[i]);[/color]
			pDC->BitBlt(m_pointArray[i].x,m_pointArray[i].y,32,32,NULL,0,0,PATCOPY);
		}
		pPrev=m_pointArray[i];
	}
	pDC->SelectObject(pOldPen);
	return TRUE;
}
The commented lines are the original lines from the Scribble app followed by the new lines I put in to change the Pen to a Bitmap.

Note that, since I only changed the DrawStroke() function of the CStroke class, the new method of drawing only occurs during the OnPaint() event. This means that to see it, I would have to draw it normally, then get it to repaint. I typically do this by minimizing and restoring the window.

I can't get it to work properly. The final argument of the BitBlt() function determines the Raster Operation. I tried several of the ROp constants from the MSDN, but I either ended up with black squares at the points or nothing whatsoever.

What am I doing wrong? How can I take a bitmap and draw it into the client area of a CView-derived class? Is BitBlt() the wrong function to call? If it's correct, what parameters should I be using? I ultimately want to be loading the bitmaps from external files rather than embedded resources, but I couldn't find any reference to loading an external file into a CBitmap object. How can I do this?

Ian
 
BitBlt isn't an explicit draw-bitmap method, it is more of a draw-portion-of-a-DC-onto-another-DC method.

So, typically what you do is
1) create a 2nd DC
2) select the bitmap into it
3) BitBlt the 2nd DC onto the destination DC.

Code:
   CDC tmpDC;
   tmpDC.CreateCompatibleDC(&destinationDC);
   tmpDC.SelectObject(&theBitmap);
   destinationDC.BitBlt(x,y,w,h,&tmpDC,left,top,SRCCOPY);


/Per
[sub]
www.perfnurt.se[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top