본문 바로가기
컴퓨터과학[2-1]/knou_[2-1]Visual_C

MFC Mouse [Mouse Drag & Drop]드레그 앤 드롭 을 해보자

by boolean 2015. 4. 6.
728x90

MFC Mouse [Mouse Drag & Drop]


Project Name
MouseCombi
Class View
CMouseCombiApp
CMouseCombiDoc
CMouseCombiView
CMainFrame

드레그 앤 드롭 을 해보자

Class View ->CMouseCombiView->마우스 더블클릭 ->MouseCombiView.h에 Public 으로 CPoint 타입 멤버선언

//--------------생략
public:
	CPoint   m_ptMouse, m_ptItemText;      //드레그할 아이템 m_ptItemText 멤버 선언
	bool     m_bDragFlag;                  //드레그 상태인지 아닌지 전달해줄 멤버 선언
// Overrides
//------------생략

Class View ->CMouseCombiView->마우스 더블클릭 ->MouseCombiView.cpp에 멤버 초기화

//--------------생략
CMouseCombiView::CMouseCombiView()
{
	// TODO: add construction code here
	m_ptItemText = CPoint(10, 30);       //드레그 아이템 초기위치 초기화
	m_bDragFlag  = false;                 //드레그 초기상태 false로 초기화
}
//------------생략

Class View ->CMouseCombiView->마우스 우클릭 ->속성(Properties) ->Messages ->WM_LBUTTONDOWN

void CMouseCombiView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(point.x >= m_ptItemText.x - 10 && point.x <= m_ptItemText.x +10
		&& point.y >= m_ptItemText.y - 10 && point.y <= m_ptItemText.y + 10)
	{
		m_bDragFlag = true;
		
		RedrawWindow();
	}

	CView::OnLButtonDown(nFlags, point);
}

Class View ->CMouseCombiView->마우스 우클릭 ->속성(Properties) ->Messages ->WM_LBUTTONUP

void CMouseCombiView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(m_bDragFlag)
	{
		m_bDragFlag = false;
		m_ptItemText = point;
		RedrawWindow();
	}
	CView::OnLButtonUp(nFlags, point);
}

Class View ->CMouseCombiView->마우스 우클릭 ->속성(Properties) ->Messages ->WM_MOUSEMOVE

void CMouseCombiView::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	m_ptMouse = point; ////마우스 좌표
	RedrawWindow();  ////////////////
	if(m_bDragFlag)
	{
		//m_bDragFlag = false;
		m_ptItemText = point;
		RedrawWindow();
	}
	CView::OnMouseMove(nFlags, point);
}

Class View ->CMouseCombiView->마우스 우클릭 ->속성(Properties) ->Messages ->WM_PAINT

void CMouseCombiView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CView::OnPaint() for painting messages

	CString strTmp = _T("");                                       ////////////
	strTmp.Format(_T("%03d, %03d"), m_ptMouse.x, m_ptMouse.y);   //마우스 좌표
	dc.TextOut(10, 10, strTmp);                                  ////////////

	if(m_bDragFlag)
		dc.TextOut(10, 50, _T("Dragging"));   //드레그 중이면 Dragging 출력
	else
		dc.TextOut(10, 50, _T("--------"));   //아니면 --------출력
	dc.TextOut(m_ptItemText.x, m_ptItemText.y, _T("Test_Item"));
	//AfxMessageBox(strTmp);
}

댓글