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

📄 2dcadview.cpp

📁 Visual c++.程序设计培训教程
💻 CPP
字号:
// 2DCADView.cpp : implementation of the CMy2DCADView class
//

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView
//自定义消息
#define       WM_MY_MESSAGE            (WM_USER + 101)

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_UPDATE_COMMAND_UI(ID_GRAPH_LINE, OnUpdateGraphLine)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage) //自定义消息
	ON_COMMAND(ID_GRAPH_LINE, OnGraphLine)
	// 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()

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

CMy2DCADView::CMy2DCADView()
{
	// TODO: add construction code here
	m_bIsPoint = FALSE;
	m_bIsLine  = FALSE;
}

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);
	// 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
	CClientDC dc(this);
	//在视图区中输出文本“画点”
	dc.TextOut(100, 100, "画点");

	m_bIsPoint = TRUE;//允许画点
	m_bIsLine  = FALSE;

	SendMessage(WM_MY_MESSAGE);//发送自定义消息
}

void CMy2DCADView::OnUpdateGraphPoint(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetRadio(m_bIsPoint);//根据布尔变量的值控制用户界面更新
}

//手动添加的消息映射函数
void CMy2DCADView::OnGraphLine() 
{
	CClientDC dc(this);
	//在视图区中输出文本“画线”
	dc.TextOut(100, 100, "画线");

	m_bIsLine  = TRUE;//允许画线
	m_bIsPoint = FALSE;
}

void CMy2DCADView::OnUpdateGraphLine(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetRadio(m_bIsLine);//根据布尔变量的值控制用户界面更新
}

#include "resource.h"
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;//声明一个CMenu类的对象		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();
		//使用TrackPopupMenu函数弹出并跟踪菜单		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;
}

⌨️ 快捷键说明

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