📄 designview.cpp
字号:
// DesignView.cpp : implementation of the CDesignView class
//
#include "stdafx.h"
#include "Design.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDesignView
IMPLEMENT_DYNCREATE(CDesignView, CScrollView)
BEGIN_MESSAGE_MAP(CDesignView, CScrollView)
//{{AFX_MSG_MAP(CDesignView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
ON_UPDATE_COMMAND_UI(ID_EDIT_DELETE, OnUpdateEditDelete)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_WIDTH_HEIGHT, OnUpdateWidthHeight)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDesignView construction/destruction
void CDesignView::OnUpdateWidthHeight(CCmdUI *pCmdUI)
{
pCmdUI->Enable(m_Dragging);
CString strPage;
CDesignDoc* pDoc = GetDocument();
UINT currentelem = pDoc->m_CurrentElement; // get current item
if ((currentelem == ID_ELEMENT_ROOM) || (currentelem == ID_ELEMENT_ROOF))
{
strPage.Format( "x = %d, y = %d; Width = %d; Height = %d", m_PointOrigin.x/8, m_PointOrigin.y/8,
abs(m_CurPos.x - m_PointOrigin.x/8) , abs(m_CurPos.y - m_PointOrigin.y/8));
}
else
{
strPage.Format( "x = %d, y = %d", m_CurPos.x, m_CurPos.y);
}
pCmdUI->SetText( strPage );
}
CDesignView::CDesignView()
{
// TODO: add construction code here
// pDoc->m_ElemSelected = -1;
m_Deletable = TRUE;
m_Dragging = 0;
// pDoc->m_sel_pt_a = (0,0);
// pDoc->m_sel_pt_b = (0,0);
m_PenDotted.CreatePen(PS_DOT, 1, RGB(0,0,0));
m_HArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
m_HCross = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
}
CDesignView::~CDesignView()
{
}
BOOL CDesignView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
m_Classname = AfxRegisterWndClass
(CS_HREDRAW | CS_VREDRAW,
0,
(HBRUSH)::GetStockObject(WHITE_BRUSH),
0);
cs.lpszClass = m_Classname;
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDesignView drawing
void CDesignView::OnDraw(CDC* pDC)
{
CDesignDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CSize ScrollSize = GetTotalSize();
CPen Pen, *pOldPen;
//select pen
Pen.CreatePen(PS_INSIDEFRAME, 1, RGB(192,192,192)); //grey
pOldPen = pDC->SelectObject(&Pen);
CRect ClipRect;
CRect DimRect;
CRect IntRect;
CElement *pElement;
//Draw grid
for (int i = 0; i <= ScrollSize.cx; i = i + 8)
{
pDC->MoveTo(i, 0);
pDC->LineTo(i, ScrollSize.cy);
}
for (i = 0; i <= ScrollSize.cy; i = i + 8)
{
pDC->MoveTo(0, i);
pDC->LineTo(ScrollSize.cx, i);
}
pDC->GetClipBox(&ClipRect);
int NumElem = pDoc->GetNumElem();
for (i = 0; i < NumElem; ++i)
{
pElement = pDoc->GetElem(i);
if (pElement->m_ElemType == ROOF)
{
DimRect = pElement->GetDimRect();
if(IntRect.IntersectRect(DimRect, ClipRect))
pElement->Draw(pDC);
}
}
for (i = 0; i < NumElem; ++i)
{
pElement = pDoc->GetElem(i);
if (pElement->m_ElemType == ROOM)
{
DimRect = pElement->GetDimRect();
if(IntRect.IntersectRect(DimRect, ClipRect))
pElement->Draw(pDC);
}
}
for (i = 0; i < NumElem; ++i)
{
pElement = pDoc->GetElem(i);
if (pElement->m_ElemType == DOOR)
{
DimRect = pElement->GetDimRect();
if(IntRect.IntersectRect(DimRect, ClipRect))
pElement->Draw(pDC);
}
}
for (i = 0; i < NumElem; ++i)
{
pElement = pDoc->GetElem(i);
if (pElement->m_ElemType == WINDOW)
{
DimRect = pElement->GetDimRect();
if(IntRect.IntersectRect(DimRect, ClipRect))
pElement->Draw(pDC);
}
}
////Draw dotted rect here////
pDC->SelectObject(&m_PenDotted);
pDC->Rectangle(pDoc->m_sel_pt_a.x, pDoc->m_sel_pt_a.y, pDoc->m_sel_pt_b.x + 1, pDoc->m_sel_pt_b.y + 1);
pDC->SelectObject(pOldPen);
}
/////////////////////////////////////////////////////////////////////////////
// CDesignView printing
BOOL CDesignView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDesignView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDesignView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDesignView diagnostics
#ifdef _DEBUG
void CDesignView::AssertValid() const
{
CScrollView::AssertValid();
}
void CDesignView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CDesignDoc* CDesignView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDesignDoc)));
return (CDesignDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDesignView message handlers
void CDesignView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
SetScrollSizes( MM_TEXT, GetDocument()->GetDocSize());
}
void CDesignView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC ClientDC(this);
OnPrepareDC(&ClientDC);
ClientDC.DPtoLP(&point);
CDesignDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
UINT currentelem = pDoc->m_CurrentElement; // get current item
int x = point.x;
int y = point.y;
int NumElem, i, j;
//////Snap to grid
x = (point.x/8)*8;
y = (point.y/8)*8;
CPoint npoint(x, y);
////// Check if cursor is within drawing area
CSize ScrollSize = GetTotalSize();
CRect ScrollRect(0,0,ScrollSize.cx, ScrollSize.cy);
if (!ScrollRect.PtInRect(npoint))
return;
CElement *pElement;
CRect Temprect, Temprect2;
////// Check if starting position is within drawable area (outside all rooms; for room selection only)
if (currentelem == ID_ELEMENT_ROOM)
{
NumElem = pDoc->GetNumElem();
for (i = 0; i < NumElem; ++i)
{
pElement = pDoc->GetElem(i);
if (pElement->m_ElemType == ROOM) // if it is a room
{
Temprect = pElement->GetDimRect();
Temprect.DeflateRect(1,1);
if (Temprect.PtInRect(npoint)) // starting point is located inside an existing room
return;
}
}
}
////// Modify and save cursor postion
m_PointOrigin = npoint;
ClientDC.SetROP2(R2_NOT);
ClientDC.SelectObject(&m_PenDotted);
ClientDC.SetBkMode(TRANSPARENT);
ClientDC.SelectStockObject(NULL_BRUSH);
////// Draw dotted box if Door or Window is selected
if ((currentelem == ID_ELEMENT_DOOR) || (currentelem == ID_ELEMENT_WINDOW))
{
if (m_PointOrigin.x < 24)
m_PointOrigin.x = 24;
if (m_PointOrigin.y < 24)
m_PointOrigin.y = 24;
ClientDC.Rectangle(m_PointOrigin.x - 24, m_PointOrigin.y - 24, m_PointOrigin.x, m_PointOrigin.y);
}
m_PointOld = m_PointOrigin;
SetCapture();
m_Dragging = 1;
/////// Select element to be deleted
BOOL IsSelected = FALSE;
if (currentelem == ID_ELEMENT_CURSOR)
{
// ClientDC.Rectangle(pDoc->m_sel_pt_a.x, pDoc->m_sel_pt_a.y, pDoc->m_sel_pt_b.x + 1, pDoc->m_sel_pt_b.y + 1);
/////// Search if any element is selected
NumElem = pDoc->GetNumElem();
/////// Search door & window
for (i = 0; ((i < NumElem) && (IsSelected == FALSE)); ++i)
{
pElement = pDoc->GetElem(i);
if ((pElement->m_ElemType == DOOR) || (pElement->m_ElemType == WINDOW)) // if it is a door or window
{
Temprect = pElement->GetDimRect();
if (Temprect.PtInRect(npoint)) // starting point is located inside an existing room
{
IsSelected = TRUE;
pDoc->m_ElemSelected = i;
m_Deletable = TRUE;
pDoc->m_sel_pt_a.x = Temprect.TopLeft().x;
pDoc->m_sel_pt_a.y = Temprect.TopLeft().y;
pDoc->m_sel_pt_b.x = Temprect.BottomRight().x;
pDoc->m_sel_pt_b.y = Temprect.BottomRight().y;
}
}
}
/////// Search roof
for (i = 0; ((i < NumElem) && (IsSelected == FALSE)); ++i)
{
pElement = pDoc->GetElem(i);
if (pElement->m_ElemType == ROOF) // if it is a roof
{
Temprect = pElement->GetDimRect();
if (Temprect.PtInRect(npoint)) // starting point is located inside an existing room
{
IsSelected = TRUE;
pDoc->m_ElemSelected = i;
m_Deletable = TRUE;
pDoc->m_sel_pt_a.x = Temprect.TopLeft().x;
pDoc->m_sel_pt_a.y = Temprect.TopLeft().y;
pDoc->m_sel_pt_b.x = Temprect.BottomRight().x;
pDoc->m_sel_pt_b.y = Temprect.BottomRight().y;
}
}
}
/////// Search room (if found, check if any door, window, or roof is associated to it)
for (i = 0; ((i < NumElem) && (IsSelected == FALSE)); ++i)
{
pElement = pDoc->GetElem(i);
if (pElement->m_ElemType == ROOM) // if it is a door or window
{
CRect IntRect;
// if (IntRect.IntersectRect(Temprect, Currentrect))
Temprect = pElement->GetDimRect();
if (Temprect.PtInRect(npoint)) // starting point is located inside an existing room
{
IsSelected = TRUE;
pDoc->m_ElemSelected = i;
m_Deletable = TRUE;
pDoc->m_sel_pt_a.x = Temprect.TopLeft().x;
pDoc->m_sel_pt_a.y = Temprect.TopLeft().y;
pDoc->m_sel_pt_b.x = Temprect.BottomRight().x;
pDoc->m_sel_pt_b.y = Temprect.BottomRight().y;
/////// Check if it has any other components attached to it
CElement *pSearchElem;
for (j = 0; ((j < NumElem) && (m_Deletable == TRUE)); ++j)
{
pSearchElem = pDoc->GetElem(j);
if (pSearchElem->m_ElemType != ROOM)
{
Temprect2 = pSearchElem->GetDimRect();
if (IntRect.IntersectRect(Temprect, Temprect2))
{
m_Deletable = FALSE;
}
}
}
}
}
}
if (IsSelected == FALSE)
{
pDoc->m_ElemSelected = -1;
pDoc->m_sel_pt_a = (0,0);
pDoc->m_sel_pt_b = (0,0);
}
Invalidate(FALSE);
}
/////// Clip mouse cursor:
ClientDC.LPtoDP(&ScrollRect);
CRect ViewRect;
GetClientRect(&ViewRect);
CRect IntRect;
IntRect.IntersectRect(&ScrollRect, &ViewRect);
ClientToScreen(&IntRect);
::ClipCursor(&IntRect);
CScrollView::OnLButtonDown(nFlags, point);
}
void CDesignView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDesignDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!m_Dragging)
return;
m_Dragging = 0;
::ReleaseCapture();
::ClipCursor(NULL);
CClientDC ClientDC(this);
OnPrepareDC(&ClientDC);
ClientDC.DPtoLP(&point);
ClientDC.SetROP2(R2_NOT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -