Click here to Skip to main content
14,212,683 members
Rate this:
Please Sign up or sign in to vote.
See more:
i am converting my code from vb to c++ there are some dynamic points which are there to draw different shapes.

in vb they have properties such as .ScaleWidth, .ScaleHeight, .ScaleTop , .ScaleLeft.

what they had done is they had scaled according to width and height using these properties and draw points(shapes) on the picture box in vb.


but in mfc i have all the input points required but when iam drawing not able to draw properly or correctly. please help me i.e not able to scale properly please help me out.

What I have tried:

void CPDlgToolPreview::S_SetDrawingScale()
{
	short i;
	float lnMaxX;
	float lnMaxY;
	float lnMinX;
	float lnMinY;
	const int BigNumber = 1000000000;
	float lnWidth;
	float lnHeight;

	lnMaxX = (float)(-BigNumber);
	lnMaxY = (float)(-BigNumber);
	lnMinX = (float)(BigNumber);
	lnMinY = (float)(BigNumber);

	for (i = 0; i < mlPoints; i++) 
	{
		if (moPts[i].X > lnMaxX) lnMaxX = (float)(moPts[i].X);
		if (moPts[i].Y > lnMaxY) lnMaxY = (float)(moPts[i].Y);
		if (moPts[i].X < lnMinX) lnMinX = (float)(moPts[i].X);
		if (moPts[i].Y < lnMinY) lnMinY = (float)(moPts[i].Y);
	}

	lnWidth = lnMaxX - lnMinX;
	lnHeight = lnMaxY - lnMinY;

	CRect cRect;
	m_cPreview.GetClientRect(&cRect);
	

	if ( lnWidth / cRect.Width() > lnHeight / cRect.Height() )
	{
		// Scale according to items width	
		nScaleWidth = lnWidth*1.2;
		nScaleHeight = cRect.Height() *nScaleWidth / cRect.Width();
	}
	else
	{
		// Scale according to items height
		 nScaleHeight = lnHeight*1.2;
		 nScaleWidth = cRect.Width() *nScaleHeight / cRect.Height();
	}

	nScaleleft = -nScaleWidth / 2;
	nScaletop = -((nScaleHeight - lnHeight) / 2) - lnHeight;

}



for (short i  = 2; i < mlPoints; i++) 
	{
		DrawLine(m_cPreview, moPts[i].X  , moPts[i].Y);
	}


void DrawLine(CWnd *wnd, int x1, int y1, int x2, int y2, int rgb)
{
CDC *cdc = wnd->GetDC();
CPen *oldPen = cdc->SelectObject(new CPen(0, 3, rgb));
cdc->MoveTo(x1, y1);
cdc->LineTo(nCurrentx = x2, currenty= y2);
delete (CPen *)cdc->SelectObject(oldPen);
wnd->ReleaseDC(cdc);
}

where to use these scaleleft, nScaleHeight etc to get proper shape.
Posted
Updated 1-Mar-17 1:06am
v2

1 solution

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

Solution 1

With C++ and MFC you have the power and responsibility to draw each pixel for yourself. For that you must scale what you want to draw in the drawing rectangle.

There are two way:

1. scale the content coordinates before drawing them (recommanded for your DrawLine)
2. draw an offline bitmap and scale that into in the drawing rectangle.

A useful function is Bitblt to draw a scaled bitmap in some step.

When you scale your graphic use one factor, if you use two the might be different and distort the image. Take care that all divisions are made as float and not implicit type conversion is hurding the results.
   
Comments
Member 12677926 1-Mar-17 8:48am
   
thanks for the reply. please do you have any example ....
Member 12677926 1-Mar-17 8:52am
   
u mean to say save a bitmap and load again ?
Member 12677926 1-Mar-17 10:49am
   
could you please help me with sample ....please

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



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 1 Mar 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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