mfc_poly_2 - 8BitsCoding/RobotMentor GitHub Wiki


닀차원 λ°°μ—΄μ˜ λ„€λͺ¨ 쀑 ν•˜λ‚˜λ₯Ό μ„ νƒν•˜λ©΄ 색상 λ³€κ²½

// Dlg.h
public:
CBrush m_select_brush, m_unselect_brush;
CPen m_select_pen, m_unselect_pen;
int m_index = -1;			// 0 ~ 17
//int m_index_x = -1;		// 0 ~ 5
//int m_index_y = -1;		// 0 ~ 2
void CMFCApplication1Dlg::OnPaint()
{
	CPaintDC dc(this); // 그리기λ₯Ό μœ„ν•œ λ””λ°”μ΄μŠ€ μ»¨ν…μŠ€νŠΈμž…λ‹ˆλ‹€.

	if (IsIconic())
	{
        // ...
	}
	else
	{
		CBrush * p_old_brush = dc.SelectObject(&m_unselect_brush);
		CPen * p_old_pen = dc.SelectObject(&m_unselect_pen);

		int x, y;

		for (y = 0; y < 3; y++) {
			for (x = 0; x < 6; x++) {
				dc.Rectangle(x * 100, y* 100, 101 + x * 100, 101 + y*100);
			}
		}

		if (m_index != -1) {
			x = m_index % 6;
			y = m_index / 6;

			dc.SelectObject(&m_select_brush);
			dc.SelectObject(&m_select_pen);

			dc.Rectangle(x * 100, y * 100, 101 + x * 100, 101 + y * 100);
		}
		
		dc.SelectObject(p_old_brush);
		dc.SelectObject(p_old_pen);

		// CDialogEx::OnPaint();
	}
}
void CMFCApplication1Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	//if (point.y < 100) {
		/*
		m_index_x = -1;

		for (int i = 0; i < 6; i++) {
			if (point.x < 100 + i * 100) {
				m_index_x = i;
				break;
			}
		}
		*/

		/*
		// μ½”λ“œλ₯Ό κ°„λ‹¨νžˆ
		m_index_x = point.x / 100;
		m_index_y = point.y / 100;
		if (m_index_x >= 6 || m_index_y >= 3) m_index_x = -1;
		*/
		
	point.x = point.x / 100;		// 0 ~ 5
	point.y = point.y / 100;		// 0 ~ 2

	if (point.x >= 6 || point.y >= 3) m_index = -1;
	m_index = point.y * 6 + point.x;

	Invalidate(TRUE);
	//}

	CDialogEx::OnLButtonDown(nFlags, point);
}