Click here to Skip to main content
14,328,676 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi;

i do double buffering technique to draw ellipse as an example, and i can not scroll the page correctly , it seems that the drawing distorted when scrolled the page, the project built upon CScrollview class, so what is wrong, thanks for your time

What I have tried:

i override
void Cfree_flickerView::OnEraseBkgnd(CDC* pDC)
with
return true;
in stead of
return CScrollView::OnEraseBkgnd(pDC);


add these code in
void Cfree_flickerView::OnPaint() 
{
CDC memDC;
memDC.CreateCompatibleDC(&dc);
Cfree_flickerView::OnDraw(&dc);<pre>
}
add some codes in
void Cfree_flickerView::OnInitialUpdate()


CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 2000;
	SetScrollSizes(MM_TEXT, sizeTotal,CSize(200,10),CSize(20,1));


finally add these codes in
void Cfree_flickerView::OnDraw(CDC* pDC)


CRect rcTotal(CPoint(0,0),GetTotalSize());
  

   CDC MemDC;
   CBitmap MemBitmap;

   pDC = this->GetDC();         // Get Current DC
   MemDC.CreateCompatibleDC(pDC);
   MemBitmap.CreateCompatibleBitmap(pDC, rcTotal.right, rcTotal.bottom);
   

   CBitmap *pOldBitmap = (CBitmap*)MemDC.SelectObject(&MemBitmap);

   MemDC.Ellipse(rcTotal);
CRect rcClientx;
	




   pDC->BitBlt(0, 0, GetTotalSize().cx,  GetTotalSize().cy, &MemDC, 0, 0, SRCCOPY);


   MemDC.SelectObject(pOldBitmap);

   ReleaseDC(pDC);
   ReleaseDC(&MemDC);
Posted
Updated 1-Dec-18 22:16pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Check the values in rcTotal. It is likely that they exceed the bounds of the view port. Similarly when blitting an image you need to make sure the aspect ration of the image width and height remain the same. Or only blit that part of the image that will fit the screen.
The following code will fix the image's dimensions to fit the available window space.

imageHeight and imageWidth give the dimensions of the source image. While destinationRect contains the RECT values of the destination Window.
/*
 * Now we need to check whether the image is larger than the destination
 * rectangle. If so we need to adjust the width and/or height in the same
 * ratios to avoid distortion.
 *
 * If the image is wider than the destination, then it will be automatically
 * scaled horizontally to fit the window. If this happens then we need to
 * scale the height by the same amount.
 *
 * The calculation for the height is:
 *
 * NewHeight = ImageHeight x (DestinationWidth / ImageWidth)
 *
 * If the NewHeight is greater than the destination height we use this value
 * as the new destination height.
 *
 * Similarly we calcuate the new width based on the height values.
 *
 */
if (imageWidth > destinationRect.Width)
{
	// if wider than window then scale the height by the width scaling
	REAL newHeight = imageHeight * (destinationRect.Width / imageWidth);
	// but don't make it deeper than the window
	if (destinationRect.Height > newHeight)
		destinationRect.Height = newHeight;
}
if (imageHeight > destinationRect.Height)
{
	// if deeper than window then adjust width by aspect ratio
	REAL newWidth = imageWidth * (destinationRect.Height / imageHeight);
	if (destinationRect.Width > newWidth)
		destinationRect.Width = newWidth;
}
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100