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

📄 drawview.cpp

📁 C++案
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			dc.LineTo(point.x, point.y);//使用MoveTo、LineTo函数画线
			int i=pDoc->linenum;
			pDoc->line[i][0]=start.x;  //记录线段起点坐标
			pDoc->line[i][1]=start.y;  
			pDoc->line[i][2]=point.x;  //记录线段终点坐标
			pDoc->line[i][3]=point.y;  
			pDoc->line[i][4]=1;
			pDoc->linenum++;         //线段总数加1
			pDoc->SetModifiedFlag();    //设置文档“脏”标志
			pDoc->UpdateAllViews(this);   // 重画所有视图
			step=0;//完成一次画线段后,将操作步数归零
			start.x=0;
			start.y=0;//将记录起点坐标归零
			end.x=0;
			end.y=0;//将终点坐标归零
			::ReleaseCapture();//释放鼠标捕捉
		}
	}
	//绘图完毕后落选画笔
    if(m_rectangle)//如果程序处于画线状态
	{
		if(step==0)
		{
			start.x=point.x;
			start.y=point.y;
			step++;
			SetCapture();
		}
		else
		{
			dc.Rectangle(start.x,start.y,point.x,point.y);
			int i=pDoc->recnum;
			pDoc->rec[i][0]=start.x; 
			pDoc->rec[i][1]=start.y;  
			pDoc->rec[i][2]=point.x; 
			pDoc->rec[i][3]=point.y; 
			pDoc->rec[i][4]=1;
			pDoc->recnum++;     
			pDoc->SetModifiedFlag();
			pDoc->UpdateAllViews(this);  
			step=0;
			start.x=0;
			start.y=0;
			end.x=0;
			end.y=0;
			::ReleaseCapture();
		}
	}
	dc.SelectObject(poldpen);
	dc.SelectObject(oldBr);
    
	if (drag)
	{
		CDrawDoc* pDoc = GetDocument();
	    ASSERT_VALID(pDoc);
	    for(int i=0;i<pDoc->recnum;i++)
		{
		    int x1 = pDoc->rec[i][0];
		    int y1 = pDoc->rec[i][1];
		    int x2 = pDoc->rec[i][2];
		    int y2 = pDoc->rec[i][3];
			CRect rect(x1,y1,x2,y2);
			BOOL t1=rect.PtInRect(point);
            if (t1)
			{   rect0=rect;
				for (int j=0;j<pDoc->recnum;j++)
				{
					int flag=pDoc->rec[j][4];
					if (flag==2)
					{
                        int x01 = pDoc->rec[j][0];
						int y01 = pDoc->rec[j][1];
						int x02 = pDoc->rec[j][2];
						int y02 = pDoc->rec[j][3];
						CRect rect1(x01,y01,x02,y02);
						int oldmode=dc.SetROP2(R2_NOTXORPEN);
						CPen newpen,*oldpen;
						newpen.CreatePen(PS_SOLID,1,RGB(255,0,0));
						CBrush newbrush,*oldbrush;
						newbrush.CreateSolidBrush(RGB(0,255,255));
						oldpen=dc.SelectObject(&newpen);
						oldbrush=dc.SelectObject(&newbrush);
						dc.Rectangle(rect1.left-3,rect1.top-3,rect1.left+3,rect1.top+3);
						dc.Rectangle(rect1.right-3,rect1.bottom-3,rect1.right+3,rect1.bottom+3);
						dc.Rectangle(rect1.left+rect1.Width()-3,rect1.top-3,rect1.left+rect1.Width()+3,rect1.top+3);
						dc.Rectangle(rect1.left-3,rect1.top+rect1.Height()-3,rect1.left+3,rect1.top+rect1.Height()+3);
						dc.Rectangle(rect1.left+rect1.Width()/2-3,rect1.top+rect1.Height()/2-3,rect1.left+rect1.Width()/2+3,rect1.top+rect1.Height()/2+3);
						dc.Rectangle(rect1);
						dc.SelectObject(oldbrush);
						dc.SelectObject(oldpen);
						dc.SetROP2(oldmode);
                        newpen.CreatePen(PS_SOLID,1,RGB(0,0,255));
						oldpen=dc.SelectObject(&newpen);
						dc.Rectangle(rect1);
						dc.SelectObject(oldpen);
						pDoc->rec[j][4]=1;
					}
				}
				CPen newpen,*oldpen;
				newpen.CreatePen(PS_SOLID,1,RGB(255,0,0));
				CBrush newbrush,*oldbrush;
				newbrush.CreateSolidBrush(RGB(0,255,255));
				oldpen=dc.SelectObject(&newpen);
				oldbrush=dc.SelectObject(&newbrush);
				dc.Rectangle(rect.left-3,rect.top-3,rect.left+3,rect.top+3);
				dc.Rectangle(rect.right-3,rect.bottom-3,rect.right+3,rect.bottom+3);
				dc.Rectangle(rect.left+rect.Width()-3,rect.top-3,rect.left+rect.Width()+3,rect.top+3);
				dc.Rectangle(rect.left-3,rect.top+rect.Height()-3,rect.left+3,rect.top+rect.Height()+3);
				dc.Rectangle(rect.left+rect.Width()/2-3,rect.top+rect.Height()/2-3,rect.left+rect.Width()/2+3,rect.top+rect.Height()/2+3);
			    dc.SelectObject(oldbrush);
				dc.SelectObject(oldpen);
				pDoc->rec[i][4]=2;
				selectindex=i;
			}
		}
    }
	CView::OnLButtonDown(nFlags, point);
}

