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

📄 2dcadview.cpp

📁 这个程序是按书本《Visual C++程序设计培训教程》写的
💻 CPP
字号:
// 2DCADView.cpp : implementation of the CMy2DCADView class
//

#include "stdafx.h"
#include "2DCAD.h"

#include "2DCADDoc.h"
#include "2DCADView.h"
#include "resource.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_MY_MESSAGE (WM_USER+101)
/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView

IMPLEMENT_DYNCREATE(CMy2DCADView, CView)

BEGIN_MESSAGE_MAP(CMy2DCADView, CView)
	ON_WM_CONTEXTMENU()
	//{{AFX_MSG_MAP(CMy2DCADView)
	ON_COMMAND(ID_GRAPH_POINT, OnGraphPoint)
	ON_UPDATE_COMMAND_UI(ID_GRAPH_POINT, OnUpdateGraphPoint)
	ON_COMMAND(ID_GRAPHY_LINE, OnGraphyLine)
	ON_UPDATE_COMMAND_UI(ID_GRAPHY_LINE, OnUpdateGraphyLine)
	ON_WM_LBUTTONDOWN()
	ON_WM_SETCURSOR()
	ON_WM_MOUSEMOVE()
	//}}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_MESSAGE(WM_MY_MESSAGE, OnMyMessage)//自己添加的
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView construction/destruction

CMy2DCADView::CMy2DCADView()
{
	// TODO: add construction code here
   m_bisPoint=FALSE;
   m_bisLine=FALSE;
   m_nStartY=0;
   m_nStartX=0;
   m_nStep=0;
   m_nEndX=0;
   m_nEndY=0;
}

CMy2DCADView::~CMy2DCADView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView drawing

void CMy2DCADView::OnDraw(CDC* pDC)
{
	CMy2DCADDoc* pDoc = GetDocument();
	
	ASSERT_VALID(pDoc);
///////// 在非客户区画图
//	pDC = GetWindowDC();  
//	pDC->MoveTo(0,0);
//	pDC->LineTo(20,20);
///////// 在非客户区画图


//	pDC->TextOut(20,20,"点");
//	pDC->SetPixel(100,40,RGB(255,0,0));//  画点
//	
//	pDC->TextOut(320,20,"线段");
//	pDC->MoveTo(400,40);//画直线
//	pDC->LineTo(500,40);
//
//	pDC->TextOut(20,170,"折线");
//	POINT Polyline[4]={{240,240},{80,120},{240,120},{80,240}};//画折线
//	pDC->Polyline(Polyline,4);
//
//	pDC->TextOut(320,170,"矩形");
//	pDC->Rectangle(390,110,600,230);//画矩形
//
//	pDC->TextOut(20,320,"椭圆");
//
//
//
//	pDC->Ellipse(80,260,280,380);//画矩形
//
//  pDC->TextOut(320,320,"多边形");
//	POINT Polygon[3]={{380,330},{530,260},{500,360}};//画折线
//	pDC->Polygon(Polygon,3);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView message handlers

void CMy2DCADView::OnGraphPoint() 
{
	// TODO: Add your command handler code here
	m_bisPoint=TRUE;
	CClientDC dc(this);//直接声明一个CDC类或者其派生类对象来获取设备环境
	dc.TextOut(100,100,"画点");

////用下面的方法获取设备环境,最后必须释放,此方法与上面实现一样
//    CDC *pDC;
//	pDC=GetDC();//
//	pDC->Ellipse(0,0,100,100);
//	ReleaseDC(pDC);

	SendMessage(WM_MY_MESSAGE);
}

void CMy2DCADView::OnUpdateGraphPoint(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetRadio(m_bisPoint);

}

void CMy2DCADView::OnContextMenu(CWnd*, CPoint point)
{
	// CG: This block was added by the Pop-up Menu component	{		if (point.x == -1 && point.y == -1)
		{			//keystroke invocation			CRect rect;			GetClientRect(rect);			ClientToScreen(rect);

			point = rect.TopLeft();
			point.Offset(5, 5);
		}

		CMenu menu;
		VERIFY(menu.LoadMenu(CG_IDR_POPUP_MY2_DCADVIEW));

		CMenu* pPopup = menu.GetSubMenu(0);
		ASSERT(pPopup != NULL);
		CWnd* pWndPopupOwner = this;

		while (pWndPopupOwner->GetStyle() & WS_CHILD)
			pWndPopupOwner = pWndPopupOwner->GetParent();

		pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
			pWndPopupOwner);
	}
}
LRESULT CMy2DCADView::OnMyMessage(WPARAM wParam,LPARAM lParam)
{
	CClientDC dc(this);
	dc.TextOut(100,50,"测试自定义消息");
	return 1;
}

