欢迎来到我的博客https://www.cnblogs.com/veis/

https://www.cnblogs.com/veis/p/14182037.html

MFC绘制一个心形

1.效果图(窗口已经被抠出为♥形)

2、核心代码

// WM_INITDIALOG消息
BOOL CRedHeardDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	CreateHeart(m_rgn);
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

// WM_PAINT消息
void CRedHeardDlg::OnPaint()
{
	CPaintDC dc(this); // 用于绘制的设备上下文
	CBrush br(RGB(255, 0, 0));
	CRgn r;
	r.CreateRectRgn(0, 0, 0, 0);
	r.CopyRgn(&m_rgn);
	dc.FillRgn(&r, &br);
	this->SetWindowRgn(r, TRUE);
}

// 生成心形的函数
// 其中在文件头部有定义#define M_PI 3.14159265358979323846
void CRedHeardDlg::CreateHeart(CRgn &rgn)
{
	CRect rect;
	GetWindowRect(rect);
	CArray<CPoint> lines;
	int a = 10;
	for (float t = 0; t < 2 * M_PI; t += 0.001)
	{
		float x = 16 * pow(sinf(t), 3);
		float y = 13 * cosf(t) - 5 * cosf(2 * t) - 2 * cosf(3 * t) - cosf(4 * t);
		lines.Add(CPoint(x * a + rect.Width() / 2, -y * a + rect.Height() / 2));
	}
	int nSize = lines.GetSize();
	rgn.CreatePolygonRgn(&lines[0], nSize, ALTERNATE);
}

  

posted @ 2020-05-21 14:22  veis  阅读(884)  评论(0编辑  收藏  举报