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

📄 draw1view.cpp

📁 用c++编写的简单的绘图程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	CDC* pDC=GetDC();
	int post;
	CString objectName;
	CMainFrame* pMainFrm;
	CComboBox *p_ComBox;
  pMainFrm=(CMainFrame *)AfxGetApp()->GetMainWnd();
  p_ComBox=(CComboBox *)pMainFrm->m_wndDlgBar.GetDlgItem(ID_OBJ_NAME_BOX);
  post=p_ComBox->GetCurSel();
  p_ComBox->GetLBText(post,objectName);
		 if(currectObject!=NULL)
		 {
			 currectObject->Activation(pDC,currectBkColor);
			 currectObject->Draw(pDC);
		 }
	FindObjByName(objectName);
        currectObject->Activation(pDC,curretColor);
   OnEraseBkgnd(pDC);
}
//寻找该对象在链表中的位置
POSITION CDRAW1View::FindThePos(CShape *shape)
{
	POSITION pos=objectAll->GetHeadPosition();
 	//if(objectAll->IsEmpty())return pos;
   CShape*  tempShape;
    while(pos!=NULL)
	{  
	  tempShape=objectAll->GetNext(pos);
      if(tempShape->GetId()==shape->GetId()){pos=objectAll->Find(tempShape);break;}
	}
  return pos;
}

//放大缩小时改变对象的模式

void CDRAW1View::SetAllObectScal()
{
	if(objectAll->GetCount()==0)return;
	POSITION pos=objectAll->GetHeadPosition();
   CShape*  tempShape;
    while(pos!=NULL)
	{  
	  tempShape=objectAll->GetNext(pos);
      tempShape->x1=(int)(tempShape->x1*currectZoom);
      tempShape->x2=(int)(tempShape->x2*currectZoom);
      tempShape->y1=(int)(tempShape->y1*currectZoom);
      tempShape->y2=(int)(tempShape->y2*currectZoom);

      tempShape->x3=(int)(tempShape->x3*currectZoom);
      tempShape->x4=(int)(tempShape->x4*currectZoom);
      tempShape->y3=(int)(tempShape->y3*currectZoom);
      tempShape->y4=(int)(tempShape->y4*currectZoom);
	  tempShape->SetBorbarScal((int)(tempShape->GetBorbarScal()*currectZoom));
	}

}

////////////////////////////////////////////////////////////////////////
//以下为两个文件处理函数

//位图方式保存图象
void CDRAW1View::OnFileSave() 
{
	// TODO: Add your command handler code here

	CClientDC	dc(this);
	CDC 		memDC;
	CRect		rect;
	GetClientRect(rect);

	memDC.CreateCompatibleDC(&dc);
	CBitmap bm;
	int Width = rect.Width();
	int Height = rect.Height();
	bm.CreateCompatibleBitmap(&dc, Width, Height);
	CBitmap*  pOld = memDC.SelectObject(&bm);
	memDC.BitBlt(0, 0, Width, Height, &dc, 0, 0, SRCCOPY);
	memDC.SelectObject(pOld);
	BITMAP  btm;
	bm.GetBitmap(&btm);
	DWORD  size = btm.bmWidthBytes * btm.bmHeight;
	LPSTR lpData = (LPSTR)GlobalAllocPtr(GPTR, size);
	BITMAPFILEHEADER   bfh;
	/////////////////////////////////////////////
	BITMAPINFOHEADER  bih;
	bih.biBitCount = btm.bmBitsPixel;
	bih.biClrImportant = 0;
	bih.biClrUsed = 0;
	bih.biCompression = 0;
	bih.biHeight = btm.bmHeight;
	bih.biPlanes = 1;
	bih.biSize = sizeof(BITMAPINFOHEADER);
	bih.biSizeImage = size;
	bih.biWidth = btm.bmWidth;
	bih.biXPelsPerMeter = 0;
	bih.biYPelsPerMeter = 0;
	GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
	bfh.bfReserved1 = bfh.bfReserved2 = 0;
	bfh.bfType = ((WORD)('M'<< 8)|'B');
	bfh.bfSize = 54 + size;
	bfh.bfOffBits = 54;

	CFileDialog dlg(false,_T("BMP"),_T("*.bmp"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("*.bmp|*.bmp|*.*|*.*|"));
	if (dlg.DoModal()!=IDOK)
		return;

	CFile  bf;

	CString ss=dlg.GetPathName();
	if(bf.Open(ss, CFile::modeCreate | CFile::modeWrite))
	{
		bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER));
		bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER));
		bf.WriteHuge(lpData, size);
		bf.Close();
		AfxMessageBox("保存成功");
	}
	GlobalFreePtr(lpData);
}