void CDrawView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	CDrawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: Add your specialized code here and/or call the base class
}

void CDrawView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);//获取设备环境
	if (move)
	{
	  if(m_bLBtnDown)
	  {
		CSize offset(point-movepoint);
		rect0+=offset;
		movepoint=point;
		Invalidate();
	  }
	}
	if(m_line)
	{
		if(step!=0)//如果不是起始点,拉出橡皮条
		{
			dc.SetROP2(R2_NOTXORPEN);//设置线条模式
			if(end.x!=0)
			{//如果不是第一次画线,则擦除上一条线
				dc.MoveTo(start);
				dc.LineTo(end);
			}
			end.x=point.x;
			end.y=point.y;
			//画出新的线段
			dc.MoveTo(start);
			dc.LineTo(end);
		}
	}
	if(m_rectangle)
	{
		if(step!=0)
		{
			CBrush* poldbrush=(CBrush*)dc.SelectStockObject(NULL_BRUSH);
			int old=dc.SetROP2(R2_NOTXORPEN);
			int oldmode=dc.SetBkMode(0);
			if(end.x!=0)
			{
				dc.Rectangle(start.x,start.y,end.x,end.y);
			}
			end.x=point.x;
			end.y=point.y;
			dc.Rectangle(start.x,start.y,end.x,end.y);
			dc.SelectObject(poldbrush);
			dc.SetROP2(old);
			dc.SetBkMode(oldmode);
		}
	}
	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);
}

void CDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    if(m_bLBtnDown)
	{
		m_bLBtnDown=FALSE;
	}
	CView::OnLButtonUp(nFlags, point);
}

BOOL CDrawView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_point)
	{//如果处于画点状态,就使用自定义的IDC_POINTER光标
		HCURSOR   hCursor;
		hCursor = AfxGetApp()->LoadCursor(IDC_CURSOR1);//装载自定义光标
		SetCursor(hCursor);							   //设置光标
		return TRUE;//直接返回,不再调用CView::OnSetCursor
	}
	if(m_line) 
	{//如果处于画线状态,就使用Windows系统的IDC_CROSS十字光标
		HCURSOR   hCursor;
		hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);//装载自定义光标
		SetCursor(hCursor);									 //设置光标
		return TRUE;
	} 
	if(move)
	{//如果处于画点状态,就使用自定义的IDC_POINTER光标
		HCURSOR   hCursor;
		hCursor = AfxGetApp()->LoadCursor(IDC_CURSOR3);//装载自定义光标
		SetCursor(hCursor);							   //设置光标
		return TRUE;//直接返回,不再调用CView::OnSetCursor
	}
	if(drag)
	{//如果处于画点状态,就使用自定义的IDC_POINTER光标
		HCURSOR   hCursor;
		hCursor = AfxGetApp()->LoadCursor(IDC_CURSOR2);//装载自定义光标
		SetCursor(hCursor);							   //设置光标
		return TRUE;//直接返回,不再调用CView::OnSetCursor
	}
	return CView::OnSetCursor(pWnd, nHitTest, message);
}


void CDrawView::OnDrag() 
{
	// TODO: Add your command handler code here
	drag=TRUE;
	m_point=FALSE;
	m_line=FALSE;
	m_rectangle=FALSE;
}

void CDrawView::OnUpdateDrag(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(drag);
}

void CDrawView::OnSetLinetype() 
{
	// TODO: Add your command handler code here
	CLineStyle dlg;
	if (dlg.DoModal()==IDOK)
	{
		CDrawDoc* pDoc = GetDocument();
	    ASSERT_VALID(pDoc);
		pDoc->linestyle=dlg.index;
		Invalidate();
	}
}

void CDrawView::OnSetLinewidth() 
{
	// TODO: Add your command handler code here
	CLineWid dlg;
	if (dlg.DoModal()==IDOK)
	{
		CDrawDoc* pDoc = GetDocument();
	    ASSERT_VALID(pDoc);
		pDoc->linewidth=dlg.width;
		Invalidate();
	}
}

void CDrawView::OnSetColor() 
{
	// TODO: Add your command handler code here
	CLineColor dlg;
	if (dlg.DoModal()==IDOK)
	{
		CDrawDoc* pDoc = GetDocument();
	    ASSERT_VALID(pDoc);
		pDoc->linecolor=dlg.color;
		Invalidate();
	}
}

void CDrawView::OnMove() 
{
	// TODO: Add your command handler code here
	move=TRUE;
	m_line=FALSE;
	m_point=FALSE;
	m_rectangle=FALSE;
}

void CDrawView::OnUpdateMove(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(move);
}

void CDrawView::OnUpdateSetLinewidth(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
}

⌨️ 快捷键说明

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