mfc_timer_advanced - 8BitsCoding/RobotMentor GitHub Wiki


이미지

// Dlg.h
#define MAX_COUNT 50

struct CircleData
{
	int x, y;		// 쀑심점
	int r;			// λ°˜μ§€λ¦„
	COLORREF color;
};
void CMFCApplication1Dlg::OnPaint()
{
	CPaintDC dc(this); // 그리기λ₯Ό μœ„ν•œ λ””λ°”μ΄μŠ€ μ»¨ν…μŠ€νŠΈμž…λ‹ˆλ‹€.

	if (IsIconic())
	{
        // ...
	}
	else
	{
		CircleData * p = m_list;
		CBrush fill_brush, *p_old_brush = dc.GetCurrentBrush;
		for (int i = 0; i < MAX_COUNT; i++) {
			// dc.Ellipse(m_list[i].x - m_list[i].r, m_list[i].y - m_list[i].r,
			//		m_list[i].x + m_list[i].r, m_list[i].y + m_list[i].r);

			// μœ„ 연산을 μ’€ 더 κ°„λ‹¨νžˆ
			fill_brush.CreateSolidBrush(p->color);
			//p_old_brush = dc.SelectObject(&fill_brush);
			dc.SelectObject(&fill_brush);
			dc.Ellipse(p->x - p->r, p->y - p->r, p->x + p->r, p->y + p->r);
			p++;

			dc.SelectObject(p_old_brush);
			fill_brush.DeleteObject();
		}
		// CDialogEx::OnPaint();
	}
}
void CMFCApplication1Dlg::OnTimer(UINT_PTR nIDEvent)
{
	
	if (nIDEvent == 1) {
		CRect r;
		GetClientRect(r);

		int w = r.Width(), h = r.Height();

		CircleData * p = m_list;
		for (int i = 0; i < MAX_COUNT; i++) {
			p->r--;
			if (p->r <= 0) {
				p->x = rand() % w;
				p->y = rand() % h;
				p->r = rand() % 40 + 10;		// 10 ~ 49
				p->color = RGB(rand() % 256, rand() % 256, rand() % 256);
			}
			p++;
		}
		Invalidate();

	}
	else {
		CDialogEx::OnTimer(nIDEvent);
	}
}