⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 13view.cpp

📁 实现直线曲线的绘制
💻 CPP
字号:
// 13View.cpp : implementation of the CMy13View class
//

#include "stdafx.h"
#include "13.h"

#include "13Doc.h"
#include "13View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy13View

IMPLEMENT_DYNCREATE(CMy13View, CView)

BEGIN_MESSAGE_MAP(CMy13View, CView)
	//{{AFX_MSG_MAP(CMy13View)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy13View construction/destruction

CMy13View::CMy13View()
{
	// TODO: add construction code here
	i=0;
}

CMy13View::~CMy13View()
{
}

BOOL CMy13View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy13View drawing

void CMy13View::OnDraw(CDC* pDC)
{
	CMy13Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMy13View printing

BOOL CMy13View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMy13View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMy13View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMy13View diagnostics

#ifdef _DEBUG
void CMy13View::AssertValid() const
{
	CView::AssertValid();
}

void CMy13View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMy13Doc* CMy13View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy13Doc)));
	return (CMy13Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMy13View message handlers

void CMy13View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDC* pd=GetDC();	//获得设备环境
	if (i==0)
	{
		RedrawWindow();		//重画窗口
		pt=point;
		pd->MoveTo(pt);
	}
	pd->MoveTo(pt);			//画鼠标点击的直线
	pd->LineTo(point);
	
	pt=point;
	x[i]=(float)point.x;	//给数组赋值
	y[i]=(float)point.y;
	i++;					//记录每次点击
	if (i>3)
	{
		HuaXian();			
	}

	
	CView::OnLButtonDown(nFlags, point);
}
void CMy13View::HuaXian()
{
	
	float a0,a1,a2,b0,b1,b2;
	int n=10,j;
	
	int x1,y1;
	float dt,t;
	
	dt=(float)(1.0/n);
	
	CDC* pdc=GetDC();


	CPen pen(PS_SOLID,2,RGB(255,0,0));
	CPen* poldpen=pdc->SelectObject(&pen);
	for(j=0;j<i-2;j++)
	{
		a0=(x[j]+x[j+1])/2;
		a1=x[j+1]-x[j];
		a2=(x[j]-2*x[j+1]+x[j+2])/2;
		b0=(y[j]+y[j+1])/2;
		b1=y[j+1]-y[j];
		b2=(y[j]-2*y[j+1]+y[j+2])/2;
		for (float ii=0;ii<=n;)
		{
			t=ii*dt;
			x1=(int)(a0+a1*t+a2*t*t);
			y1=(int)((b0+b1*t+b2*t*t));
			//CPoint pt(x1,y1);
			if(ii==0)
				pdc->MoveTo((int)((x[j]+x[j+1])/2),(int)((y[j]+y[j+1])/2));
			pdc->LineTo(x1,y1);
			ii=(float)(ii+0.1);
		}
	}
	

}

void CMy13View::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	RedrawWindow();		
	i=0;
	CView::OnRButtonDown(nFlags, point);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -