绘图所有者绘制图片控件 [英] Drawing on owner draw picture control

查看:83
本文介绍了绘图所有者绘制图片控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的代码从vb转换为c ++有一些动态点可以绘制不同的形状。



在vb中它们具有.ScaleWidth等属性,.ScaleHeight,.ScaleTop,.ScaleLeft。



他们所做的是他们根据宽度和高度使用这些属性缩放并绘制点(形状) vb中的图片框。





但在mfc我有所需的所有输入点但是当iam绘图无法正确或正确绘制。请帮帮我,即无法正确伸缩,请帮帮我。



我尝试过:



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(新CPen( 0,3,rgb));

cdc-> MoveTo(x1,y1);

cdc-> LineTo(nCurrentx = x2,currenty = y2);

删除(CPen *)cdc-> SelectObject(oldPen);

wnd-> ReleaseDC(cdc);

}



在哪里使用这些scaleleft,nScaleHeight等来获得正确的形状。



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.

推荐答案

使用C ++和MFC你有权力为自己绘制每个像素的责任。为此,您必须缩放要在绘图矩形中绘制的内容。



有两种方式:



1.在绘制之前缩放内容坐标(推荐给你DrawLine)

2.绘制一个离线位图并将其缩放到绘图矩形中。



一个有用的函数是Bitblt 在某个步骤中绘制缩放的位图。



缩放图形时使用一个因子,如果使用两个,可能会有所不同并使图像失真。注意所有分区都是浮动的,而不是隐式类型转换正在推动结果。
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.


这篇关于绘图所有者绘制图片控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