//位图方式打开对象
void CDRAW1View::OnFileOpen() 
{
	// TODO: Add your command handler code here
	CDC  dc;
	CDC * pDC = GetDC();

	if(!dc.CreateCompatibleDC(pDC))
		return;
	CBitmap b;
	BITMAP  bm;
	CString  path;
	CFileDialog dlg(true,_T("BMP"),_T("*.bmp"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("*.bmp|*.bmp|*.*|*.*|"));
	if (dlg.DoModal()==IDOK)
	{
		path =dlg.GetPathName();
	}
	else 
		return;
	HBITMAP  hbmp = (HBITMAP)LoadImage(NULL, _T(path), IMAGE_BITMAP,
		0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
	b.Attach(hbmp);

	b.GetBitmap(&bm);
	dc.SelectObject(&b);
	pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dc, 0, 0, SRCCOPY);
	
}
//成员函数结束
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
//以下为所有事件


//////////////////////////////////////////////////////////////////////////
//以下为对象对话条事件

//移动事件
//东移动对象
void CDRAW1View::OnObjMoveEast() 
{
	// TODO: Add your control notification handler code here
  CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->MoveShape(DIR_EAST);
  OnEraseBkgnd(pDC);
	
}

//西移动对象
void CDRAW1View::OnObjMoveWest() 
{
	// TODO: Add your control notification handler code here
  CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->MoveShape(DIR_WEST);
  OnEraseBkgnd(pDC);
	
}
//南移动对象
void CDRAW1View::OnObjMoveSouth() 
{
	// TODO: Add your control notification handler code here
  CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->MoveShape(DIR_SOUTH);
  OnEraseBkgnd(pDC);
}
//北移动对象
void CDRAW1View::OnObjMoveNorth() 
{
	// TODO: Add your control notification handler code here
  CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->MoveShape(DIR_NORTH);
  OnEraseBkgnd(pDC);
}

///////////旋转事件
//顺时针
void CDRAW1View::OnObjAntic() 
{
	// TODO: Add your control notification handler code here
  CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->Circumrotate(ID_OBJ_ANTIC);
  OnEraseBkgnd(pDC);
}
//逆时针
void CDRAW1View::OnObjDelic() 
{
	// TODO: Add your control notification handler code here
  CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->Circumrotate(ID_OBJ_DELIC);
  OnEraseBkgnd(pDC);
	
}

void CDRAW1View::OnObjSizeLarge() 
{
	// TODO: Add your control notification handler code here
  CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->SizeScal(IDC_OBJ_SIZE_LARGE);
  OnEraseBkgnd(pDC);
	
}
void CDRAW1View::OnObjSizeSmall() 
{
	// TODO: Add your control notification handler code here
  CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->SizeScal(IDC_OBJ_SIZE_SMALL);
  OnEraseBkgnd(pDC);
	
}
////////////边框事件
//边框变大
void CDRAW1View::OnObjLarge() 
{
	// TODO: Add your control notification handler code here
  CDC*   pDC=GetDC();
  int borScal;
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
   borScal=objectAll->GetAt(pos)->GetBorbarScal();
   if(borScal<20)borScal+=1;
   else return;
  objectAll->GetAt(pos)->SetBorbarScal(borScal);
  OnEraseBkgnd(pDC);
	
}
//边框变小
void CDRAW1View::OnObjSmall() 
{
	// TODO: Add your control notification handler code here
CDC*   pDC=GetDC();
  int borScal;
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
   borScal=objectAll->GetAt(pos)->GetBorbarScal();
   if(borScal>1)borScal-=1;
   else return;
  objectAll->GetAt(pos)->SetBorbarScal(borScal);
  OnEraseBkgnd(pDC);
}

//变成填充模式
void CDRAW1View::OnObjFillHave() 
{
	// TODO: Add your control notification handler code here
	CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->SetIfFill(true);
  OnEraseBkgnd(pDC);
}
//变成不填充模式
void CDRAW1View::OnObjFillNone() 
{
	// TODO: Add your control notification handler code here
	CDC*   pDC=GetDC();
  if( objectAll->IsEmpty())return;
  POSITION pos;
  pos=FindThePos(currectObject);//查找对象
  objectAll->GetAt(pos)->SetIfFill(false);
  OnEraseBkgnd(pDC);
	
}

void CDRAW1View::OnObjActHave() 
{
	// TODO: Add your control notification handler code here
	CDC*   pDC=GetDC();
	  currectObject->Activation(pDC,curretColor);
}
void CDRAW1View::OnObjActNone() 
{
	// TODO: Add your control notification handler code here
	  CDC*   pDC=GetDC();
	  currectObject->Activation(pDC,currectBkColor);
     //OnEraseBkgnd( pDC)
	  currectObject->Draw(pDC);
}

///////////////删除事件
//删除对象当前
void CDRAW1View::OnObjDelete() 
{
	// TODO: Add your control notification handler code here
	CDC* pDC=GetDC();
  CString objectName;
  if( objectAll->IsEmpty())return;
  int post;
  bool isTail=false;
  POSITION pos;
  pos=FindThePos(currectObject);//删除对象
	CMainFrame* pMainFrm;
	CComboBox *p_ComBox;
   pMainFrm=(CMainFrame *)AfxGetApp()->GetMainWnd();
   p_ComBox=(CComboBox *)pMainFrm->m_wndDlgBar.GetDlgItem(ID_OBJ_NAME_BOX);
  
   post=p_ComBox->GetCurSel();

   currectObject->Delete(pDC,currectBkColor);
   p_ComBox->DeleteString(post);

   if(objectAll->GetTail()->GetId()==objectAll->GetAt(pos)->GetId())
	   isTail=true;//检测是否到尾部
	   objectAll->RemoveAt(pos);

   if(currectObject!=NULL)
	  currectObject->Activation(pDC,currectBkColor);
	  currectObject->Draw(pDC);

   //重设当前对象
   if(!objectAll->IsEmpty())
   {
	   if(isTail)p_ComBox->SetCurSel(--post);
	   else p_ComBox->SetCurSel(post);
       p_ComBox->GetLBText(post,objectName);
		  FindObjByName(objectName);
   }
   else 
   {
	  currectObject=NULL;
       p_ComBox->ResetContent();
   }

   if(currectObject!=NULL)//激活当前对象
	  currectObject->Activation(pDC,curretColor);

    OnEraseBkgnd(pDC);//发送重画消息
	
}



////////////////////////////////////////////////////////////////////////
//以下为四大视图事件

//鼠标左键按下事件
//用于创建基本图元及图形放大(全)
void CDRAW1View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDC* pDC = GetDC();//获得DC
	CDC 		memDC;
	OnPrepareDC(pDC);//设置DC的滚动属性,与ScollView的滚动有关
	pDC->DPtoLP(&point);//转换当前点为逻辑位置坐标
	CDRAW1Doc* pDoc = GetDocument();
	CRect client;
	GetClientRect(&client);//获得客户区范围
	ClientToScreen(&client);//转化为屏幕坐标
	::ClipCursor(&client);//锁定鼠标移动范围

   switch(curretHandObj)
   {
   case ID_IMAGE_PEN://画笔情况
        switch(curretShape)
		{
		case ID_SHAPE_STR_LINE://画直线
         createObject=new CLine(curretColor,currectBordarScal);
         currectState=OBJECT_STATE_CREATING;//设置当前状态为创建
         createObject->NewPoint(point.x,point.y);
		 break;
		case ID_SHAPE_RECTANGLE://画四边形
         createObject=new CRectangle(curretColor,currectBordarScal,currectFill);
         currectState=OBJECT_STATE_CREATING;
         createObject->NewPoint(point.x,point.y);
		 break;
		case ID_SHAPE_SQUARE://画正方形
         createObject=new CSquare(curretColor,currectBordarScal,currectFill);
         currectState=OBJECT_STATE_CREATING;
         createObject->NewPoint(point.x,point.y);
		 break;
		case ID_SHAPE_ELLIPSE://画椭圆
         createObject=new CEllipse(curretColor,currectBordarScal,currectFill);
         currectState=OBJECT_STATE_CREATING;
         createObject->NewPoint(point.x,point.y);
		 break;
		case ID_SHAPE_CIRCLE://画圆
        createObject=new CCircle(curretColor,currectBordarScal,currectFill);
        currectState=OBJECT_STATE_CREATING;
         createObject->NewPoint(point.x,point.y);
		 break;
		case ID_SHAPE_TRIANGLE://画三角形
        createObject=new CTriangle(curretColor,currectBordarScal,currectFill);
        currectState=OBJECT_STATE_CREATING;
         createObject->NewPoint(point.x,point.y);
		 break;
		}

⌨️ 快捷键说明

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