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

MFC Mouse [WM_MOUSEMOVE, WM_PAINT]마우스 좌표 출력하기

by boolean 2015. 4. 6.
728x90

MFC Mouse [WM_MOUSEMOVE, WM_PAINT]


Project Name
MouseCombi
Class View
CMouseCombiApp
CMouseCombiDoc
CMouseCombiView
CMainFrame

마우스 움직임 좌표값을 출력해 보자

Class View ->CMouseCombiView->마우스 더블클릭 ->MouseCombiView.h에 Public 으로 CPoint 타입 객체 생성

// MouseCombiView.h : interface of the CMouseCombiView class
//

#pragma once


class CMouseCombiView : public CView
{
protected: // create from serialization only
	CMouseCombiView();
	DECLARE_DYNCREATE(CMouseCombiView)

// Attributes
public:
	CMouseCombiDoc* GetDocument() const;

// Operations
public:
	CPoint   m_ptMouse; //--------------------------객체 생성
// Overrides
public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

// Implementation
public:
	virtual ~CMouseCombiView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

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;                            //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("");                  //CString 타입 객체 strTmp 생성 및 초기화
	strTmp.Format(_T("%03d, %03d"), m_ptMouse.x, m_ptMouse.y);    // x,y 좌표 대입
	dc.TextOut(10, 10, strTmp);                                                     
 //윈도우 좌표(x = 10,  y = 10 )위치에 현재 마우스 좌표 출력
}

댓글