void CMy2DCADView::OnGraphyLine() 
{
	// TODO: Add your command handler code here
	CClientDC dc(this);
	//dc.TextOut(70,30,"画线");
    dc.MoveTo(0,0);
	dc.LineTo(30,30);
//	CWindowDC wdc(this);
//	//dc.TextOut(0,0,"画线");
//    wdc.MoveTo(0,0);
//	wdc.LineTo(30,30);
	m_bisLine=TRUE;
}

void CMy2DCADView::OnUpdateGraphyLine(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
		pCmdUI->SetRadio(m_bisLine);
}

void CMy2DCADView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_nEndX=0;
	m_nEndY=0;
	CClientDC dc(this);
	dc.TextOut(100,100,"鼠标左键按下");
	if (nFlags&MK_SHIFT)
	{
		dc.TextOut(10,50," SHIFT键被按下");
	}
//	CClientDC dc(this);
	if (m_bisPoint)
	{
     dc.Ellipse(point.x-2,point.y-2,point.x+2,point.y+2);
	}
	if (m_bisLine)
	{
		//在鼠标两个点之间画线
		if (m_nStep==0)
		{
			m_nStartX=point.x;
			m_nStartY=point.y;
			m_nStep++;
			SetCapture();//捕捉鼠标
		}
		else
		{
			dc.MoveTo(m_nStartX,m_nStartY);
			dc.LineTo(point.x,point.y);
			
			m_nStep=0;
			m_nStartX=0;
			m_nStartY=0;
			::ReleaseCapture();// 释放鼠标捕捉
		}
	}

	CView::OnLButtonDown(nFlags, point);
}

BOOL CMy2DCADView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) //显示自己画的光标
{
	// TODO: Add your message handler code here and/or call default
	if (m_bisPoint)
	{
		HCURSOR hCursor;
		hCursor=AfxGetApp()->LoadCursor(IDC_POINTCURSOR);
		SetCursor(hCursor);
		return true;
	}
	if (m_bisLine)
	{
        HCURSOR hCursor;
		hCursor=AfxGetApp()->LoadStandardCursor(IDC_CROSS);
		SetCursor(hCursor);
		return true;
	}
	return CView::OnSetCursor(pWnd, nHitTest, message);
}

void CMy2DCADView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	if (m_bisLine)
	{
		if (m_nStep!=0)
		{
			dc.SetROP2(R2_NOTXORPEN);
			if (m_nEndX!=0)
			{
				dc.MoveTo(m_nStartX,m_nStartY);
				dc.LineTo(m_nEndX,m_nEndY);
			}
			m_nEndX=point.x;
			m_nEndY=point.y;
			dc.MoveTo(m_nStartX,m_nStartY);
			dc.LineTo(m_nEndX,m_nEndY);
		}
	}
	//获取状态栏窗口的指针
	CStatusBar* pStatus;
	pStatus=(CStatusBar*)AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);
	CView::OnMouseMove(nFlags, point);
	//输出鼠标的坐标值
	CString str;
	if (pStatus)//
	{
		str.Format("x=%3d,y=%3d",point.x,point.y);//将坐标写入字符串
		pStatus->SetPaneText(1,str);//将字符串输出到第二个窗格
	}
}

⌨️ 快捷键说明

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