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

📄 vecdrawview.cpp

📁 这是一个VC++写的串口通信程序
💻 CPP
字号:
// VecDrawView.cpp : implementation of the CVecDrawView class
//

#include "stdafx.h"

#include "VecDraw.h"
#include "TextDlg.h"

#include "SerailDlg.h"

#include "VecDrawDoc.h"
#include "VecDrawView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CVecDrawView

IMPLEMENT_DYNCREATE(CVecDrawView, CView)

BEGIN_MESSAGE_MAP(CVecDrawView, CView)
	//{{AFX_MSG_MAP(CVecDrawView)
	ON_COMMAND(ID_bDrawLine, OnbDrawLine)
	ON_COMMAND(ID_bCircleFill, OnbCircleFill)
	ON_COMMAND(ID_bDrawCircle, OnbDrawCircle)
	ON_COMMAND(ID_bDrawRect, OnbDrawRect)
	ON_COMMAND(ID_bDrawRectRound, OnbDrawRectRound)
	ON_COMMAND(ID_bRectFill, OnbRectFill)
	ON_COMMAND(ID_bRectRoundFill, OnbRectRoundFill)
	ON_COMMAND(ID_bSelectObject, OnbSelectObject)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_bText, OnbText)
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_btnSerial, OnbtnSerial)
	ON_COMMAND(ID_bReDraw, OnbReDraw)
	//}}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)
//	ON_COMMAND(WM_GETMAXMININFO,ONGetMaxMinInfo)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVecDrawView construction/destruction

CVecDrawView::CVecDrawView()
{
	// TODO: add construction code here
	pCurDraw=new CDraw();
	m_DrawCurrent=ID_bSelectObject;
	m_PushNumb=0;
	saveType=0,saveIndex=0;
	SaveFlag=0;

}

CVecDrawView::~CVecDrawView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CVecDrawView drawing

void CVecDrawView::OnDraw(CDC* pDC)
{
	int upper,vType,vIndex;
	CVecDrawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CClientDC ht(this);
	ht.SetWindowExt(200,200);
	upper=pDoc->GetSaveSize();
	for(int i=0;i<upper;i++)
	{
		vType=pDoc->GetSaveOrder(i)->GetType();
		vIndex=pDoc->GetSaveOrder(i)->GetIndex();
		pDoc->Draw(pDC,vType,vIndex);
	}
	if(pCurDraw->b_Select==1)
		pCurDraw->CurDraw(pDC,RGB(0,0,255));
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CVecDrawView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CVecDrawView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CVecDrawView message handlers

void CVecDrawView::OnbDrawLine() 
{
	// TODO: Add your command handler code here
//	MessageBox("This is a test!");
	m_DrawCurrent=CurID_DrawLine;
	m_PushNumb=0;
	pCurDraw->b_Select=0;
	Invalidate();
}

void CVecDrawView::OnbCircleFill() 
{
	// TODO: Add your command handler code here
	m_DrawCurrent=CurID_CircleFill;
	m_PushNumb=0;
	pCurDraw->b_Select=0;
	Invalidate();

}

void CVecDrawView::OnbDrawCircle() 
{
	// TODO: Add your command handler code here
	m_DrawCurrent=CurID_DrawCircle;
	m_PushNumb=0;
	pCurDraw->b_Select=0;
	Invalidate();
}

void CVecDrawView::OnbDrawRect() 
{
	// TODO: Add your command handler code here
	m_DrawCurrent=CurID_DrawRect;
	m_PushNumb=0;
	pCurDraw->b_Select=0;
	Invalidate();	
}

void CVecDrawView::OnbDrawRectRound() 
{
	// TODO: Add your command handler code here
	m_DrawCurrent=CurID_DrawRectRound;
	m_PushNumb=0;	
	pCurDraw->b_Select=0;
	Invalidate();	
}

void CVecDrawView::OnbRectFill() 
{
	// TODO: Add your command handler code here
	m_DrawCurrent=CurID_RectFill;
	m_PushNumb=0;	
	pCurDraw->b_Select=0;
	Invalidate();	
}

void CVecDrawView::OnbRectRoundFill() 
{
	// TODO: Add your command handler code here
	m_DrawCurrent=CurID_RectRoundFill;
	m_PushNumb=0;	
	pCurDraw->b_Select=0;
	Invalidate();
	
}

void CVecDrawView::OnbSelectObject() 
{
	// TODO: Add your command handler code here
	m_DrawCurrent=CurID_SelectObject;
	m_PushNumb=0;
	pCurDraw->b_Select=0;
	Invalidate();
	
}
void CVecDrawView::OnbText() 
{
	// TODO: Add your command handler code here
	m_DrawCurrent=CurID_Text;
	m_PushNumb=0;	
	pCurDraw->b_Select=0;
	Invalidate();
}
void CVecDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int vType,vIndex;
	int Radius=0;
	COLORREF tColor;
	tColor=RGB(128,128,128);
	CVecDrawDoc *pDoc=GetDocument();
	CVecDrawApp *pApp=(CVecDrawApp *)AfxGetApp();
	CClientDC ht(this);
	if(m_DrawCurrent==CurID_DrawLine)
	{
		if(m_PushNumb==0)
		{
			m_PushNumb++;
			prePoint=point;
			lastPoint=point;
			SetCapture();
		}
		else if(m_PushNumb==1)
		{
			pDoc->AddLine(prePoint,point,tColor)->Draw(&ht,tColor);
			m_PushNumb=0;
			ReleaseCapture();
			vType=1;
			vIndex=pDoc->GetGraphNumb(vType);
			vIndex-=1;
			pDoc->AddSave(vType,vIndex);
			 

		}
	}
	if(m_DrawCurrent==CurID_DrawRect)
	{

		if(m_PushNumb==0)
		{
			m_PushNumb++;
			prePoint=point;
			lastPoint=point;
			SetCapture();
		}
		else if(m_PushNumb==1)
		{
			pDoc->AddRect(prePoint,point,tColor)->Draw(&ht,tColor);
			m_PushNumb=0;
			ReleaseCapture();
			vType=2;
			vIndex=pDoc->GetGraphNumb(vType);
			vIndex-=1;
			pDoc->AddSave(vType,vIndex);
			 
		}
	}
	if(m_DrawCurrent==CurID_RectFill)
	{

		if(m_PushNumb==0)
		{
			m_PushNumb++;
			prePoint=point;
			lastPoint=point;
			SetCapture();
		}
		else if(m_PushNumb==1)
		{
			pDoc->AddRectFill(prePoint,point,tColor)->Draw(&ht,tColor);
			m_PushNumb=0;
			ReleaseCapture();
			vType=4;
			vIndex=pDoc->GetGraphNumb(vType);
			vIndex-=1;
			pDoc->AddSave(vType,vIndex);
			 
		}
	}

	if(m_DrawCurrent==CurID_DrawCircle)
	{

		if(m_PushNumb==0)
		{
			m_PushNumb++;
			prePoint=point;
			lastPoint=point;
			SetCapture();
		}
		else if(m_PushNumb==1)
		{
			vType=3;
			Radius=(int)Distance(prePoint,point);
			pDoc->AddCircle(prePoint,Radius,tColor)->Draw(&ht,vType);
			m_PushNumb=0;
			ReleaseCapture();
			vIndex=pDoc->GetGraphNumb(vType);
			vIndex-=1;
			pDoc->AddSave(vType,vIndex);
			 
		}
	}
	if(m_DrawCurrent==CurID_Text)
	{
		CTextDlg InText;
		CString str;

		vType=8;
		InText.DoModal();
		str=InText.m_Text;
		if(str!="")
		{
//		str="This is a test!";
			pDoc->AddText(point,str,tColor)->Draw(&ht,vType);
		//ht.TextOut(point.x,point.y,str);
			vIndex=pDoc->GetGraphNumb(vType);
			vIndex-=1;
			pDoc->AddSave(vType,vIndex);
			 
			//        MessageBox("text sub");
		}
	}
	if(m_DrawCurrent==CurID_SelectObject)
	{
		CDraw *saveDraw=pCurDraw;
		m_Draging=1;
		prePoint=point;
		int i,nn,vT;
		for(vT=1;vT<9;vT++)
		{
			nn=pDoc->GetGraphNumb(vT);
			for(i=0;i<nn;i++)
				if(pDoc->GetGraph(vT,i)->IsPoint(point)<5)
				{
					CString str;
					str.Format("(%d,%d)",point.x,point.y);
//					pDoc->GetGraph(vT,i)->CurDraw(&ht,RGB(0,0,255),1);
					if(pDoc->GetGraph(vT,i)->b_Select!=1)
					{
						saveDraw->b_Select=0;
						pCurDraw=pDoc->GetGraph(vT,i);
						pCurDraw->b_Select=1;
						pDoc->AddSave(vT,i);						
					}
					else
					{						
						saveDraw->b_Select=0;
						pCurDraw=saveDraw;
					}
					ht.TextOut(point.x,point.y,str);
					goto Quit_Select;
			//	MessageBox("This is a test!");
				}
		}
Quit_Select:
	    Invalidate();
	}
	
	CView::OnLButtonDown(nFlags, point);
}

void CVecDrawView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int Radius;
	CVecDrawDoc *pDoc=GetDocument();
	CVecDrawApp *pApp=(CVecDrawApp *)AfxGetApp();
	CClientDC ddd(this);
	CPen pen(0,0,RGB(255,255,0));
	CPen *pOldPen=ddd.SelectObject(&pen);
	ddd.SetROP2(R2_NOT);
	if(m_DrawCurrent==CurID_DrawLine&&m_PushNumb==1)
	{
		if(lastPoint!=point)
		{

			ddd.MoveTo(prePoint);
			ddd.LineTo(lastPoint);
			ddd.MoveTo(prePoint);
			ddd.LineTo(point);

			lastPoint=point;
		}
	}
	if(m_DrawCurrent==CurID_DrawRect&&m_PushNumb==1)
	{
		if(lastPoint!=point)
		{
			ddd.Rectangle(prePoint.x,prePoint.y,lastPoint.x,lastPoint.y);
			ddd.Rectangle(prePoint.x,prePoint.y,point.x,point.y);
			lastPoint=point;
		}
	}
	if(m_DrawCurrent==CurID_RectFill&&m_PushNumb==1)
	{
		if(lastPoint!=point)
		{
			ddd.Rectangle(prePoint.x,prePoint.y,lastPoint.x,lastPoint.y);
			ddd.Rectangle(prePoint.x,prePoint.y,point.x,point.y);
			lastPoint=point;
		}
	}

	if(m_DrawCurrent==CurID_DrawCircle&&m_PushNumb==1)
	{
		if(lastPoint!=point)
		{
			Radius=(int)Distance(prePoint,lastPoint);
			ddd.Ellipse(prePoint.x-Radius,prePoint.y-Radius,prePoint.x+Radius,prePoint.y+Radius);
			Radius=(int)Distance(prePoint,point);
			ddd.Ellipse(prePoint.x-Radius,prePoint.y-Radius,prePoint.x+Radius,prePoint.y+Radius);
			lastPoint=point;
		}
	}	
    if((pCurDraw->b_Select==1)&&(m_Draging==1))
	{
		pCurDraw->Offset(point.x-prePoint.x,point.y-prePoint.y);
		prePoint=point;
		Invalidate();
//		OnDraw(pDC);
	}		
	CView::OnMouseMove(nFlags, point);
}

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



void CVecDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_Draging=0;
	CView::OnLButtonUp(nFlags, point);
}
/*
void CVecDrawView::ONGetMaxMinInfo()
{
	SetWindowPos(NULL,40,40,600,600,SWP_NOMOVE|SWP_NOZORDER);
}
*/

void CVecDrawView::OnbtnSerial() 
{
	// TODO: Add your command handler code here
	CSerailDlg SerialDlg;

	SerialDlg.DoModal();
	
}

void CVecDrawView::OnbReDraw() 
{
	// TODO: Add your command handler code here
	CVecDrawApp *pApp=(CVecDrawApp *)AfxGetApp();
	pApp->OnFileOpen();
}

⌨️ 快捷键说明

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