mfc_mouse_click - 8BitsCoding/RobotMentor GitHub Wiki
์ ์ฒ๋ผ ๋ค๋ชจ๋ฅผ ํด๋ฆญ ํ ๋๋๊ทธ ํ๋ฉด ์ด๋ํ๊ฒ ๋ง๋ค๊ณ ์ถ๋ค.
// Dlg.h
CRect m_rect;
char m_is_clicked = 0;
CPoint m_prev_pos;
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
void CMFCApplication5Dlg::OnPaint()
{
CPaintDC dc(this); // ๊ทธ๋ฆฌ๊ธฐ๋ฅผ ์ํ ๋๋ฐ์ด์ค ์ปจํ
์คํธ์
๋๋ค.
if (IsIconic())
{
// ...
}
else
{
dc.Rectangle(m_rect);
// CDialogEx::OnPaint();
}
}
void CMFCApplication5Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
//if (point.x >= m_rect.left && point.y >= m_rect.top &&
// point.x <= m_rect.right && point.y <= m_rect.bottom)
if(m_rect.PtInRect(point))
{
m_is_clicked = 1;
m_prev_pos = point;
SetCapture();
}
CDialogEx::OnLButtonDown(nFlags, point);
}
void CMFCApplication5Dlg::OnLButtonUp(UINT nFlags, CPoint point)
{
if (m_is_clicked == 1) {
m_is_clicked = 0;
ReleaseCapture();
}
CDialogEx::OnLButtonUp(nFlags, point);
}
void CMFCApplication5Dlg::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_is_clicked == 1) {
CPoint move_pos;
move_pos = point - m_prev_pos;
m_rect = m_rect + move_pos;
/*
m_rect.left += move_pos.x;
m_rect.top += move_pos.y;
m_rect.right += move_pos.x;
m_rect.bottom += move_pos.y;
*/
m_prev_pos = point;
Invalidate();
}
CDialogEx::OnMouseMove(nFlags, point);
}