Visual C++ MFC绘制哆啦A梦代码
时间: 2023-07-11 18:52:08 浏览: 217
C++的MFC绘图编程
以下是一个简单的Visual C++ MFC程序,可以绘制哆啦A梦的图像:
```cpp
#include "stdafx.h"
#include "MyDoraemon.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define PI 3.1415926
// CMyDoraemonApp
BEGIN_MESSAGE_MAP(CMyDoraemonApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CMyDoraemonApp 构造
CMyDoraemonApp::CMyDoraemonApp()
{
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
}
// 唯一的 CMyDoraemonApp 对象
CMyDoraemonApp theApp;
// CMyDoraemonApp 初始化
BOOL CMyDoraemonApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
// CMyDoraemonWnd
CMyDoraemonWnd::CMyDoraemonWnd()
{
Create(NULL, _T("My Doraemon"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 600, 600), NULL, NULL);
}
CMyDoraemonWnd::~CMyDoraemonWnd()
{
}
BEGIN_MESSAGE_MAP(CMyDoraemonWnd, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
// CMyDoraemonWnd 消息处理程序
void CMyDoraemonWnd::OnPaint()
{
CPaintDC dc(this);
int cx = 300, cy = 300;
int r1 = 200, r2 = 80, r3 = 50;
int x1 = cx - 45, y1 = cy - 60;
int x2 = cx + 45, y2 = cy - 60;
int x3 = cx, y3 = cy + 100;
int x4 = cx - 75, y4 = cy + 70;
int x5 = cx + 75, y5 = cy + 70;
// 绘制脸部
CPen pen(PS_SOLID, 10, RGB(255, 255, 255));
dc.SelectObject(&pen);
dc.Ellipse(cx - r1, cy - r1, cx + r1, cy + r1);
// 绘制眼睛
dc.SelectObject(&CPen(PS_SOLID, 20, RGB(0, 0, 0)));
dc.MoveTo(x1, y1);
dc.LineTo(x1 + 90, y1);
dc.MoveTo(x2, y2);
dc.LineTo(x2 - 90, y2);
dc.SelectObject(&CBrush(RGB(0, 0, 0)));
dc.Ellipse(cx - r2, cy - 40, cx - r2 + 80, cy + 40);
dc.Ellipse(cx + r2 - 80, cy - 40, cx + r2, cy + 40);
dc.SelectObject(&CBrush(RGB(255, 255, 255)));
dc.Ellipse(cx - r3, cy - 20, cx - r3 + 40, cy + 20);
dc.Ellipse(cx + r3 - 40, cy - 20, cx + r3, cy + 20);
// 绘制嘴巴
dc.SelectObject(&CPen(PS_SOLID, 20, RGB(255, 0, 0)));
dc.MoveTo(x3 - 40, y3);
dc.LineTo(x3 + 40, y3);
dc.MoveTo(x3 - 40, y3);
dc.AngleArc(x3, y3, 40, 180, 360);
// 绘制身体
dc.SelectObject(&CPen(PS_SOLID, 20, RGB(0, 0, 0)));
dc.Rectangle(cx - 110, cy + 70, cx + 110, cy + 270);
dc.SelectObject(&CBrush(RGB(255, 255, 255)));
dc.Ellipse(cx - 75, cy + 70, cx - 5, cy + 130);
dc.Ellipse(cx + 5, cy + 70, cx + 75, cy + 130);
// 绘制胳膊
dc.MoveTo(x4 - 20, y4);
dc.LineTo(x4 + 20, y4);
dc.MoveTo(x5 - 20, y5);
dc.LineTo(x5 + 20, y5);
dc.SelectObject(&CPen(PS_SOLID, 40, RGB(255, 255, 255)));
dc.MoveTo(x4, y4);
dc.LineTo(x4 - 60, y4 + 75);
dc.MoveTo(x5, y5);
dc.LineTo(x5 + 60, y5 + 75);
// 绘制小铃铛
dc.SelectObject(&CPen(PS_SOLID, 10, RGB(0, 0, 0)));
dc.SelectObject(&CBrush(RGB(255, 255, 0)));
dc.Ellipse(cx - 20, cy + 130, cx + 20, cy + 170);
dc.SelectObject(&CPen(PS_SOLID, 5, RGB(0, 0, 0)));
dc.MoveTo(cx - 20, cy + 150);
dc.LineTo(cx - 40, cy + 150);
dc.AngleArc(cx, cy + 150, 20, 180, 360);
dc.MoveTo(cx + 20, cy + 150);
dc.LineTo(cx + 40, cy + 150);
dc.AngleArc(cx, cy + 150, 20, 0, 180);
}
```
该程序绘制了哆啦A梦的头部、眼睛、嘴巴、身体、胳膊和小铃铛等部分。在窗口中显示图像,可以直接运行该程序。
阅读全文