📄 2dcadview.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)
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_KEYDOWN()
ON_WM_MOUSEMOVE()
//}}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;
m_nStep = 0;//操作步数初始为0
m_nStartX = m_nStartY = 0;//坐标值初始也为0
m_nEndX = 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);
// 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;
}
void CMy2DCADView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/* CClientDC dc(this);//获取设备环境
//在视图区中输出文本“鼠标左键按下”
dc.TextOut(100, 100, "鼠标左键按下");
if( nFlags & MK_SHIFT )
{//检查在按下鼠标左键的同时,Shift键是否被按下
dc.TextOut(100, 50, "Shift键被按下");
}
*/
CClientDC dc(this);//获取设备环境
//////////////////////////////////////////////////
// 以下的代码在视图区中画点 //
//////////////////////////////////////////////////
if(m_bIsPoint)//如果程序处于画点状态
{
//调用CDC::Ellipse函数以点为中心,画一个小的圆圈
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 ++;//操作步数加1
SetCapture();//捕捉鼠标
}
else//如果是线段终点,则在视图区中画出线段
{
dc.MoveTo(m_nStartX, m_nStartY);
dc.LineTo(point.x, point.y);//使用MoveTo、LineTo函数画线
m_nStep = 0;//完成一次画线段后,将操作步数归零
m_nStartX = m_nStartY = 0;//将记录起点坐标归零
m_nEndX = m_nEndY = 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)
{//如果处于画点状态,就使用自定义的IDC_POINTER光标
HCURSOR hCursor;
hCursor = AfxGetApp()->LoadCursor(IDC_POINTER);//装载自定义光标
SetCursor(hCursor); //设置光标
return TRUE;//直接返回,不再调用CView::OnSetCursor
}
if(m_bIsLine)
{//如果处于画线状态,就使用Windows系统的IDC_CROSS十字光标
HCURSOR hCursor;
hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);//装载自定义光标
SetCursor(hCursor); //设置光标
return TRUE;
}
//其它情况下,由基类的OnSetCursor函数来设置缺省光标形状
return CView::OnSetCursor(pWnd, nHitTest, message);
}
void CMy2DCADView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
//如果按下Esc键,则终止所有的绘图工作
if(nChar == VK_ESCAPE)
{
//清除掉画点和画线状态
m_bIsPoint = FALSE;
m_bIsLine = FALSE;
//将一些成员变量恢复初始值
m_nStep = 0;//操作步数归零
m_nStartX = m_nStartY = 0;//记录起点坐标归零
::ReleaseCapture();//释放鼠标捕捉
}
//将消息交给基类的OnKeyDown函数处理
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
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);
}
}
//////////////////////////////////////////////////////////////////
// 在状态栏的第二个窗格中输出鼠标当前位置 //
//////////////////////////////////////////////////////////////////
CString str; //声明一个字符串
CStatusBar* pStatus;
pStatus = (CStatusBar*)AfxGetApp()->m_pMainWnd->
GetDescendantWindow(AFX_IDW_STATUS_BAR);//获取状态栏窗口指针
if (pStatus) //如果获取指针成功(不为空)
{
str.Format("x = %3d,y = %3d", point.x, point.y);//将坐标写入字符串
pStatus->SetPaneText(1, str);//将字符串输出到第二个窗格
}
//调用基类的鼠标移动消息处理函数
CView::OnMouseMove(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -