108上位机VC MFC设备上下文件CDC绘制三维矩形
108上位机VC MFC设备上下文件CDC绘制三维矩形
功能展示
设备上下文件CDC绘制三维矩形在多媒体编辑会有用到,但CDC没有现成的函数绘制三维图形,我们当前例程通过 MoveTo(), LineTo()函数实现三维矩形绘制,效果如图 要点提示 绘制图形前我们得设置它默认的原点,不然绘制的图形部份不可见,设置原点我可以用CDC的SetViewportOrg()设置,它跟随一个坐标点参数,我们可以传入窗口的中心点进行便可;
实现功能 1.新建基于对话框的应用程序 2.拖拽一图片控件,用于绘制矩形,设置ID为IDC_RECT 添加四编辑框关联UINT类型变量,用于设置矩形的长宽高角度,分别为 UINT m_nWidth; UINT m_nHeight; UINT m_nLength; UINT m_nAngle;
3.添加自定义函数DrawStereoRect()现添加四个编辑框的EN_CHANGE消息响应函数,下面是五个函数的函数体 - void CGkbc8Dlg::OnChangeEdit1() //width
- {
- DrawStereoRect();
- }
- void CGkbc8Dlg::OnChangeEdit4() //height
- {
- DrawStereoRect();
- }
- void CGkbc8Dlg::OnChangeEdit3() //length
- {
- DrawStereoRect();
- }
- void CGkbc8Dlg::OnChangeEdit2() //angle
- {
- DrawStereoRect();
- }
复制代码- void CGkbc8Dlg::DrawStereoRect()
- {
- UpdateData();
- //
- CDC* pDC;
- pDC = GetDlgItem(IDC_RECT)->GetDC();
- CRect rc,rect;
- GetDlgItem(IDC_RECT)->GetClientRect(rect);
- GetDlgItem(IDC_RECT)->GetWindowRect(rc);
- pDC->FillRect(rect,NULL);
- //取出中心点
- CPoint center;
- center.x=rc.Width()/2;
- center.y=rc.Height()/2;
- pDC->SetViewportOrg(center);
- CPoint LTop,LBottom,RTop,RBottom;
- LTop.x=1-m_nLength/2;
- LTop.y=1-m_nHeight/2;
- RTop.x=m_nLength/2;
- RTop.y=1-m_nHeight/2;
- LBottom.x=1-m_nLength/2;
- LBottom.y=m_nHeight/2;
- RBottom.x=m_nLength/2;
- RBottom.y=m_nHeight/2;
- CPen pen(PS_SOLID,1,RGB(0,0,0));
- CPen DOTPen; //虚线
- DOTPen.CreatePen(PS_DOT,1,RGB(0,0,0));
- pDC->SelectObject(&pen);
- //画正面矩形
- pDC->Rectangle(LTop.x,LTop.y,RBottom.x,RBottom.y);
- CPoint LeftTop,RightTop;
- LeftTop.x=(long)(LTop.x+(cos(m_nAngle*PI/180)*m_nWidth));
- LeftTop.y=(long)(LTop.y-(sin(m_nAngle*PI/180)*m_nWidth));
- RightTop.x=LeftTop.x+m_nLength;
- RightTop.y=LeftTop.y;
- pDC->MoveTo(LTop);
- pDC->LineTo(LeftTop);
- pDC->LineTo(RightTop);
-
复制代码- pDC->LineTo(RTop);
- CPoint Other,DotPoint;
- DotPoint.x=LeftTop.x ;
- DotPoint.y=LeftTop.y+m_nHeight;
- pDC->MoveTo(RightTop);
- if(m_nAngle<89)
- {
- pDC->SelectObject(&pen);
- Other.x=RightTop.x;
- Other.y=RightTop.y+m_nHeight;
- pDC->LineTo(Other);
- pDC->LineTo(RBottom);
- pDC->SelectObject(&DOTPen);
- pDC->MoveTo(LeftTop);
- pDC->LineTo(DotPoint);
- pDC->LineTo(LBottom);
- }
- else
- {
- pDC->SelectObject(&DOTPen);
- Other.x=RightTop.x;
- Other.y=RightTop.y+m_nHeight;
- pDC->LineTo(Other);
- pDC->LineTo(RBottom);
- pDC->SelectObject(&pen);
- pDC->MoveTo(LeftTop);
- pDC->LineTo(DotPoint);
- pDC->LineTo(LBottom);
- }
- pDC->SelectObject(&DOTPen);
- pDC->MoveTo(DotPoint);
- pDC->LineTo(Other);
- }
- <div style="text-align: center;"><span style="font-size: xx-large; line-height: 1.5;">我们来演示实现过程</span></div>
复制代码
|