Visual C++ MFC绘制熊猫吉祥物的代码
时间: 2024-01-22 12:02:20 浏览: 338
以下是一个基于 MFC 的绘制熊猫吉祥物的示例代码:
```cpp
void CPandaView::OnDraw(CDC* pDC)
{
CPandaDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// 获取绘图区域的宽度和高度
CRect rect;
GetClientRect(&rect);
int nWidth = rect.Width();
int nHeight = rect.Height();
// 计算熊猫头部的位置和大小
int nHeadX = nWidth / 2;
int nHeadY = nHeight / 3;
int nHeadSize = nHeight / 3;
// 绘制熊猫头部
CPen pen(PS_SOLID, 3, RGB(0, 0, 0));
CBrush brush(RGB(255, 255, 255));
CPen* pOldPen = pDC->SelectObject(&pen);
CBrush* pOldBrush = pDC->SelectObject(&brush);
pDC->Ellipse(nHeadX - nHeadSize, nHeadY - nHeadSize, nHeadX + nHeadSize, nHeadY + nHeadSize);
// 计算熊猫眼睛的位置和大小
int nEyeSize = nHeadSize / 4;
int nLeftEyeX = nHeadX - nHeadSize / 2 + nEyeSize * 2;
int nRightEyeX = nHeadX + nHeadSize / 2 - nEyeSize * 2;
int nEyeY = nHeadY - nHeadSize / 4;
// 绘制熊猫眼睛
CBrush blackBrush(RGB(0, 0, 0));
CBrush* pOldBrush2 = pDC->SelectObject(&blackBrush);
pDC->Ellipse(nLeftEyeX - nEyeSize, nEyeY - nEyeSize, nLeftEyeX + nEyeSize, nEyeY + nEyeSize);
pDC->Ellipse(nRightEyeX - nEyeSize, nEyeY - nEyeSize, nRightEyeX + nEyeSize, nEyeY + nEyeSize);
// 绘制熊猫鼻子
CBrush redBrush(RGB(255, 0, 0));
CBrush* pOldBrush3 = pDC->SelectObject(&redBrush);
pDC->Ellipse(nHeadX - nEyeSize, nHeadY, nHeadX + nEyeSize, nHeadY + nEyeSize);
// 绘制熊猫嘴巴
CPen pen2(PS_SOLID, 2, RGB(0, 0, 0));
CPen* pOldPen2 = pDC->SelectObject(&pen2);
pDC->Arc(nHeadX - nHeadSize / 2, nHeadY, nHeadX + nHeadSize / 2, nHeadY + nHeadSize / 2, nHeadX - nHeadSize / 2, nHeadY, nHeadX + nHeadSize / 2, nHeadY);
// 绘制熊猫耳朵
CPoint pt[3];
pt[0] = CPoint(nHeadX - nHeadSize / 2, nHeadY - nHeadSize / 2);
pt[1] = CPoint(nHeadX - nHeadSize / 4, nHeadY - nHeadSize * 3 / 4);
pt[2] = CPoint(nHeadX + nHeadSize / 4, nHeadY - nHeadSize / 2);
pDC->Polygon(pt, 3);
pt[0] = CPoint(nHeadX + nHeadSize / 2, nHeadY - nHeadSize / 2);
pt[1] = CPoint(nHeadX + nHeadSize / 4, nHeadY - nHeadSize * 3 / 4);
pt[2] = CPoint(nHeadX - nHeadSize / 4, nHeadY - nHeadSize / 2);
pDC->Polygon(pt, 3);
// 恢复画笔和画刷
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldBrush2);
pDC->SelectObject(pOldBrush3);
pDC->SelectObject(pOldPen2);
}
```
在上述代码中,我们使用了 MFC 的绘图函数,如 `SelectObject`、`Ellipse`、`Polygon` 等,来绘制熊猫的各个部分。通过计算各个部分的位置和大小,我们可以将熊猫绘制在绘图区域的中央。
阅读全文