mfc_pattern_brush2 - 8BitsCoding/RobotMentor GitHub Wiki


// Dlg.h
CBrush m_normal_brush, m_small_brush;
CImage m_normal_image, m_small_image, m_mem_view;
int m_radius = 10;
int m_alpha = 0;
int m_is_clicked = 0;
BOOL CMFCApplication2Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 이 λŒ€ν™” μƒμžμ˜ μ•„μ΄μ½˜μ„ μ„€μ •ν•©λ‹ˆλ‹€.  μ‘μš© ν”„λ‘œκ·Έλž¨μ˜ μ£Ό 창이 λŒ€ν™” μƒμžκ°€ 아닐 κ²½μš°μ—λŠ”
	//  ν”„λ ˆμž„μ›Œν¬κ°€ 이 μž‘μ—…μ„ μžλ™μœΌλ‘œ μˆ˜ν–‰ν•©λ‹ˆλ‹€.
	SetIcon(m_hIcon, TRUE);			// 큰 μ•„μ΄μ½˜μ„ μ„€μ •ν•©λ‹ˆλ‹€.
	SetIcon(m_hIcon, FALSE);		// μž‘μ€ μ•„μ΄μ½˜μ„ μ„€μ •ν•©λ‹ˆλ‹€.

	// TODO: 여기에 μΆ”κ°€ μ΄ˆκΈ°ν™” μž‘μ—…μ„ μΆ”κ°€ν•©λ‹ˆλ‹€.

	m_normal_image.Load(L"normal_image.png");
	m_small_image.Load(L"small_image.png");

	CRect win_rect, client_rect;
	GetWindowRect(win_rect);
	GetClientRect(client_rect);

	CBitmap * p_bmp = CBitmap::FromHandle(m_normal_image);
	m_normal_brush.CreatePatternBrush(p_bmp);

	p_bmp = CBitmap::FromHandle(m_small_image);
	m_small_brush.CreatePatternBrush(p_bmp);

	int w_fix = win_rect.Width() - client_rect.Width();
	int h_fix = win_rect.Height() - client_rect.Height();

	SetWindowPos(NULL, 0, 0, m_small_image.GetWidth()+ w_fix, m_small_image.GetHeight()+ h_fix, SWP_NOMOVE);

	SetBackgroundColor(RGB(0, 0, 0));
	m_mem_view.Create(m_small_image.GetWidth(), m_small_image.GetHeight(), 32);

	return TRUE;  // 포컀슀λ₯Ό μ»¨νŠΈλ‘€μ— μ„€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ TRUEλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
}
void CMFCApplication2Dlg::OnMouseMove(UINT nFlags, CPoint point)
{
	CClientDC dc(this);

	CDC * p_dc = CDC::FromHandle(m_mem_view.GetDC());

	p_dc->FillSolidRect(0, 0, m_small_image.GetWidth(), m_small_image.GetHeight(), RGB(0, 0, 0));
	m_small_image.AlphaBlend(*p_dc, 0, 0, m_alpha);

	CBrush * p_old_brush;

	if (m_is_clicked) {
		p_dc->SetBrushOrg(point.x * (-1), point.y * (-1));
		p_old_brush = p_dc->SelectObject(&m_normal_brush);
	}
	else {
		p_dc->SetBrushOrg(0, 0);
		p_old_brush = p_dc->SelectObject(&m_small_brush);
	}
	
	p_dc->Ellipse(point.x - m_radius, point.y - m_radius, point.x + m_radius, point.y + m_radius);
	p_dc->SelectObject(p_old_brush);

	if (m_is_clicked) {
		CPen grid_pen(PS_DOT, 1, RGB(160, 160, 160));
		CPen * p_old_pen = p_dc->SelectObject(&grid_pen);
		p_dc->SetBkMode(TRANSPARENT);
		p_dc->MoveTo(point.x, point.y - m_radius);
		p_dc->LineTo(point.x, point.y + m_radius);

		p_dc->MoveTo(point.x - m_radius, point.y);
		p_dc->LineTo(point.x + m_radius, point.y);

		p_dc->SelectObject(p_old_pen);
	}

	m_mem_view.ReleaseDC();
	m_mem_view.Draw(dc, 0, 0);
	// CDialogEx::OnMouseMove(nFlags, point);
}
void CMFCApplication2Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_is_clicked = 1;
	SetCapture();
	OnMouseMove(nFlags, point);

	CDialogEx::OnLButtonDown(nFlags, point);
}


void CMFCApplication2Dlg::OnLButtonUp(UINT nFlags, CPoint point)
{
	m_is_clicked = 0;
	ReleaseCapture();
	OnMouseMove(nFlags, point);

	CDialogEx::OnLButtonUp(nFlags, point);
}
BOOL CMFCApplication2Dlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	int old_radius = m_radius;
	if (zDelta > 0) {
		// νœ μ„ λ―ΈλŠ” 것
		if(m_radius >= 20) m_radius -= 10;

		if (nFlags & MK_CONTROL) {
			m_alpha -= 10;
			if (m_alpha <= 0) m_alpha = 0;
		}
	}
	else {
		// νœ μ„ λ‹ΉκΈ°λŠ” 것
		if(m_radius < 100) m_radius += 10;

		if (nFlags & MK_CONTROL) {
			m_alpha += 10;
			if (m_alpha >= 255) m_alpha = 255;
		}
	}
	if (old_radius != m_radius) {
		POINT pos;
		GetCursorPos(&pos);
		ScreenToClient(&pos);

		OnMouseMove(0, pos);
	}
	return CDialogEx::OnMouseWheel(nFlags, zDelta, pt);
}