📄 test6view.cpp
字号:
pDC->SelectObject(pOldBrush);
ReleaseDC(pDC);
CScrollView::OnLButtonUp(nFlags, point);
}
void CTest6View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (nFlags & MK_RBUTTON)
{
if (point.x < TILEWIDTH)
SendMessage(WM_HSCROLL, (WPARAM)SB_LINELEFT, (LPARAM)NULL);
if (point.x > m_rectClient.right - TILEWIDTH)
SendMessage(WM_HSCROLL, (WPARAM)SB_LINERIGHT, (LPARAM)NULL);
if (point.y < TILEHEIGHT)
SendMessage(WM_VSCROLL, (WPARAM)SB_LINEUP, (LPARAM)NULL);
if (point.y > m_rectClient.bottom - TILEHEIGHT)
SendMessage(WM_VSCROLL, (WPARAM)SB_LINEDOWN, (LPARAM)NULL);
}
point += GetScrollPosition();
int x = point.x/TILEWIDTH;
int y = point.y/TILEHEIGHT;
CString str;
str.Format("(%d,%d)", x, y);
m_pStatusBar->SetPaneText(0, str);
if (x < 0 || y < 0 || x >= MAPWIDTH || y >= MAPHEIGHT)
return;
if (m_nFlag == 1 && (nFlags & MK_RBUTTON))
{
m_bIsModified = TRUE;
bool bFlag1=true, bFlag2=true;
CDC *pDC = GetDC();
pDC->SetWindowOrg(GetScrollPosition());
CPen *pOldPen, pen1(PS_SOLID, 1, RGB(125,125,125));
pOldPen = pDC->SelectObject(&pen1);
CBrush *pOldBrush, brush0(RGB(255,255,255)), brush1(RGB(0,0,0));
pOldBrush = pDC->SelectObject(&brush0);
CRect rect1(m_ptTemp1.x/TILEWIDTH, m_ptTemp1.y/TILEHEIGHT,
m_ptTemp2.x/TILEWIDTH, m_ptTemp2.y/TILEHEIGHT);
rect1.NormalizeRect();
rect1.right++; rect1.bottom++;
m_ptTemp2 = point;
CRect rect2(m_ptTemp1.x/TILEWIDTH, m_ptTemp1.y/TILEHEIGHT,
m_ptTemp2.x/TILEWIDTH, m_ptTemp2.y/TILEHEIGHT);
rect2.NormalizeRect();
rect2.right++; rect2.bottom++;
/* for (int i=rect1.top; i<=rect1.bottom; i++)
for (int j=rect1.left; j<=rect1.right; j++)
m_Map[i][j] = 0;
pDC->Rectangle(rect1.left*TILEWIDTH, rect1.top*TILEHEIGHT,
rect1.right*TILEWIDTH, rect1.bottom*TILEWIDTH);
pDC->SelectObject(&brush1);
for (i=rect2.top; i<=rect2.bottom; i++)
for (int j=rect2.left; j<=rect2.right; j++)
m_Map[i][j] = 1;
pDC->Rectangle(rect2.left*TILEWIDTH, rect2.top*TILEHEIGHT,
rect2.right*TILEWIDTH, rect2.bottom*TILEWIDTH);
*/
CRect rect3,rect4;
if (rect1.TopLeft() == rect2.TopLeft())
{
rect3.SetRect(rect1.left, min(rect1.bottom,rect2.bottom), max(rect1.right,rect2.right), max(rect1.bottom,rect2.bottom));
rect3.NormalizeRect();
bFlag1 = (rect1.bottom < rect2.bottom)?false:true;
rect4.SetRect(min(rect1.right,rect2.right), rect1.top, max(rect1.right,rect2.right), max(rect1.bottom,rect2.bottom));
rect4.NormalizeRect();
bFlag2 = (rect1.right < rect2.right)?false:true;
}
else if (rect1.right == rect2.right && rect1.top == rect2.top)
{
rect3.SetRect(min(rect1.left,rect2.left), rect1.top, max(rect1.left,rect2.left), max(rect1.bottom, rect2.bottom));
rect3.NormalizeRect();
bFlag1 = (rect1.left < rect2.left)?true:false;
rect4.SetRect(max(rect1.left,rect2.left), min(rect1.bottom,rect2.bottom), rect1.right, max(rect1.bottom,rect2.bottom));
rect4.NormalizeRect();
bFlag2 = (rect1.bottom < rect2.bottom)?false:true;
}
else if (rect1.BottomRight() == rect2.BottomRight())
{
rect3.SetRect(min(rect1.left,rect2.left), min(rect1.top,rect2.top), rect1.right, max(rect1.top,rect2.top));
rect3.NormalizeRect();
bFlag1 = (rect1.top < rect2.top)?true:false;
rect4.SetRect(min(rect1.left,rect2.left), max(rect1.top,rect2.top), max(rect1.left,rect2.left), rect1.bottom);
rect4.NormalizeRect();
bFlag2 = (rect1.left < rect2.left)?true:false;
}
else if (rect1.left == rect2.left && rect1.bottom == rect2.bottom)
{
rect3.SetRect(rect1.left, min(rect1.top,rect2.top), max(rect1.right,rect2.right), max(rect1.top,rect2.top));
rect3.NormalizeRect();
bFlag1 = (rect1.top < rect2.top)?true:false;
rect4.SetRect(min(rect1.right,rect2.right), min(rect1.top,rect2.top), max(rect1.right, rect2.right), rect1.bottom);
rect4.NormalizeRect();
bFlag2 = (rect1.right < rect2.right)?false:true;
}
CRect rect;
rect = rect3;
if (!rect.IsRectEmpty())
{
if (bFlag1)//m_Map[rect.top][rect.left] > 0)
{
pDC->SelectObject(&brush0);
for (int i=rect.top; i<rect.bottom; i++)
for (int j=rect.left; j<rect.right; j++)
{
m_Map[i][j] = 0;
pDC->Rectangle(j*TILEWIDTH, i*TILEHEIGHT, (j+1)*TILEWIDTH+1, (i+1)*TILEHEIGHT+1);
}
}
else
{
pDC->SelectObject(&brush1);
for (int i=rect.top; i<rect.bottom; i++)
for (int j=rect.left; j<rect.right; j++)
{
m_Map[i][j] = 1;
pDC->Rectangle(j*TILEWIDTH, i*TILEHEIGHT, (j+1)*TILEWIDTH+1, (i+1)*TILEHEIGHT+1);
}
}
}
rect = rect4;
if (!rect.IsRectEmpty())
{
if (bFlag2)//m_Map[rect.top][rect.left] > 0)
{
pDC->SelectObject(&brush0);
for (int i=rect.top; i<rect.bottom; i++)
for (int j=rect.left; j<rect.right; j++)
{
m_Map[i][j] = 0;
pDC->Rectangle(j*TILEWIDTH, i*TILEHEIGHT, (j+1)*TILEWIDTH+1, (i+1)*TILEHEIGHT+1);
}
}
else
{
pDC->SelectObject(&brush1);
for (int i=rect.top; i<rect.bottom; i++)
for (int j=rect.left; j<rect.right; j++)
{
m_Map[i][j] = 1;
pDC->Rectangle(j*TILEWIDTH, i*TILEHEIGHT, (j+1)*TILEWIDTH+1, (i+1)*TILEHEIGHT+1);
}
}
}
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
ReleaseDC(pDC);
}
CScrollView::OnMouseMove(nFlags, point);
}
void CTest6View::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();
if (m_nFlag == 1)
{
m_bIsModified = TRUE;
point += GetScrollPosition();
m_ptTemp1 = point;
m_ptTemp2 = point;
m_Map[point.y/TILEHEIGHT][point.x/TILEWIDTH] = 1;
CDC *pDC = GetDC();
pDC->SetWindowOrg(GetScrollPosition());
CPen *pOldPen, pen1(PS_SOLID, 1, RGB(125,125,125));
pOldPen = pDC->SelectObject(&pen1);
CBrush *pOldBrush, brush1(RGB(0,0,0));
pOldBrush = pDC->SelectObject(&brush1);
pDC->Rectangle(point.x/TILEWIDTH*TILEWIDTH, point.y/TILEHEIGHT*TILEHEIGHT,
(point.x/TILEWIDTH+1)*TILEWIDTH+1, (point.y/TILEHEIGHT+1)*TILEHEIGHT+1);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
CScrollView::OnRButtonDown(nFlags, point);
}
void CTest6View::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
ReleaseCapture();
CScrollView::OnRButtonUp(nFlags, point);
}
void CTest6View::OnRefresh()
{
// TODO: Add your command handler code here
for (int i=0;i<MAPHEIGHT;i++)
for (int j=0;j<MAPWIDTH;j++)
m_Map[i][j] = 0;
m_ptStart.x = -1; m_ptStart.y = 0;
m_ptEnd.x = -1; m_ptEnd.y = 0;
m_nFlag = 0;
m_bIsModified = FALSE;
Invalidate();
}
/*
void CTest6View::OnChangemap()
{
// TODO: Add your command handler code here
CChangeMapDlg dlg;
if (dlg.DoModal() == ID_OK)
{
MAPWIDTH = dlg.m_nWidth;
MAPHEIGHT = dlg.m_nHeight;
CSize sizeTotal;
sizeTotal.cx = MAPWIDTH*TILEWIDTH + 1;
sizeTotal.cy = MAPHEIGHT*TILEHEIGHT + 1;
SetScrollSizes(MM_TEXT, sizeTotal);
OnRefresh();
}
}
*/
void CTest6View::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GetClientRect(&m_rectClient);
}
void CTest6View::OnNewMap()
{
// TODO: Add your command handler code here
if (IsModified())
{
CString str;
str = "文件" + m_strFileName + "的正文已经更改。\n" + "是否保存更改?";
int rval = MessageBox(str, "A*算法演示程序", MB_YESNOCANCEL | MB_ICONWARNING);
if (rval == IDCANCEL)
return;
else if (rval == IDYES)
OnSaveMap();
}
m_strFileName = "Newmap.map";
OnRefresh();
AfxGetMainWnd()->SetWindowText("A*算法演示程序 -- Newmap.map");
}
void CTest6View::OnOpenMap()
{
// TODO: Add your command handler code here
if (IsModified())
{
CString str;
str = "文件" + m_strFileName + "的正文已经更改。\n" + "是否保存更改?";
int rval = MessageBox(str, "A*算法演示程序", MB_YESNOCANCEL | MB_ICONWARNING);
if (rval == IDCANCEL)
return;
else if (rval == IDYES)
OnSaveMap();
}
CFileDialog dlg(TRUE, _T("map"), NULL, OFN_PATHMUSTEXIST,
_T("Map Files(*.map)|*.map||"), this);
if (dlg.DoModal() == IDOK)
{
m_nFlag = 0;
m_strFileName = dlg.GetPathName();
CFile file(m_strFileName, CFile::modeRead);
file.Read(&m_ptStart, sizeof(POINT));
file.Read(&m_ptEnd, sizeof(POINT));
for (int i=0; i<MAPHEIGHT; i++)
for (int j=0; j<MAPWIDTH; j++)
file.Read(&m_Map[i][j], sizeof(int));
Invalidate();
CString strTitle;
strTitle = "A*算法演示程序 -- " + dlg.GetFileName();
AfxGetMainWnd()->SetWindowText(strTitle);
m_bIsModified = FALSE;
}
}
void CTest6View::OnSaveMap()
{
// TODO: Add your command handler code here
CFile file(m_strFileName, CFile::modeCreate | CFile::modeWrite);
file.Write(&m_ptStart, sizeof(POINT));
file.Write(&m_ptEnd, sizeof(POINT));
for (int i=0; i<MAPHEIGHT; i++)
for (int j=0; j<MAPWIDTH; j++)
file.Write(&m_Map[i][j], sizeof(int));
m_bIsModified = FALSE;
}
void CTest6View::OnSaveMapAs()
{
// TODO: Add your command handler code here
CFileDialog dlg(FALSE, _T("map"), NULL, OFN_PATHMUSTEXIST,
_T("Map Files(*.map)|*.map||"), this);
if (dlg.DoModal() == IDOK)
{
m_bIsModified = FALSE;
m_strFileName = dlg.GetPathName();
OnSaveMap();
CString strTitle;
strTitle = "A*算法演示程序 -- " + dlg.GetFileName();
AfxGetMainWnd()->SetWindowText(strTitle);
}
}
BOOL CTest6View::IsModified()
{
return m_bIsModified;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -