【精品】画点-画线-移动点(鼠标左键按下-鼠标右键按下-鼠标移动...5
画点-画线-移动点(鼠标左键按下-鼠标右键按下-鼠标移动) DrawGraphView.cpp
// DrawGraphView.cpp : implementation of the CDrawGraphView class
//
#include "stdafx.h"
#include "DrawGraph.h"
#include "DrawGraphDoc.h"
#include "DrawGraphView.h"
#include
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CDrawGraphView
static long startx = 0;
static long starty = 0;
static long endx = 0;
static long endy = 0;
static short flagCap = 0;
static short flagMovePnt = 0;
IMPLEMENT_DYNCREATE(CDrawGraphView, CView)
BEGIN_MESSAGE_MAP(CDrawGraphView, CView)
// 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(ID_DRAW_DRAWPOINT, &CDrawGraphView::OnDrawDrawpoint)
ON_COMMAND(ID_DRAW_DRAWLINE, &CDrawGraphView::OnDrawDrawline)
ON_COMMAND(ID_DRAW_MOVEPOINT, &CDrawGraphView::OnDrawMovepoint)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
// CDrawGraphView construction/destruction
int CalcMinDist(PNT_INFO* pPnt,int num, PNT_INFO& pnt)
{
float fTmp=0,fDist=99999;
int iNo = -1;
if(pPnt==NULL||num<=0)
return -1;
for(int i=0;iSelectObject(&pen);
pDC->SetROP2(R2_NOTXORPEN );
switch(m_iDrawMode)
{
case 1:
case 3:
for(i=0;iRectangle((int)m_point[i].x-5,(int)m_point[i].y-5,(int)m_point[i].x+5,(int)m_point[i].
y+5);
break;
case 2:
for(i=0;iMoveTo((int)m_line[i].pnt[j].x,(int)m_line[i].pnt[j].y);
pDC->LineTo((int)m_line[i].pnt[j+1].x,(int)m_line[i].pnt[j+1].y);
}
}
break;
default:
break;
}
// TODO: add draw code for native data here
}
// CDrawGraphView printing
BOOL CDrawGraphView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo); }
void CDrawGraphView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) {
// TODO: add extra initialization before printing
}
void CDrawGraphView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) {
// TODO: add cleanup after printing }
// CDrawGraphView diagnostics
#ifdef _DEBUG
void CDrawGraphView::AssertValid() const
{
CView::AssertValid();
}
void CDrawGraphView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawGraphDoc* CDrawGraphView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawGraphDoc)));
return (CDrawGraphDoc*)m_pDocument;
}
#endif //_DEBUG
// CDrawGraphView message handlers
void CDrawGraphView::OnDrawDrawpoint()
{
// TODO: Add your command handler code here
m_iDrawMode = 1;
}
void CDrawGraphView::OnDrawDrawline()
{
// TODO: Add your command handler code here
m_iDrawMode = 2;
}
void CDrawGraphView::OnDrawMovepoint()
{
// TODO: Add your command handler code here
m_iDrawMode = 3;
}
void CDrawGraphView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC = GetDC();
CPen pen, *pOldPen=NULL;
pDC->SetROP2(R2_NOTXORPEN );
pOldPen = pDC->SelectObject(&pen);
if(m_iDrawMode == 1)
{
m_point[m_pointNum].x = (float)point.x;
m_point[m_pointNum].y = (float)point.y;
pDC->Rectangle((int)m_point[m_pointNum].x-5,(int)m_point[m_pointNum].y-5,(int)m_point[m_pointNum].x+5,(int)m_point[m_pointNum].y+5);
m_pointNum++;
}
if(m_iDrawMode == 2)
{
startx = point.x;
starty = point.y;
endx = point.x;
endy = point.y;
pDC->MoveTo(startx,starty);
pDC->LineTo(endx,endy);
m_linepnt[m_linepntNum].x = (float)point.x;
m_linepnt[m_linepntNum].y = (float)point.y;
m_linepntNum++;
SetCapture();
flagCap = 1;
}
if(m_iDrawMode == 3)
{
if(flagMovePnt==0)
{
flagMovePnt = 1;
PNT_INFO pnt;
pnt.x = (float)point.x;
pnt.y = (float)point.y;
//需要加入求距离最短点的函数,返回点数组下标;
m_iNo = CalcMinDist(m_point,m_pointNum,pnt);
SetCapture();
}
else
{
flagMovePnt = 0;
::ReleaseCapture();
}
}
pDC->SelectObject(pOldPen);
ReleaseDC(pDC);
CView::OnLButtonDown(nFlags, point);
}
void CDrawGraphView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC = GetDC();
CPen pen, *pOldPen=NULL;
pDC->SetROP2(R2_NOTXORPEN );
pOldPen = pDC->SelectObject(&pen);
if(m_iDrawMode == 2)
{
pDC->MoveTo(startx,starty);
pDC->LineTo(endx,endy);
endx = point.x;
endy = point.y;
flagCap = 0;
for(int i=0;iSelectObject(pOldPen);
ReleaseDC(pDC);
CView::OnRButtonDown(nFlags, point); }
void CDrawGraphView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC = GetDC();
CPen pen, *pOldPen=NULL;
pDC->SetROP2(R2_NOTXORPEN);
pOldPen = pDC->SelectObject(&pen);
if(m_iDrawMode == 2)
{
if(flagCap==1)
{
pDC->MoveTo(startx,starty);
pDC->LineTo(endx,endy);
endx = point.x;
endy = point.y;
pDC->MoveTo(startx,starty);
pDC->LineTo(endx,endy);
}
}
if(m_iDrawMode == 3)
{
if(m_iNo<0)
goto lab;
if(flagMovePnt==1)
{
pDC->Rectangle(m_point[m_iNo].x-5,m_point[m_iNo].y-5,m_point[m_iNo].x+5,m_point[m_iNo].y+5)
;
m_point[m_iNo].x = point.x;
m_point[m_iNo].y = point.y;
pDC->Rectangle(m_point[m_iNo].x-5,m_point[m_iNo].y-5,m_point[m_iNo].x+5,m_point[m_iNo].y+5)
;
}
}
lab:
pDC->SelectObject(pOldPen);
ReleaseDC(pDC);
CView::OnMouseMove(nFlags, point); }
DrawGraphView.h
// DrawGraphView.h : interface of the CDrawGraphView class
//
#pragma once
typedef struct tagPoint
{
float x;
float y;
}PNT_INFO;
typedef struct tagLine
{
PNT_INFO pnt[100];
long num;
}LIN_INFO;
class CDrawGraphView : public CView
{
protected: // create from serialization only
CDrawGraphView();
DECLARE_DYNCREATE(CDrawGraphView)
// Attributes
public:
CDrawGraphDoc* GetDocument() const;
// Operations
public:
// Overrides
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
// Implementation
public:
virtual ~CDrawGraphView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnDrawDrawpoint();
public:
afx_msg void OnDrawDrawline();
public:
afx_msg void OnDrawMovepoint();
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
public:
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
public:
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
public:
short m_iDrawMode;
public:
long m_pointNum;
public:
long m_lineNum;
public:
long m_linepntNum;
public:
int m_iNo;
public:
PNT_INFO m_point[100];
LIN_INFO m_line[100];
PNT_INFO m_linepnt[100];
};
#ifndef _DEBUG // debug version in DrawGraphView.cpp
inline CDrawGraphDoc* CDrawGraphView::GetDocument() const
{ return reinterpret_cast(m_pDocument); } #endif